blob: e47c456577c2289051d45db098e1e7bd7c287a04 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010079 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010090 }
91
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
500 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
510 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 const mbedtls_md_info_t *md_info;
664 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100665 int ret;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 hs = ( slen + 1 ) / 2;
673 S1 = secret;
674 S2 = secret + slen - hs;
675
676 nb = strlen( label );
677 memcpy( tmp + 20, label, nb );
678 memcpy( tmp + 20 + nb, random, rlen );
679 nb += rlen;
680
681 /*
682 * First compute P_md5(secret,label+random)[0..dlen]
683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100688 return( ret );
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
691 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
692 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 for( i = 0; i < dlen; i += 16 )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_hmac_reset ( &md_ctx );
697 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
698 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_md_hmac_reset ( &md_ctx );
701 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
702 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
705
706 for( j = 0; j < k; j++ )
707 dstbuf[i + j] = h_i[j];
708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100711
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 /*
713 * XOR out with P_sha1(secret,label+random)[0..dlen]
714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719 return( ret );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
722 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
723 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 for( i = 0; i < dlen; i += 20 )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md_hmac_reset ( &md_ctx );
728 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
729 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_reset ( &md_ctx );
732 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
736
737 for( j = 0; j < k; j++ )
738 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
739 }
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
744 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
746 return( 0 );
747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100751#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
752MBEDTLS_ALWAYS_INLINE static inline
753#else
754static
755#endif
756int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
760 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761{
762 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
777 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
780 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100781 memcpy( tmp + md_len, label, nb );
782 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000783 nb += rlen;
784
785 /*
786 * Compute P_<hash>(secret, label + random)[0..dlen]
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100789 return( ret );
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
792 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
793 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100794
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_hmac_reset ( &md_ctx );
798 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
799 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_md_hmac_reset ( &md_ctx );
802 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
803 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100805 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806
807 for( j = 0; j < k; j++ )
808 dstbuf[i + j] = h_i[j];
809 }
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100812
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500813 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
814 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
816 return( 0 );
817}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100820MBEDTLS_NO_INLINE static int tls_prf_sha256(
821 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100822 const char *label,
823 const unsigned char *random, size_t rlen,
824 unsigned char *dstbuf, size_t dlen )
825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 label, random, rlen, dstbuf, dlen ) );
828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100832MBEDTLS_NO_INLINE static int tls_prf_sha384(
833 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200834 const char *label,
835 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836 unsigned char *dstbuf, size_t dlen )
837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100839 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SHA512_C */
842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100844/*
845 * Call the appropriate PRF function
846 */
Hanno Becker2793f742019-08-16 14:28:43 +0100847MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100848 mbedtls_md_type_t hash,
849 const unsigned char *secret, size_t slen,
850 const char *label,
851 const unsigned char *random, size_t rlen,
852 unsigned char *dstbuf, size_t dlen )
853{
854#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
855 (void) hash;
856#endif
857
858#if defined(MBEDTLS_SSL_PROTO_SSL3)
859 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
860 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
864 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
865 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869#if defined(MBEDTLS_SHA512_C)
870 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
871 hash == MBEDTLS_MD_SHA384 )
872 {
873 return( tls_prf_sha384( secret, slen, label, random, rlen,
874 dstbuf, dlen ) );
875 }
876 else
877#endif
878#if defined(MBEDTLS_SHA256_C)
879 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
880 {
881 return( tls_prf_sha256( secret, slen, label, random, rlen,
882 dstbuf, dlen ) );
883 }
884#endif
885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
886
887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
888}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200889
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100890#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100891MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100892 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
893{
894 const char *sender;
895 mbedtls_md5_context md5;
896 mbedtls_sha1_context sha1;
897
898 unsigned char padbuf[48];
899 unsigned char md5sum[16];
900 unsigned char sha1sum[20];
901
902 mbedtls_ssl_session *session = ssl->session_negotiate;
903 if( !session )
904 session = ssl->session;
905
906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
907
908 mbedtls_md5_init( &md5 );
909 mbedtls_sha1_init( &sha1 );
910
911 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
912 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
913
914 /*
915 * SSLv3:
916 * hash =
917 * MD5( master + pad2 +
918 * MD5( handshake + sender + master + pad1 ) )
919 * + SHA1( master + pad2 +
920 * SHA1( handshake + sender + master + pad1 ) )
921 */
922
923#if !defined(MBEDTLS_MD5_ALT)
924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
925 md5.state, sizeof( md5.state ) );
926#endif
927
928#if !defined(MBEDTLS_SHA1_ALT)
929 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
930 sha1.state, sizeof( sha1.state ) );
931#endif
932
933 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
934 : "SRVR";
935
936 memset( padbuf, 0x36, 48 );
937
938 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
939 mbedtls_md5_update_ret( &md5, session->master, 48 );
940 mbedtls_md5_update_ret( &md5, padbuf, 48 );
941 mbedtls_md5_finish_ret( &md5, md5sum );
942
943 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
944 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
945 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
946 mbedtls_sha1_finish_ret( &sha1, sha1sum );
947
948 memset( padbuf, 0x5C, 48 );
949
950 mbedtls_md5_starts_ret( &md5 );
951 mbedtls_md5_update_ret( &md5, session->master, 48 );
952 mbedtls_md5_update_ret( &md5, padbuf, 48 );
953 mbedtls_md5_update_ret( &md5, md5sum, 16 );
954 mbedtls_md5_finish_ret( &md5, buf );
955
956 mbedtls_sha1_starts_ret( &sha1 );
957 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
958 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
959 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
960 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
961
962 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
963
964 mbedtls_md5_free( &md5 );
965 mbedtls_sha1_free( &sha1 );
966
967 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
968 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
969 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
970
971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
972}
973#endif /* MBEDTLS_SSL_PROTO_SSL3 */
974
975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100976MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100977 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
978{
979 int len = 12;
980 const char *sender;
981 mbedtls_md5_context md5;
982 mbedtls_sha1_context sha1;
983 unsigned char padbuf[36];
984
985 mbedtls_ssl_session *session = ssl->session_negotiate;
986 if( !session )
987 session = ssl->session;
988
989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
990
991 mbedtls_md5_init( &md5 );
992 mbedtls_sha1_init( &sha1 );
993
994 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
995 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
996
997 /*
998 * TLSv1:
999 * hash = PRF( master, finished_label,
1000 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1001 */
1002
1003#if !defined(MBEDTLS_MD5_ALT)
1004 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1005 md5.state, sizeof( md5.state ) );
1006#endif
1007
1008#if !defined(MBEDTLS_SHA1_ALT)
1009 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1010 sha1.state, sizeof( sha1.state ) );
1011#endif
1012
1013 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1014 ? "client finished"
1015 : "server finished";
1016
1017 mbedtls_md5_finish_ret( &md5, padbuf );
1018 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1019
1020 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1021 mbedtls_ssl_suite_get_mac(
1022 mbedtls_ssl_ciphersuite_from_id(
1023 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1024 session->master, 48, sender,
1025 padbuf, 36, buf, len );
1026
1027 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1028
1029 mbedtls_md5_free( &md5 );
1030 mbedtls_sha1_free( &sha1 );
1031
1032 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1033
1034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1035}
1036#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1037
1038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1039#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001040MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001041 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1042{
1043 int len = 12;
1044 const char *sender;
1045 mbedtls_sha256_context sha256;
1046 unsigned char padbuf[32];
1047
1048 mbedtls_ssl_session *session = ssl->session_negotiate;
1049 if( !session )
1050 session = ssl->session;
1051
1052 mbedtls_sha256_init( &sha256 );
1053
1054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1055
1056 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1057
1058 /*
1059 * TLSv1.2:
1060 * hash = PRF( master, finished_label,
1061 * Hash( handshake ) )[0.11]
1062 */
1063
1064#if !defined(MBEDTLS_SHA256_ALT)
1065 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1066 sha256.state, sizeof( sha256.state ) );
1067#endif
1068
1069 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1070 ? "client finished"
1071 : "server finished";
1072
1073 mbedtls_sha256_finish_ret( &sha256, padbuf );
1074
1075 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1076 mbedtls_ssl_suite_get_mac(
1077 mbedtls_ssl_ciphersuite_from_id(
1078 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1079 session->master, 48, sender,
1080 padbuf, 32, buf, len );
1081
1082 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1083
1084 mbedtls_sha256_free( &sha256 );
1085
1086 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1087
1088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1089}
1090#endif /* MBEDTLS_SHA256_C */
1091
1092#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001093MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001094 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1095{
1096 int len = 12;
1097 const char *sender;
1098 mbedtls_sha512_context sha512;
1099 unsigned char padbuf[48];
1100
1101 mbedtls_ssl_session *session = ssl->session_negotiate;
1102 if( !session )
1103 session = ssl->session;
1104
1105 mbedtls_sha512_init( &sha512 );
1106
1107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1108
1109 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1110
1111 /*
1112 * TLSv1.2:
1113 * hash = PRF( master, finished_label,
1114 * Hash( handshake ) )[0.11]
1115 */
1116
1117#if !defined(MBEDTLS_SHA512_ALT)
1118 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1119 sha512.state, sizeof( sha512.state ) );
1120#endif
1121
1122 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1123 ? "client finished"
1124 : "server finished";
1125
1126 mbedtls_sha512_finish_ret( &sha512, padbuf );
1127
1128 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1129 mbedtls_ssl_suite_get_mac(
1130 mbedtls_ssl_ciphersuite_from_id(
1131 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1132 session->master, 48, sender,
1133 padbuf, 48, buf, len );
1134
1135 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1136
1137 mbedtls_sha512_free( &sha512 );
1138
1139 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1140
1141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1142}
1143#endif /* MBEDTLS_SHA512_C */
1144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1145
Hanno Becker2793f742019-08-16 14:28:43 +01001146MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1147 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001148 mbedtls_md_type_t hash,
1149 mbedtls_ssl_context *ssl,
1150 unsigned char *buf,
1151 int from )
1152{
1153#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1154 (void) hash;
1155#endif
1156
1157#if defined(MBEDTLS_SSL_PROTO_SSL3)
1158 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1159 ssl_calc_finished_ssl( ssl, buf, from );
1160 else
1161#endif
1162#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1163 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1164 ssl_calc_finished_tls( ssl, buf, from );
1165 else
1166#endif
1167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1168#if defined(MBEDTLS_SHA512_C)
1169 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1170 hash == MBEDTLS_MD_SHA384 )
1171 {
1172 ssl_calc_finished_tls_sha384( ssl, buf, from );
1173 }
1174 else
1175#endif
1176#if defined(MBEDTLS_SHA256_C)
1177 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1178 ssl_calc_finished_tls_sha256( ssl, buf, from );
1179 else
1180#endif
1181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1183
1184 return( 0 );
1185}
1186
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001187/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001188 * Populate a transform structure with session keys and all the other
1189 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001190 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001191 * Parameters:
1192 * - [in/out]: transform: structure to populate
1193 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001194 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001195 * - [in] ciphersuite
1196 * - [in] master
1197 * - [in] encrypt_then_mac
1198 * - [in] trunc_hmac
1199 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001200 * - [in] tls_prf: pointer to PRF to use for key derivation
1201 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001202 * - [in] minor_ver: SSL/TLS minor version
1203 * - [in] endpoint: client or server
1204 * - [in] ssl: optionally used for:
1205 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1206 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1207 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001208 */
Hanno Becker298a4702019-08-16 10:21:32 +01001209/* Force compilers to inline this function if it's used only
1210 * from one place, because at least ARMC5 doesn't do that
1211 * automatically. */
1212#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1213MBEDTLS_ALWAYS_INLINE static inline
1214#else
1215static
1216#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1217int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001218 int ciphersuite,
1219 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001220#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1222 int encrypt_then_mac,
1223#endif
1224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1225 int trunc_hmac,
1226#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001227#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001228#if defined(MBEDTLS_ZLIB_SUPPORT)
1229 int compression,
1230#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001231 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001232 int minor_ver,
1233 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001234 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001236 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 unsigned char keyblk[256];
1238 unsigned char *key1;
1239 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 unsigned char *mac_enc;
1241 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001243 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001244 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001245 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 const mbedtls_cipher_info_t *cipher_info;
1247 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001248
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001249#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1250 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1251 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001252 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001253 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001254#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001255
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001256 /*
1257 * Some data just needs copying into the structure
1258 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001259#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1260 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001261 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001263
1264#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001265 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001266#else
1267 ((void) minor_ver);
1268#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001270#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1271 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1272#endif
1273
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001274 /*
1275 * Get various info structures
1276 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001277 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001278 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001279 {
1280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001281 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1283 }
1284
Hanno Becker473f98f2019-06-26 10:27:32 +01001285 cipher_info = mbedtls_cipher_info_from_type(
1286 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 if( cipher_info == NULL )
1288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001290 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001292 }
1293
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 md_info = mbedtls_md_info_from_type(
1295 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 if( md_info == NULL )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001301 }
1302
Hanno Beckera5a2b082019-05-15 14:03:01 +01001303#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001304 /* Copy own and peer's CID if the use of the CID
1305 * extension has been negotiated. */
1306 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1307 {
1308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001309
Hanno Becker4932f9f2019-05-03 15:23:51 +01001310 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001311 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001312 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001313 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001314
1315 transform->out_cid_len = ssl->handshake->peer_cid_len;
1316 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1317 ssl->handshake->peer_cid_len );
1318 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1319 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001320 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001321#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001322
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001324 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001325 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001326 ret = ssl_prf( minor_ver,
1327 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1328 master, 48, "key expansion", randbytes, 64,
1329 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001330 if( ret != 0 )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001333 return( ret );
1334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001337 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001338 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 /*
1343 * Determine the appropriate key, IV and MAC length.
1344 */
Paul Bakker68884e32013-01-07 18:20:04 +01001345
Hanno Beckere7f2df02017-12-27 08:17:40 +00001346 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001347
Hanno Beckerf1229442018-01-03 15:32:31 +00001348#if defined(MBEDTLS_GCM_C) || \
1349 defined(MBEDTLS_CCM_C) || \
1350 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001352 cipher_info->mode == MBEDTLS_MODE_CCM ||
1353 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001355 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001356 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001357 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1358 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001359
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001360 /* All modes haves 96-bit IVs;
1361 * GCM and CCM has 4 implicit and 8 explicit bytes
1362 * ChachaPoly has all 12 bytes implicit
1363 */
Paul Bakker68884e32013-01-07 18:20:04 +01001364 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001365 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1366 transform->fixed_ivlen = 12;
1367 else
1368 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001369 }
1370 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001371#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1372#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1373 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1374 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001375 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001376 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1378 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001384 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001385 mac_key_len = mbedtls_md_get_size( md_info );
1386 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001389 /*
1390 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1391 * (rfc 6066 page 13 or rfc 2104 section 4),
1392 * so we only need to adjust the length here.
1393 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001394 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001397
1398#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1399 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001400 * HMAC implementation which also truncates the key
1401 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001402 mac_key_len = transform->maclen;
1403#endif
1404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001406
1407 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001408 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001410 else
1411#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1412 {
1413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Hanno Beckera9d5c452019-07-25 16:47:12 +01001417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001418 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001419 (unsigned) transform->ivlen,
1420 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 /*
1423 * Finally setup the cipher contexts, IVs and MAC secrets.
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001426 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001428 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001429 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Paul Bakker68884e32013-01-07 18:20:04 +01001431 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001432 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434 /*
1435 * This is not used in TLS v1.1.
1436 */
Paul Bakker48916f92012-09-16 19:57:18 +00001437 iv_copy_len = ( transform->fixed_ivlen ) ?
1438 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001439 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1440 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_CLI_C */
1445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001446 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001448 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001449 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Hanno Becker81c7b182017-11-09 18:39:33 +00001451 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001452 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001454 /*
1455 * This is not used in TLS v1.1.
1456 */
Paul Bakker48916f92012-09-16 19:57:18 +00001457 iv_copy_len = ( transform->fixed_ivlen ) ?
1458 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001459 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1460 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Hanno Becker92231322018-01-03 15:32:51 +00001470#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001472 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001473 {
Hanno Becker92231322018-01-03 15:32:51 +00001474 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001478 }
1479
Hanno Becker81c7b182017-11-09 18:39:33 +00001480 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1481 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001482 }
1483 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1485#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1486 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001487 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001488 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001489 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1490 For AEAD-based ciphersuites, there is nothing to do here. */
1491 if( mac_key_len != 0 )
1492 {
1493 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1494 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1495 }
Paul Bakker68884e32013-01-07 18:20:04 +01001496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497 else
1498#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001502 }
Hanno Becker92231322018-01-03 15:32:51 +00001503#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1506 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001507 {
1508 int ret = 0;
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001511
Hanno Beckere7f2df02017-12-27 08:17:40 +00001512 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001513 transform->iv_enc, transform->iv_dec,
1514 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001515 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001516 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1519 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001520 }
1521 }
Hanno Becker92231322018-01-03 15:32:51 +00001522#else
1523 ((void) mac_dec);
1524 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001526
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001527#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1528 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001529 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001530 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001531 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001532 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001533 iv_copy_len );
1534 }
1535#endif
1536
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001537 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001538 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 return( ret );
1542 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001543
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001544 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 cipher_info ) ) != 0 )
1546 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 return( ret );
1549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001552 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 return( ret );
1557 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001560 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 return( ret );
1565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_CIPHER_MODE_CBC)
1568 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1571 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 return( ret );
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001586 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001588 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001590 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593
Paul Bakker48916f92012-09-16 19:57:18 +00001594 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1595 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001597 if( deflateInit( &transform->ctx_deflate,
1598 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001599 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1602 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603 }
1604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 return( 0 );
1608}
1609
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001610#if defined(MBEDTLS_SSL_PROTO_SSL3)
1611static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1612 unsigned char hash[36],
1613 size_t *hlen )
1614{
1615 mbedtls_md5_context md5;
1616 mbedtls_sha1_context sha1;
1617 unsigned char pad_1[48];
1618 unsigned char pad_2[48];
1619
1620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1621
1622 mbedtls_md5_init( &md5 );
1623 mbedtls_sha1_init( &sha1 );
1624
1625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1627
1628 memset( pad_1, 0x36, 48 );
1629 memset( pad_2, 0x5C, 48 );
1630
1631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1633 mbedtls_md5_finish_ret( &md5, hash );
1634
1635 mbedtls_md5_starts_ret( &md5 );
1636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1638 mbedtls_md5_update_ret( &md5, hash, 16 );
1639 mbedtls_md5_finish_ret( &md5, hash );
1640
1641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1644
1645 mbedtls_sha1_starts_ret( &sha1 );
1646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1648 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1650
1651 *hlen = 36;
1652
1653 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1655
1656 mbedtls_md5_free( &md5 );
1657 mbedtls_sha1_free( &sha1 );
1658
1659 return;
1660}
1661#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1662
1663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1664static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1665 unsigned char hash[36],
1666 size_t *hlen )
1667{
1668 mbedtls_md5_context md5;
1669 mbedtls_sha1_context sha1;
1670
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1672
1673 mbedtls_md5_init( &md5 );
1674 mbedtls_sha1_init( &sha1 );
1675
1676 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1677 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1678
1679 mbedtls_md5_finish_ret( &md5, hash );
1680 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1681
1682 *hlen = 36;
1683
1684 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1686
1687 mbedtls_md5_free( &md5 );
1688 mbedtls_sha1_free( &sha1 );
1689
1690 return;
1691}
1692#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1693
1694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1695#if defined(MBEDTLS_SHA256_C)
1696static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1697 unsigned char hash[32],
1698 size_t *hlen )
1699{
1700 mbedtls_sha256_context sha256;
1701
1702 mbedtls_sha256_init( &sha256 );
1703
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1705
1706 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1707 mbedtls_sha256_finish_ret( &sha256, hash );
1708
1709 *hlen = 32;
1710
1711 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1713
1714 mbedtls_sha256_free( &sha256 );
1715
1716 return;
1717}
1718#endif /* MBEDTLS_SHA256_C */
1719
1720#if defined(MBEDTLS_SHA512_C)
1721static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1722 unsigned char hash[48],
1723 size_t *hlen )
1724{
1725 mbedtls_sha512_context sha512;
1726
1727 mbedtls_sha512_init( &sha512 );
1728
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1730
1731 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1732 mbedtls_sha512_finish_ret( &sha512, hash );
1733
1734 *hlen = 48;
1735
1736 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1738
1739 mbedtls_sha512_free( &sha512 );
1740
1741 return;
1742}
1743#endif /* MBEDTLS_SHA512_C */
1744#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1745
Hanno Becker2f41b242019-08-15 17:29:43 +01001746int mbedtls_ssl_calc_verify( int minor_ver,
1747 mbedtls_md_type_t hash,
1748 mbedtls_ssl_context const *ssl,
1749 unsigned char *dst,
1750 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001751{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001752#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1753 (void) hash;
1754#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001755
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001756#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001757 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001758 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001759 else
1760#endif
1761#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001762 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001763 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764 else
1765#endif
1766#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1767#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001768 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1769 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001771 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001772 }
1773 else
1774#endif
1775#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001776 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001778 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 }
1780 else
1781#endif
1782#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1783 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1785 }
1786
1787 return( 0 );
1788}
1789
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001790/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001791 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001792 *
1793 * Parameters:
1794 * [in/out] handshake
1795 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1796 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001797 * [out] master
1798 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001800static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001801 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001802 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001803{
1804 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001805
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001806/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1807/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1808/* (void) ssl; */
1809/* #endif */
1810
1811 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1812 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001813
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001814#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001816 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1818 return( 0 );
1819 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001820#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001821
1822 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1823 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001824
1825#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001826 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1827 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001828 {
1829 unsigned char session_hash[48];
1830 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831
Hanno Becker2f41b242019-08-15 17:29:43 +01001832 mbedtls_ssl_calc_verify(
1833 mbedtls_ssl_get_minor_ver( ssl ),
1834 mbedtls_ssl_suite_get_mac( ciphersuite ),
1835 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001836
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001837 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1838 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001839
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001840 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1841 mbedtls_ssl_suite_get_mac( ciphersuite ),
1842 handshake->premaster, handshake->pmslen,
1843 "extended master secret",
1844 session_hash, hash_len,
1845 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846 }
1847 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001848#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001849 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001850 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1851 mbedtls_ssl_suite_get_mac( ciphersuite ),
1852 handshake->premaster, handshake->pmslen,
1853 "master secret",
1854 handshake->randbytes, 64,
1855 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001856 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001857 if( ret != 0 )
1858 {
1859 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1860 return( ret );
1861 }
1862
1863 mbedtls_platform_zeroize( handshake->premaster,
1864 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001865
1866 return( 0 );
1867}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001868
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001869int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1870{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001871 int ret;
1872
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1874
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001875 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001876 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001877 ssl->session_negotiate->master,
1878 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001879 if( ret != 0 )
1880 {
1881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1882 return( ret );
1883 }
1884
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001885 /* Swap the client and server random values:
1886 * - MS derivation wanted client+server (RFC 5246 8.1)
1887 * - key derivation wants server+client (RFC 5246 6.3) */
1888 {
1889 unsigned char tmp[64];
1890 memcpy( tmp, ssl->handshake->randbytes, 64 );
1891 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1892 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1893 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1894 }
1895
1896 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001897 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001898 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1899 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001900#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001901#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001902 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001903#endif
1904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001905 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001906#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001907#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001908#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001909 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001910#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001912 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001913 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1914 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001915 if( ret != 0 )
1916 {
1917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1918 return( ret );
1919 }
1920
1921 /* We no longer need Server/ClientHello.random values */
1922 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1923 sizeof( ssl->handshake->randbytes ) );
1924
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001925 /* Allocate compression buffer */
1926#if defined(MBEDTLS_ZLIB_SUPPORT)
1927 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1928 ssl->compress_buf == NULL )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1931 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1932 if( ssl->compress_buf == NULL )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001935 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001936 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1937 }
1938 }
1939#endif
1940
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1942
1943 return( 0 );
1944}
1945
Hanno Becker09d23642019-07-22 17:18:18 +01001946int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1947{
1948 int ret;
1949
1950 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1951 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1952
Hanno Beckera3c2c172019-07-23 16:51:57 +01001953#if defined(MBEDTLS_USE_TINYCRYPT)
1954 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001955 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001956 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001957 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1958 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1959 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1960 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1961 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001962 {
1963 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001964 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001965
1966 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1967 ssl->handshake->ecdh_privkey,
1968 ssl->handshake->premaster,
1969 uecc_curve ) )
1970 {
1971 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1972 }
1973
1974 ssl->handshake->pmslen = NUM_ECC_BYTES;
1975 }
1976 else
1977#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001978#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1979 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1980 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1981 {
1982 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1983 ssl->handshake->premaster,
1984 MBEDTLS_PREMASTER_SIZE,
1985 &ssl->handshake->pmslen,
1986 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001987 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001988 {
1989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1990 return( ret );
1991 }
1992
1993 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1994 }
1995 else
1996#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001997#if defined(MBEDTLS_ECDH_C) && \
1998 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1999 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2000 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2001 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002002 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2003 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2004 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2005 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2006 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2007 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2008 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2009 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2010 {
2011 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2012 &ssl->handshake->pmslen,
2013 ssl->handshake->premaster,
2014 MBEDTLS_MPI_MAX_SIZE,
2015 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002016 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002017 {
2018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2019 return( ret );
2020 }
2021
2022 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2023 }
2024 else
2025#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2026 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2027 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2028 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2029#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2030 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2031 {
2032 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002033 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002034 {
2035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2036 return( ret );
2037 }
2038 }
2039 else
2040#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2041#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2042 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2043 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2044 {
2045 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2046 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2047 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002048 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002049 if( ret != 0 )
2050 {
2051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2052 return( ret );
2053 }
2054 }
2055 else
2056#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2057#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2058 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2059 == MBEDTLS_KEY_EXCHANGE_RSA )
2060 {
2061 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002062 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002063 * ssl_rsa_generate_partial_pms(). Only the
2064 * PMS length needs to be set. */
2065 ssl->handshake->pmslen = 48;
2066 }
2067 else
2068#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2069 {
2070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2072 }
2073
2074 return( 0 );
2075}
2076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2078int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002079{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002080 unsigned char *p = ssl->handshake->premaster;
2081 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002082 const unsigned char *psk = ssl->conf->psk;
2083 size_t psk_len = ssl->conf->psk_len;
2084
2085 /* If the psk callback was called, use its result */
2086 if( ssl->handshake->psk != NULL )
2087 {
2088 psk = ssl->handshake->psk;
2089 psk_len = ssl->handshake->psk_len;
2090 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002091
2092 /*
2093 * PMS = struct {
2094 * opaque other_secret<0..2^16-1>;
2095 * opaque psk<0..2^16-1>;
2096 * };
2097 * with "other_secret" depending on the particular key exchange
2098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2100 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002101 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002102 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002105 *(p++) = (unsigned char)( psk_len >> 8 );
2106 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002107
2108 if( end < p || (size_t)( end - p ) < psk_len )
2109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2110
2111 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002112 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002113 }
2114 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2116#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2117 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002118 {
2119 /*
2120 * other_secret already set by the ClientKeyExchange message,
2121 * and is 48 bytes long
2122 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002123 if( end - p < 2 )
2124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2125
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002126 *p++ = 0;
2127 *p++ = 48;
2128 p += 48;
2129 }
2130 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2132#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2133 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002134 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002135 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002136 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002137
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002138 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002140 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002141 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002142 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002145 return( ret );
2146 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002147 *(p++) = (unsigned char)( len >> 8 );
2148 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002149 p += len;
2150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002152 }
2153 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2155#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2156 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002157 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002158 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002159 size_t zlen;
2160
Hanno Becker982da7e2019-09-02 09:47:39 +01002161#if defined(MBEDTLS_USE_TINYCRYPT)
2162 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
2163 ((void) ret);
2164
2165 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2166 ssl->handshake->ecdh_privkey,
2167 p + 2,
2168 uecc_curve ) )
2169 {
2170 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2171 }
2172
2173 zlen = NUM_ECC_BYTES;
2174#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002176 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002177 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002178 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002181 return( ret );
2182 }
2183
Hanno Becker982da7e2019-09-02 09:47:39 +01002184 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2185 MBEDTLS_DEBUG_ECDH_Z );
2186#endif /* MBEDTLS_USE_TINYCRYPT */
2187
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188 *(p++) = (unsigned char)( zlen >> 8 );
2189 *(p++) = (unsigned char)( zlen );
2190 p += zlen;
2191
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192 }
2193 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2197 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002198 }
2199
2200 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002201 if( end - p < 2 )
2202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002203
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002204 *(p++) = (unsigned char)( psk_len >> 8 );
2205 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002206
2207 if( end < p || (size_t)( end - p ) < psk_len )
2208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2209
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002210 memcpy( p, psk, psk_len );
2211 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002212
2213 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2214
2215 return( 0 );
2216}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002220/*
2221 * SSLv3.0 MAC functions
2222 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002223#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002224static void ssl_mac( mbedtls_md_context_t *md_ctx,
2225 const unsigned char *secret,
2226 const unsigned char *buf, size_t len,
2227 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002228 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002229{
2230 unsigned char header[11];
2231 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002232 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2234 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002235
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002236 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002238 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002239 else
Paul Bakker68884e32013-01-07 18:20:04 +01002240 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
2242 memcpy( header, ctr, 8 );
2243 header[ 8] = (unsigned char) type;
2244 header[ 9] = (unsigned char)( len >> 8 );
2245 header[10] = (unsigned char)( len );
2246
Paul Bakker68884e32013-01-07 18:20:04 +01002247 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 mbedtls_md_starts( md_ctx );
2249 mbedtls_md_update( md_ctx, secret, md_size );
2250 mbedtls_md_update( md_ctx, padding, padlen );
2251 mbedtls_md_update( md_ctx, header, 11 );
2252 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002253 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Paul Bakker68884e32013-01-07 18:20:04 +01002255 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_md_starts( md_ctx );
2257 mbedtls_md_update( md_ctx, secret, md_size );
2258 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002259 mbedtls_md_update( md_ctx, out, md_size );
2260 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002263
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002264/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002265 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002266#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002267 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2268 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2269 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2270/* This function makes sure every byte in the memory region is accessed
2271 * (in ascending addresses order) */
2272static void ssl_read_memory( unsigned char *p, size_t len )
2273{
2274 unsigned char acc = 0;
2275 volatile unsigned char force;
2276
2277 for( ; len != 0; p++, len-- )
2278 acc ^= *p;
2279
2280 force = acc;
2281 (void) force;
2282}
2283#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2284
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002285/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002287 */
Hanno Becker3307b532017-12-27 21:37:21 +00002288
Hanno Beckera5a2b082019-05-15 14:03:01 +01002289#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002290/* This functions transforms a DTLS plaintext fragment and a record content
2291 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002292 *
2293 * struct {
2294 * opaque content[DTLSPlaintext.length];
2295 * ContentType real_type;
2296 * uint8 zeros[length_of_padding];
2297 * } DTLSInnerPlaintext;
2298 *
2299 * Input:
2300 * - `content`: The beginning of the buffer holding the
2301 * plaintext to be wrapped.
2302 * - `*content_size`: The length of the plaintext in Bytes.
2303 * - `max_len`: The number of Bytes available starting from
2304 * `content`. This must be `>= *content_size`.
2305 * - `rec_type`: The desired record content type.
2306 *
2307 * Output:
2308 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2309 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2310 *
2311 * Returns:
2312 * - `0` on success.
2313 * - A negative error code if `max_len` didn't offer enough space
2314 * for the expansion.
2315 */
2316static int ssl_cid_build_inner_plaintext( unsigned char *content,
2317 size_t *content_size,
2318 size_t remaining,
2319 uint8_t rec_type )
2320{
2321 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002322 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2323 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2324 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002325
2326 /* Write real content type */
2327 if( remaining == 0 )
2328 return( -1 );
2329 content[ len ] = rec_type;
2330 len++;
2331 remaining--;
2332
2333 if( remaining < pad )
2334 return( -1 );
2335 memset( content + len, 0, pad );
2336 len += pad;
2337 remaining -= pad;
2338
2339 *content_size = len;
2340 return( 0 );
2341}
2342
Hanno Becker7dc25772019-05-20 15:08:01 +01002343/* This function parses a DTLSInnerPlaintext structure.
2344 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002345static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2346 size_t *content_size,
2347 uint8_t *rec_type )
2348{
2349 size_t remaining = *content_size;
2350
2351 /* Determine length of padding by skipping zeroes from the back. */
2352 do
2353 {
2354 if( remaining == 0 )
2355 return( -1 );
2356 remaining--;
2357 } while( content[ remaining ] == 0 );
2358
2359 *content_size = remaining;
2360 *rec_type = content[ remaining ];
2361
2362 return( 0 );
2363}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002364#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002365
Hanno Becker99abf512019-05-20 14:50:53 +01002366/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002367 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002368static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002369 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002370 mbedtls_record *rec )
2371{
Hanno Becker99abf512019-05-20 14:50:53 +01002372 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002373 *
2374 * additional_data = seq_num + TLSCompressed.type +
2375 * TLSCompressed.version + TLSCompressed.length;
2376 *
Hanno Becker99abf512019-05-20 14:50:53 +01002377 * For the CID extension, this is extended as follows
2378 * (quoting draft-ietf-tls-dtls-connection-id-05,
2379 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002380 *
2381 * additional_data = seq_num + DTLSPlaintext.type +
2382 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002383 * cid +
2384 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002385 * length_of_DTLSInnerPlaintext;
2386 */
2387
Hanno Becker3307b532017-12-27 21:37:21 +00002388 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2389 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002390 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002391
Hanno Beckera5a2b082019-05-15 14:03:01 +01002392#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002393 if( rec->cid_len != 0 )
2394 {
2395 memcpy( add_data + 11, rec->cid, rec->cid_len );
2396 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2397 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2398 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2399 *add_data_len = 13 + 1 + rec->cid_len;
2400 }
2401 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002402#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002403 {
2404 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2405 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2406 *add_data_len = 13;
2407 }
Hanno Becker3307b532017-12-27 21:37:21 +00002408}
2409
Hanno Becker611a83b2018-01-03 14:27:32 +00002410int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2411 mbedtls_ssl_transform *transform,
2412 mbedtls_record *rec,
2413 int (*f_rng)(void *, unsigned char *, size_t),
2414 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002417 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002418 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002419 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002420 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002421 size_t post_avail;
2422
2423 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002424#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002425 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002426 ((void) ssl);
2427#endif
2428
2429 /* The PRNG is used for dynamic IV generation that's used
2430 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2431#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2432 ( defined(MBEDTLS_AES_C) || \
2433 defined(MBEDTLS_ARIA_C) || \
2434 defined(MBEDTLS_CAMELLIA_C) ) && \
2435 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2436 ((void) f_rng);
2437 ((void) p_rng);
2438#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Hanno Becker3307b532017-12-27 21:37:21 +00002442 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002443 {
Hanno Becker3307b532017-12-27 21:37:21 +00002444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2446 }
Hanno Becker505089d2019-05-01 09:45:57 +01002447 if( rec == NULL
2448 || rec->buf == NULL
2449 || rec->buf_len < rec->data_offset
2450 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002451#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002452 || rec->cid_len != 0
2453#endif
2454 )
Hanno Becker3307b532017-12-27 21:37:21 +00002455 {
2456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002458 }
2459
Hanno Becker3307b532017-12-27 21:37:21 +00002460 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002461 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002463 data, rec->data_len );
2464
2465 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2466
2467 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2468 {
2469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2470 (unsigned) rec->data_len,
2471 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2473 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002474
Hanno Beckera5a2b082019-05-15 14:03:01 +01002475#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002476 /*
2477 * Add CID information
2478 */
2479 rec->cid_len = transform->out_cid_len;
2480 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2481 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002482
2483 if( rec->cid_len != 0 )
2484 {
2485 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002486 * Wrap plaintext into DTLSInnerPlaintext structure.
2487 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002488 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002489 * Note that this changes `rec->data_len`, and hence
2490 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002491 */
2492 if( ssl_cid_build_inner_plaintext( data,
2493 &rec->data_len,
2494 post_avail,
2495 rec->type ) != 0 )
2496 {
2497 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2498 }
2499
2500 rec->type = MBEDTLS_SSL_MSG_CID;
2501 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002502#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002503
Hanno Becker92c930f2019-04-29 17:31:37 +01002504 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2505
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002507 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002509#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 if( mode == MBEDTLS_MODE_STREAM ||
2511 ( mode == MBEDTLS_MODE_CBC
2512#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002513 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002514#endif
2515 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 {
Hanno Becker3307b532017-12-27 21:37:21 +00002517 if( post_avail < transform->maclen )
2518 {
2519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2520 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2521 }
2522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002524 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2525 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002526 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002527 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002528 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2529 data, rec->data_len, rec->ctr, rec->type, mac );
2530 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002531 }
2532 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2535 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002536 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2537 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002538 {
Hanno Becker992b6872017-11-09 18:57:39 +00002539 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2540
Hanno Beckere83efe62019-04-29 13:52:53 +01002541 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002542
Hanno Becker3307b532017-12-27 21:37:21 +00002543 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002544 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002545 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2546 data, rec->data_len );
2547 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2548 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2549
2550 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002551 }
2552 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002553#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002557 }
2558
Hanno Becker3307b532017-12-27 21:37:21 +00002559 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2560 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002561
Hanno Becker3307b532017-12-27 21:37:21 +00002562 rec->data_len += transform->maclen;
2563 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002564 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002565 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002566#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002568 /*
2569 * Encrypt
2570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2572 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002574 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002575 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002577 "including %d bytes of padding",
2578 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Hanno Becker3307b532017-12-27 21:37:21 +00002580 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2581 transform->iv_enc, transform->ivlen,
2582 data, rec->data_len,
2583 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002586 return( ret );
2587 }
2588
Hanno Becker3307b532017-12-27 21:37:21 +00002589 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 }
Paul Bakker68884e32013-01-07 18:20:04 +01002595 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002597
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002598#if defined(MBEDTLS_GCM_C) || \
2599 defined(MBEDTLS_CCM_C) || \
2600 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002602 mode == MBEDTLS_MODE_CCM ||
2603 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002604 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002605 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002606 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002607 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002608
Hanno Becker3307b532017-12-27 21:37:21 +00002609 /* Check that there's space for both the authentication tag
2610 * and the explicit IV before and after the record content. */
2611 if( post_avail < transform->taglen ||
2612 rec->data_offset < explicit_iv_len )
2613 {
2614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2615 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2616 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002617
Paul Bakker68884e32013-01-07 18:20:04 +01002618 /*
2619 * Generate IV
2620 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2622 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002623 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002624 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002625 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2626 explicit_iv_len );
2627 /* Prefix record content with explicit IV. */
2628 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 }
2630 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2631 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002632 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 unsigned char i;
2634
2635 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2636
2637 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002638 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 }
2640 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002641 {
2642 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002645 }
2646
Hanno Beckere83efe62019-04-29 13:52:53 +01002647 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002648
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002649 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2650 iv, transform->ivlen );
2651 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002652 data - explicit_iv_len, explicit_iv_len );
2653 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002654 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002656 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002657 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002658
Paul Bakker68884e32013-01-07 18:20:04 +01002659 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002660 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002661 */
Hanno Becker3307b532017-12-27 21:37:21 +00002662
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002663 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002664 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002665 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002666 data, rec->data_len, /* source */
2667 data, &rec->data_len, /* destination */
2668 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002671 return( ret );
2672 }
2673
Hanno Becker3307b532017-12-27 21:37:21 +00002674 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2675 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002676
Hanno Becker3307b532017-12-27 21:37:21 +00002677 rec->data_len += transform->taglen + explicit_iv_len;
2678 rec->data_offset -= explicit_iv_len;
2679 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002680 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002681 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2684#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002685 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002688 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002689 size_t padlen, i;
2690 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002691
Hanno Becker3307b532017-12-27 21:37:21 +00002692 /* Currently we're always using minimal padding
2693 * (up to 255 bytes would be allowed). */
2694 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2695 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 padlen = 0;
2697
Hanno Becker3307b532017-12-27 21:37:21 +00002698 /* Check there's enough space in the buffer for the padding. */
2699 if( post_avail < padlen + 1 )
2700 {
2701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2702 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2703 }
2704
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002706 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Hanno Becker3307b532017-12-27 21:37:21 +00002708 rec->data_len += padlen + 1;
2709 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002712 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002713 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2714 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002715 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002716 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2717 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002718 {
Hanno Becker3307b532017-12-27 21:37:21 +00002719 if( f_rng == NULL )
2720 {
2721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2722 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2723 }
2724
2725 if( rec->data_offset < transform->ivlen )
2726 {
2727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2728 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2729 }
2730
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002731 /*
2732 * Generate IV
2733 */
Hanno Becker3307b532017-12-27 21:37:21 +00002734 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002735 if( ret != 0 )
2736 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002737
Hanno Becker3307b532017-12-27 21:37:21 +00002738 memcpy( data - transform->ivlen, transform->iv_enc,
2739 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002740
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002741 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002745 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002746 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002747 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Hanno Becker3307b532017-12-27 21:37:21 +00002749 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2750 transform->iv_enc,
2751 transform->ivlen,
2752 data, rec->data_len,
2753 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002756 return( ret );
2757 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002758
Hanno Becker3307b532017-12-27 21:37:21 +00002759 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2762 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002763 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002766 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2767 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002768 {
2769 /*
2770 * Save IV in SSL3 and TLS1
2771 */
Hanno Becker3307b532017-12-27 21:37:21 +00002772 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2773 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 }
Hanno Becker3307b532017-12-27 21:37:21 +00002775 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002776#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002777 {
2778 data -= transform->ivlen;
2779 rec->data_offset -= transform->ivlen;
2780 rec->data_len += transform->ivlen;
2781 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002784 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002785 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002786 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2787
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788 /*
2789 * MAC(MAC_write_key, seq_num +
2790 * TLSCipherText.type +
2791 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002792 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002793 * IV + // except for TLS 1.0
2794 * ENC(content + padding + padding_length));
2795 */
Hanno Becker3307b532017-12-27 21:37:21 +00002796
2797 if( post_avail < transform->maclen)
2798 {
2799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2800 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2801 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002802
Hanno Beckere83efe62019-04-29 13:52:53 +01002803 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002806 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002807 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002808
Hanno Becker3307b532017-12-27 21:37:21 +00002809 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002810 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002811 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2812 data, rec->data_len );
2813 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2814 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002815
Hanno Becker3307b532017-12-27 21:37:21 +00002816 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002817
Hanno Becker3307b532017-12-27 21:37:21 +00002818 rec->data_len += transform->maclen;
2819 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002820 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002821 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002824 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002826 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002827 {
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é-Gonnardf7dc3782013-09-13 14:10:44 +02002830 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002832 /* Make extra sure authentication was performed, exactly once */
2833 if( auth_done != 1 )
2834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837 }
2838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
2841 return( 0 );
2842}
2843
Hanno Becker40478be2019-07-12 08:23:59 +01002844int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002845 mbedtls_ssl_transform *transform,
2846 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002847{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002848 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002850 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002851#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002852 size_t padlen = 0, correct = 1;
2853#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002854 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002855 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002856 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002857
Hanno Becker611a83b2018-01-03 14:27:32 +00002858#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002859 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002860 ((void) ssl);
2861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002864 if( rec == NULL ||
2865 rec->buf == NULL ||
2866 rec->buf_len < rec->data_offset ||
2867 rec->buf_len - rec->data_offset < rec->data_len )
2868 {
2869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002871 }
2872
Hanno Becker4c6876b2017-12-27 21:28:58 +00002873 data = rec->buf + rec->data_offset;
2874 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Hanno Beckera5a2b082019-05-15 14:03:01 +01002876#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002877 /*
2878 * Match record's CID with incoming CID.
2879 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002880 if( rec->cid_len != transform->in_cid_len ||
2881 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2882 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002883 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002884 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002885#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2888 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002889 {
2890 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002891 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2892 transform->iv_dec,
2893 transform->ivlen,
2894 data, rec->data_len,
2895 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002898 return( ret );
2899 }
2900
Hanno Becker4c6876b2017-12-27 21:28:58 +00002901 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 }
Paul Bakker68884e32013-01-07 18:20:04 +01002907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002909#if defined(MBEDTLS_GCM_C) || \
2910 defined(MBEDTLS_CCM_C) || \
2911 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002913 mode == MBEDTLS_MODE_CCM ||
2914 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002915 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002916 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002917 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002918
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002919 /*
2920 * Prepare IV from explicit and implicit data.
2921 */
2922
2923 /* Check that there's enough space for the explicit IV
2924 * (at the beginning of the record) and the MAC (at the
2925 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002926 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002929 "+ taglen (%d)", rec->data_len,
2930 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002932 }
Paul Bakker68884e32013-01-07 18:20:04 +01002933
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002934#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002935 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2936 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002937 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002938
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002939 /* Fixed */
2940 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2941 /* Explicit */
2942 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002943 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002944 else
2945#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2946#if defined(MBEDTLS_CHACHAPOLY_C)
2947 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002948 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002949 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002950 unsigned char i;
2951
2952 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2953
2954 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002955 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002956 }
2957 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002958#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002959 {
2960 /* Reminder if we ever add an AEAD mode with a different size */
2961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2963 }
2964
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002965 /* Group changes to data, data_len, and add_data, because
2966 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002967 data += explicit_iv_len;
2968 rec->data_offset += explicit_iv_len;
2969 rec->data_len -= explicit_iv_len + transform->taglen;
2970
Hanno Beckere83efe62019-04-29 13:52:53 +01002971 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002972 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002973 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002974
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002975 /* Because of the check above, we know that there are
2976 * explicit_iv_len Bytes preceeding data, and taglen
2977 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002978 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002979 * mbedtls_cipher_auth_decrypt() below. */
2980
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002981 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002982 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002983 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002984
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002985 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002986 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002987 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002988 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2989 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002990 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002991 data, rec->data_len,
2992 data, &olen,
2993 data + rec->data_len,
2994 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2999 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003000
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003001 return( ret );
3002 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003003 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003005 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003006 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3009 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003010 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003012 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3014#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003015 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003018 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003019
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 /*
Paul Bakker45829992013-01-03 14:52:21 +01003021 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003024 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3025 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003026 {
3027 /* The ciphertext is prefixed with the CBC IV. */
3028 minlen += transform->ivlen;
3029 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003030#endif
Paul Bakker45829992013-01-03 14:52:21 +01003031
Hanno Becker4c6876b2017-12-27 21:28:58 +00003032 /* Size considerations:
3033 *
3034 * - The CBC cipher text must not be empty and hence
3035 * at least of size transform->ivlen.
3036 *
3037 * Together with the potential IV-prefix, this explains
3038 * the first of the two checks below.
3039 *
3040 * - The record must contain a MAC, either in plain or
3041 * encrypted, depending on whether Encrypt-then-MAC
3042 * is used or not.
3043 * - If it is, the message contains the IV-prefix,
3044 * the CBC ciphertext, and the MAC.
3045 * - If it is not, the padded plaintext, and hence
3046 * the CBC ciphertext, has at least length maclen + 1
3047 * because there is at least the padding length byte.
3048 *
3049 * As the CBC ciphertext is not empty, both cases give the
3050 * lower bound minlen + maclen + 1 on the record size, which
3051 * we test for in the second check below.
3052 */
3053 if( rec->data_len < minlen + transform->ivlen ||
3054 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057 "+ 1 ) ( + expl IV )", rec->data_len,
3058 transform->ivlen,
3059 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003061 }
3062
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003063 /*
3064 * Authenticate before decrypt if enabled
3065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003067 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003068 {
Hanno Becker992b6872017-11-09 18:57:39 +00003069 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003072
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003073 /* Update data_len in tandem with add_data.
3074 *
3075 * The subtraction is safe because of the previous check
3076 * data_len >= minlen + maclen + 1.
3077 *
3078 * Afterwards, we know that data + data_len is followed by at
3079 * least maclen Bytes, which justifies the call to
3080 * mbedtls_ssl_safer_memcmp() below.
3081 *
3082 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003083 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003084 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003085
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003086 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003087 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3088 add_data_len );
3089 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3090 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003091 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3092 data, rec->data_len );
3093 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3094 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003095
Hanno Becker4c6876b2017-12-27 21:28:58 +00003096 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3097 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003098 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003099 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003100
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003101 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3103 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003107 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003108 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003109 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003111
3112 /*
3113 * Check length sanity
3114 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003115
3116 /* We know from above that data_len > minlen >= 0,
3117 * so the following check in particular implies that
3118 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003119 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003122 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003124 }
3125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003127 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003128 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003129 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003130 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3131 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003132 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003133 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003134 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003135
Hanno Becker4c6876b2017-12-27 21:28:58 +00003136 data += transform->ivlen;
3137 rec->data_offset += transform->ivlen;
3138 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003139 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003141
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003142 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3143
Hanno Becker4c6876b2017-12-27 21:28:58 +00003144 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3145 transform->iv_dec, transform->ivlen,
3146 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003149 return( ret );
3150 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003151
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003152 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003153 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003157 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003160 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3161 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003162 {
3163 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003164 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3165 * records is equivalent to CBC decryption of the concatenation
3166 * of the records; in other words, IVs are maintained across
3167 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003168 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003169 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3170 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003171 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003172#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
Hanno Becker4c6876b2017-12-27 21:28:58 +00003174 /* Safe since data_len >= minlen + maclen + 1, so after having
3175 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003176 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3177 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003178 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003179
Hanno Becker4c6876b2017-12-27 21:28:58 +00003180 if( auth_done == 1 )
3181 {
3182 correct *= ( rec->data_len >= padlen + 1 );
3183 padlen *= ( rec->data_len >= padlen + 1 );
3184 }
3185 else
Paul Bakker45829992013-01-03 14:52:21 +01003186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003188 if( rec->data_len < transform->maclen + padlen + 1 )
3189 {
3190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3191 rec->data_len,
3192 transform->maclen,
3193 padlen + 1 ) );
3194 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003195#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003196
3197 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3198 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
Hanno Becker4c6876b2017-12-27 21:28:58 +00003201 padlen++;
3202
3203 /* Regardless of the validity of the padding,
3204 * we have data_len >= padlen here. */
3205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003207 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3208 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003209 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003210 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212#if defined(MBEDTLS_SSL_DEBUG_ALL)
3213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003214 "should be no more than %d",
3215 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003216#endif
Paul Bakker45829992013-01-03 14:52:21 +01003217 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 }
3219 }
3220 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3222#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3223 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003224 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3225 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003227 /* The padding check involves a series of up to 256
3228 * consecutive memory reads at the end of the record
3229 * plaintext buffer. In order to hide the length and
3230 * validity of the padding, always perform exactly
3231 * `min(256,plaintext_len)` reads (but take into account
3232 * only the last `padlen` bytes for the padding check). */
3233 size_t pad_count = 0;
3234 size_t real_count = 0;
3235 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003236
Hanno Becker4c6876b2017-12-27 21:28:58 +00003237 /* Index of first padding byte; it has been ensured above
3238 * that the subtraction is safe. */
3239 size_t const padding_idx = rec->data_len - padlen;
3240 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3241 size_t const start_idx = rec->data_len - num_checks;
3242 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003243
Hanno Becker4c6876b2017-12-27 21:28:58 +00003244 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003245 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003246 real_count |= ( idx >= padding_idx );
3247 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003248 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003249 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003252 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003254#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003255 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003257 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3259 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003263 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003264
Hanno Becker4c6876b2017-12-27 21:28:58 +00003265 /* If the padding was found to be invalid, padlen == 0
3266 * and the subtraction is safe. If the padding was found valid,
3267 * padlen hasn't been changed and the previous assertion
3268 * data_len >= padlen still holds. */
3269 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003271 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003273 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003279#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003281 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003282#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
3284 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003285 * Authenticate if not done yet.
3286 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003288#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003289 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003290 {
Hanno Becker992b6872017-11-09 18:57:39 +00003291 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003292
Hanno Becker4c6876b2017-12-27 21:28:58 +00003293 /* If the initial value of padlen was such that
3294 * data_len < maclen + padlen + 1, then padlen
3295 * got reset to 1, and the initial check
3296 * data_len >= minlen + maclen + 1
3297 * guarantees that at this point we still
3298 * have at least data_len >= maclen.
3299 *
3300 * If the initial value of padlen was such that
3301 * data_len >= maclen + padlen + 1, then we have
3302 * subtracted either padlen + 1 (if the padding was correct)
3303 * or 0 (if the padding was incorrect) since then,
3304 * hence data_len >= maclen in any case.
3305 */
3306 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003307 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003310 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3311 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003312 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003313 ssl_mac( &transform->md_ctx_dec,
3314 transform->mac_dec,
3315 data, rec->data_len,
3316 rec->ctr, rec->type,
3317 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003318 }
3319 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3321#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3322 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003323 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3324 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003325 {
3326 /*
3327 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003328 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003329 *
3330 * Known timing attacks:
3331 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3332 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003333 * To compensate for different timings for the MAC calculation
3334 * depending on how much padding was removed (which is determined
3335 * by padlen), process extra_run more blocks through the hash
3336 * function.
3337 *
3338 * The formula in the paper is
3339 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3340 * where L1 is the size of the header plus the decrypted message
3341 * plus CBC padding and L2 is the size of the header plus the
3342 * decrypted message. This is for an underlying hash function
3343 * with 64-byte blocks.
3344 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3345 * correctly. We round down instead of up, so -56 is the correct
3346 * value for our calculations instead of -55.
3347 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003348 * Repeat the formula rather than defining a block_size variable.
3349 * This avoids requiring division by a variable at runtime
3350 * (which would be marginally less efficient and would require
3351 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003352 */
3353 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003354 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003355
3356 /*
3357 * The next two sizes are the minimum and maximum values of
3358 * in_msglen over all padlen values.
3359 *
3360 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003361 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003362 *
3363 * Note that max_len + maclen is never more than the buffer
3364 * length, as we previously did in_msglen -= maclen too.
3365 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003366 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003367 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3368
Hanno Becker4c6876b2017-12-27 21:28:58 +00003369 memset( tmp, 0, sizeof( tmp ) );
3370
3371 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003372 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003373#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3374 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003375 case MBEDTLS_MD_MD5:
3376 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003377 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003378 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003379 extra_run =
3380 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3381 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003382 break;
3383#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003384#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003385 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003386 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003387 extra_run =
3388 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3389 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003390 break;
3391#endif
3392 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3395 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003396
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003397 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003398
Hanno Beckere83efe62019-04-29 13:52:53 +01003399 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3400 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003401 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3402 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003403 /* Make sure we access everything even when padlen > 0. This
3404 * makes the synchronisation requirements for just-in-time
3405 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003406 ssl_read_memory( data + rec->data_len, padlen );
3407 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003408
3409 /* Call mbedtls_md_process at least once due to cache attacks
3410 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003411 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003412 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003413
Hanno Becker4c6876b2017-12-27 21:28:58 +00003414 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003415
3416 /* Make sure we access all the memory that could contain the MAC,
3417 * before we check it in the next code block. This makes the
3418 * synchronisation requirements for just-in-time Prime+Probe
3419 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003420 ssl_read_memory( data + min_len,
3421 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003422 }
3423 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3425 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003429 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003430
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003431#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003432 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3433 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003434#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003435
Hanno Becker4c6876b2017-12-27 21:28:58 +00003436 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3437 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#if defined(MBEDTLS_SSL_DEBUG_ALL)
3440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003441#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003442 correct = 0;
3443 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003444 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003445 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003446
3447 /*
3448 * Finally check the correct flag
3449 */
3450 if( correct == 0 )
3451 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003452#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003453
3454 /* Make extra sure authentication was performed, exactly once */
3455 if( auth_done != 1 )
3456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003460
Hanno Beckera5a2b082019-05-15 14:03:01 +01003461#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003462 if( rec->cid_len != 0 )
3463 {
3464 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3465 &rec->type );
3466 if( ret != 0 )
3467 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3468 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003469#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003472
3473 return( 0 );
3474}
3475
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003476#undef MAC_NONE
3477#undef MAC_PLAINTEXT
3478#undef MAC_CIPHERTEXT
3479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003481/*
3482 * Compression/decompression functions
3483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003485{
3486 int ret;
3487 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003488 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003489 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003490 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003493
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003494 if( len_pre == 0 )
3495 return( 0 );
3496
Paul Bakker2770fbd2012-07-03 13:30:23 +00003497 memcpy( msg_pre, ssl->out_msg, len_pre );
3498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003500 ssl->out_msglen ) );
3501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003503 ssl->out_msg, ssl->out_msglen );
3504
Paul Bakker48916f92012-09-16 19:57:18 +00003505 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3506 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3507 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003508 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003509
Paul Bakker48916f92012-09-16 19:57:18 +00003510 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003511 if( ret != Z_OK )
3512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3514 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003515 }
3516
Angus Grattond8213d02016-05-25 20:56:48 +10003517 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003518 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003521 ssl->out_msglen ) );
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003524 ssl->out_msg, ssl->out_msglen );
3525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003527
3528 return( 0 );
3529}
3530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003532{
3533 int ret;
3534 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003535 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003536 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003537 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003540
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003541 if( len_pre == 0 )
3542 return( 0 );
3543
Paul Bakker2770fbd2012-07-03 13:30:23 +00003544 memcpy( msg_pre, ssl->in_msg, len_pre );
3545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547 ssl->in_msglen ) );
3548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003550 ssl->in_msg, ssl->in_msglen );
3551
Paul Bakker48916f92012-09-16 19:57:18 +00003552 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3553 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3554 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003555 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003556 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003557
Paul Bakker48916f92012-09-16 19:57:18 +00003558 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559 if( ret != Z_OK )
3560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3562 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003563 }
3564
Angus Grattond8213d02016-05-25 20:56:48 +10003565 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003566 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003569 ssl->in_msglen ) );
3570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003571 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003572 ssl->in_msg, ssl->in_msglen );
3573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003575
3576 return( 0 );
3577}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3581static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583#if defined(MBEDTLS_SSL_PROTO_DTLS)
3584static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003585{
3586 /* If renegotiation is not enforced, retransmit until we would reach max
3587 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003588 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003589 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003590 uint32_t ratio =
3591 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3592 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003593 unsigned char doublings = 1;
3594
3595 while( ratio != 0 )
3596 {
3597 ++doublings;
3598 ratio >>= 1;
3599 }
3600
3601 if( ++ssl->renego_records_seen > doublings )
3602 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003604 return( 0 );
3605 }
3606 }
3607
3608 return( ssl_write_hello_request( ssl ) );
3609}
3610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003612
Paul Bakker5121ce52009-01-03 21:22:43 +00003613/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003614 * Fill the input message buffer by appending data to it.
3615 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003616 *
3617 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3618 * available (from this read and/or a previous one). Otherwise, an error code
3619 * is returned (possibly EOF or WANT_READ).
3620 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003621 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3622 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3623 * since we always read a whole datagram at once.
3624 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003625 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003626 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003629{
Paul Bakker23986e52011-04-24 08:57:21 +00003630 int ret;
3631 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003634
Hanno Beckera58a8962019-06-13 16:11:15 +01003635 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3636 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003639 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003641 }
3642
Angus Grattond8213d02016-05-25 20:56:48 +10003643 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003647 }
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003650 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003651 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003652 uint32_t timeout;
3653
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003654 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003655 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3656 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003657 {
3658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3659 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3661 }
3662
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003663 /*
3664 * The point is, we need to always read a full datagram at once, so we
3665 * sometimes read more then requested, and handle the additional data.
3666 * It could be the rest of the current record (while fetching the
3667 * header) and/or some other records in the same datagram.
3668 */
3669
3670 /*
3671 * Move to the next record in the already read datagram if applicable
3672 */
3673 if( ssl->next_record_offset != 0 )
3674 {
3675 if( ssl->in_left < ssl->next_record_offset )
3676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003679 }
3680
3681 ssl->in_left -= ssl->next_record_offset;
3682
3683 if( ssl->in_left != 0 )
3684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003686 ssl->next_record_offset ) );
3687 memmove( ssl->in_hdr,
3688 ssl->in_hdr + ssl->next_record_offset,
3689 ssl->in_left );
3690 }
3691
3692 ssl->next_record_offset = 0;
3693 }
3694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003696 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003697
3698 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003699 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003700 */
3701 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003704 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003705 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003706
3707 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003708 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003709 * are not at the beginning of a new record, the caller did something
3710 * wrong.
3711 */
3712 if( ssl->in_left != 0 )
3713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003716 }
3717
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003718 /*
3719 * Don't even try to read if time's out already.
3720 * This avoids by-passing the timer when repeatedly receiving messages
3721 * that will end up being dropped.
3722 */
3723 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003724 {
3725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003726 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003727 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003728 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003729 {
Angus Grattond8213d02016-05-25 20:56:48 +10003730 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003733 timeout = ssl->handshake->retransmit_timeout;
3734 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003735 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003738
Hanno Beckera58a8962019-06-13 16:11:15 +01003739 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3740 {
3741 ret = mbedtls_ssl_get_recv_timeout( ssl )
3742 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3743 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003744 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003745 {
3746 ret = mbedtls_ssl_get_recv( ssl )
3747 ( ssl->p_bio, ssl->in_hdr, len );
3748 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003751
3752 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003754 }
3755
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003756 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003759 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003762 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003763 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003766 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003767 }
3768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003772 return( ret );
3773 }
3774
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003775 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003778 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3779 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003781 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003782 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003785 return( ret );
3786 }
3787
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003788 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003791 }
3792
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 if( ret < 0 )
3794 return( ret );
3795
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003796 ssl->in_left = ret;
3797 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003798 MBEDTLS_SSL_TRANSPORT_ELSE
3799#endif /* MBEDTLS_SSL_PROTO_DTLS */
3800#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003803 ssl->in_left, nb_want ) );
3804
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003805 while( ssl->in_left < nb_want )
3806 {
3807 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003808
3809 if( ssl_check_timer( ssl ) != 0 )
3810 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3811 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003812 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003813 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003814 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003815 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3816 ssl->in_hdr + ssl->in_left, len,
3817 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003818 }
3819 else
3820 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003821 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003822 ssl->in_hdr + ssl->in_left, len );
3823 }
3824 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003827 ssl->in_left, nb_want ) );
3828 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003829
3830 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003832
3833 if( ret < 0 )
3834 return( ret );
3835
mohammad160352aecb92018-03-28 23:41:40 -07003836 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003837 {
Darryl Green11999bb2018-03-13 15:22:58 +00003838 MBEDTLS_SSL_DEBUG_MSG( 1,
3839 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003840 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003841 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3842 }
3843
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003844 ssl->in_left += ret;
3845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003846 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003847#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003850
3851 return( 0 );
3852}
3853
3854/*
3855 * Flush any data not yet written
3856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003858{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003859 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003860 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003863
Hanno Beckera58a8962019-06-13 16:11:15 +01003864 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003867 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003869 }
3870
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003871 /* Avoid incrementing counter if data is flushed */
3872 if( ssl->out_left == 0 )
3873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003875 return( 0 );
3876 }
3877
Paul Bakker5121ce52009-01-03 21:22:43 +00003878 while( ssl->out_left > 0 )
3879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003881 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003882
Hanno Becker2b1e3542018-08-06 11:19:13 +01003883 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003884 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003887
3888 if( ret <= 0 )
3889 return( ret );
3890
mohammad160352aecb92018-03-28 23:41:40 -07003891 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003892 {
Darryl Green11999bb2018-03-13 15:22:58 +00003893 MBEDTLS_SSL_DEBUG_MSG( 1,
3894 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003895 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3897 }
3898
Paul Bakker5121ce52009-01-03 21:22:43 +00003899 ssl->out_left -= ret;
3900 }
3901
Hanno Becker2b1e3542018-08-06 11:19:13 +01003902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003903 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003904 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003905 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003906 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003907 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003908#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003909#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003910 {
3911 ssl->out_hdr = ssl->out_buf + 8;
3912 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003913#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003914 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917
3918 return( 0 );
3919}
3920
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003921/*
3922 * Functions to handle the DTLS retransmission state machine
3923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003925/*
3926 * Append current handshake message to current outgoing flight
3927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3932 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3933 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934
3935 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003936 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == 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",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939 sizeof( mbedtls_ssl_flight_item ) ) );
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
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003943 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003944 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003947 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003948 }
3949
3950 /* Copy current handshake message with headers */
3951 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3952 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003953 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003954 msg->next = NULL;
3955
3956 /* Append to the current flight */
3957 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003958 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003959 else
3960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003962 while( cur->next != NULL )
3963 cur = cur->next;
3964 cur->next = msg;
3965 }
3966
Hanno Becker3b235902018-08-06 09:54:53 +01003967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968 return( 0 );
3969}
3970
3971/*
3972 * Free the current flight of handshake messages
3973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003975{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 mbedtls_ssl_flight_item *cur = flight;
3977 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978
3979 while( cur != NULL )
3980 {
3981 next = cur->next;
3982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 mbedtls_free( cur->p );
3984 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003985
3986 cur = next;
3987 }
3988}
3989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3991static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003992#endif
3993
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003994/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003995 * Swap transform_out and out_ctr with the alternative ones
3996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003998{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004000 unsigned char tmp_out_ctr[8];
4001
4002 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004005 return;
4006 }
4007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004009
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004010 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004011 tmp_transform = ssl->transform_out;
4012 ssl->transform_out = ssl->handshake->alt_transform_out;
4013 ssl->handshake->alt_transform_out = tmp_transform;
4014
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004015 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004016 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4017 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004018 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004019
4020 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004021 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4024 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004026 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4029 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004030 }
4031 }
4032#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004033}
4034
4035/*
4036 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004037 */
4038int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4039{
4040 int ret = 0;
4041
4042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4043
4044 ret = mbedtls_ssl_flight_transmit( ssl );
4045
4046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4047
4048 return( ret );
4049}
4050
4051/*
4052 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004053 *
4054 * Need to remember the current message in case flush_output returns
4055 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004056 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004057 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004058int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004059{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004060 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004064 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004066
4067 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004068 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004069 ssl_swap_epochs( ssl );
4070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004072 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073
4074 while( ssl->handshake->cur_msg != NULL )
4075 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004076 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004077 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004078
Hanno Beckere1dcb032018-08-17 16:47:58 +01004079 int const is_finished =
4080 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4081 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4082
Hanno Becker04da1892018-08-14 13:22:10 +01004083 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4084 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4085
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004086 /* Swap epochs before sending Finished: we can't do it after
4087 * sending ChangeCipherSpec, in case write returns WANT_READ.
4088 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004089 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004090 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004092 ssl_swap_epochs( ssl );
4093 }
4094
Hanno Becker67bc7c32018-08-06 11:33:50 +01004095 ret = ssl_get_remaining_payload_in_datagram( ssl );
4096 if( ret < 0 )
4097 return( ret );
4098 max_frag_len = (size_t) ret;
4099
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004100 /* CCS is copied as is, while HS messages may need fragmentation */
4101 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4102 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004103 if( max_frag_len == 0 )
4104 {
4105 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4106 return( ret );
4107
4108 continue;
4109 }
4110
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004111 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004112 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004113 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004114
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004115 /* Update position inside current message */
4116 ssl->handshake->cur_msg_p += cur->len;
4117 }
4118 else
4119 {
4120 const unsigned char * const p = ssl->handshake->cur_msg_p;
4121 const size_t hs_len = cur->len - 12;
4122 const size_t frag_off = p - ( cur->p + 12 );
4123 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004124 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004125
Hanno Beckere1dcb032018-08-17 16:47:58 +01004126 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004127 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004128 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004129 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004130
Hanno Becker67bc7c32018-08-06 11:33:50 +01004131 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4132 return( ret );
4133
4134 continue;
4135 }
4136 max_hs_frag_len = max_frag_len - 12;
4137
4138 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4139 max_hs_frag_len : rem_len;
4140
4141 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004142 {
4143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004144 (unsigned) cur_hs_frag_len,
4145 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004146 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004147
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004148 /* Messages are stored with handshake headers as if not fragmented,
4149 * copy beginning of headers then fill fragmentation fields.
4150 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4151 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004152
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004153 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4154 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4155 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4156
Hanno Becker67bc7c32018-08-06 11:33:50 +01004157 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4158 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4159 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004160
4161 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4162
Hanno Becker3f7b9732018-08-28 09:53:25 +01004163 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004164 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4165 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004166 ssl->out_msgtype = cur->type;
4167
4168 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004169 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004170 }
4171
4172 /* If done with the current message move to the next one if any */
4173 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4174 {
4175 if( cur->next != NULL )
4176 {
4177 ssl->handshake->cur_msg = cur->next;
4178 ssl->handshake->cur_msg_p = cur->next->p + 12;
4179 }
4180 else
4181 {
4182 ssl->handshake->cur_msg = NULL;
4183 ssl->handshake->cur_msg_p = NULL;
4184 }
4185 }
4186
4187 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004188 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004191 return( ret );
4192 }
4193 }
4194
Hanno Becker67bc7c32018-08-06 11:33:50 +01004195 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4196 return( ret );
4197
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004198 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4200 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004201 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004204 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4205 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004206
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004208
4209 return( 0 );
4210}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004211
4212/*
4213 * To be called when the last message of an incoming flight is received.
4214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004216{
4217 /* We won't need to resend that one any more */
4218 ssl_flight_free( ssl->handshake->flight );
4219 ssl->handshake->flight = NULL;
4220 ssl->handshake->cur_msg = NULL;
4221
4222 /* The next incoming flight will start with this msg_seq */
4223 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4224
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004225 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004226 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004227
Hanno Becker0271f962018-08-16 13:23:47 +01004228 /* Clear future message buffering structure. */
4229 ssl_buffering_free( ssl );
4230
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004231 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004232 ssl_set_timer( ssl, 0 );
4233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4235 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004238 }
4239 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004240 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004241}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004242
4243/*
4244 * To be called when the last message of an outgoing flight is send.
4245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004246void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004247{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004248 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004249 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4252 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004255 }
4256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004257 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004260
Paul Bakker5121ce52009-01-03 21:22:43 +00004261/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004262 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004263 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004264
4265/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004266 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004267 *
4268 * - fill in handshake headers
4269 * - update handshake checksum
4270 * - DTLS: save message for resending
4271 * - then pass to the record layer
4272 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004273 * DTLS: except for HelloRequest, messages are only queued, and will only be
4274 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004275 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004276 * Inputs:
4277 * - ssl->out_msglen: 4 + actual handshake message len
4278 * (4 is the size of handshake headers for TLS)
4279 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4280 * - ssl->out_msg + 4: the handshake message body
4281 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004282 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004283 * - ssl->out_msglen: the length of the record contents
4284 * (including handshake headers but excluding record headers)
4285 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004286 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004287int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004288{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004289 int ret;
4290 const size_t hs_len = ssl->out_msglen - 4;
4291 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004292
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4294
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004295 /*
4296 * Sanity checks
4297 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004298 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004299 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4300 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004301 /* In SSLv3, the client might send a NoCertificate alert. */
4302#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004303 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004304 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004305 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4306 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004307#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4308 {
4309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4311 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004313
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004314 /* Whenever we send anything different from a
4315 * HelloRequest we should be in a handshake - double check. */
4316 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4317 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004318 ssl->handshake == NULL )
4319 {
4320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004325 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004326 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004328 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004331 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004332#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004333
Hanno Beckerb50a2532018-08-06 11:52:54 +01004334 /* Double-check that we did not exceed the bounds
4335 * of the outgoing record buffer.
4336 * This should never fail as the various message
4337 * writing functions must obey the bounds of the
4338 * outgoing record buffer, but better be safe.
4339 *
4340 * Note: We deliberately do not check for the MTU or MFL here.
4341 */
4342 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4343 {
4344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4345 "size %u, maximum %u",
4346 (unsigned) ssl->out_msglen,
4347 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4349 }
4350
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004351 /*
4352 * Fill handshake headers
4353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004356 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4357 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4358 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004359
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004360 /*
4361 * DTLS has additional fields in the Handshake layer,
4362 * between the length field and the actual payload:
4363 * uint16 message_seq;
4364 * uint24 fragment_offset;
4365 * uint24 fragment_length;
4366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004368 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004369 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004370 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004371 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004372 {
4373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4374 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004375 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004376 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4378 }
4379
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004380 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004381 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004382
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004383 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004384 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004385 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004386 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4387 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4388 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004389 }
4390 else
4391 {
4392 ssl->out_msg[4] = 0;
4393 ssl->out_msg[5] = 0;
4394 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004395
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004396 /* Handshake hashes are computed without fragmentation,
4397 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004398 memset( ssl->out_msg + 6, 0x00, 3 );
4399 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004400 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004401#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004402
Hanno Becker0207e532018-08-28 10:28:28 +01004403 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004404 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004405 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004406 }
4407
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004408 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004410 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004411 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4412 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004413 {
4414 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004417 return( ret );
4418 }
4419 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004420 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004421#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004422 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004423 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004424 {
4425 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4426 return( ret );
4427 }
4428 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004429
4430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4431
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004432 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004433}
4434
4435/*
4436 * Record layer functions
4437 */
4438
4439/*
4440 * Write current record.
4441 *
4442 * Uses:
4443 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4444 * - ssl->out_msglen: length of the record content (excl headers)
4445 * - ssl->out_msg: record content
4446 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004447int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004448{
4449 int ret, done = 0;
4450 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004451 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004452
4453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004456 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004458 {
4459 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004462 return( ret );
4463 }
4464
4465 len = ssl->out_msglen;
4466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4470 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 ret = mbedtls_ssl_hw_record_write( ssl );
4475 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4478 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004479 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004480
4481 if( ret == 0 )
4482 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004483 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004485 if( !done )
4486 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004487 unsigned i;
4488 size_t protected_record_size;
4489
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004490 /* Skip writing the record content type to after the encryption,
4491 * as it may change when using the CID extension. */
4492
Hanno Becker2881d802019-05-22 14:44:53 +01004493 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4494 mbedtls_ssl_get_minor_ver( ssl ),
4495 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004496
Hanno Becker19859472018-08-06 09:40:20 +01004497 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004498 ssl->out_len[0] = (unsigned char)( len >> 8 );
4499 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004500
Paul Bakker48916f92012-09-16 19:57:18 +00004501 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004502 {
Hanno Becker3307b532017-12-27 21:37:21 +00004503 mbedtls_record rec;
4504
4505 rec.buf = ssl->out_iv;
4506 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4507 ( ssl->out_iv - ssl->out_buf );
4508 rec.data_len = ssl->out_msglen;
4509 rec.data_offset = ssl->out_msg - rec.buf;
4510
4511 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004512 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4513 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004514 ssl->conf->transport, rec.ver );
4515 rec.type = ssl->out_msgtype;
4516
Hanno Beckera5a2b082019-05-15 14:03:01 +01004517#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004518 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004519 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004520#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004521
Hanno Becker611a83b2018-01-03 14:27:32 +00004522 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004523 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004524 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004526 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004527 return( ret );
4528 }
4529
Hanno Becker3307b532017-12-27 21:37:21 +00004530 if( rec.data_offset != 0 )
4531 {
4532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4534 }
4535
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004536 /* Update the record content type and CID. */
4537 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004538#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004539 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004540#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004541 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004542 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4543 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004544 }
4545
Hanno Becker43395762019-05-03 14:46:38 +01004546 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004547
4548#if defined(MBEDTLS_SSL_PROTO_DTLS)
4549 /* In case of DTLS, double-check that we don't exceed
4550 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004551 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004552 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004553 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004554 if( ret < 0 )
4555 return( ret );
4556
4557 if( protected_record_size > (size_t) ret )
4558 {
4559 /* Should never happen */
4560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4561 }
4562 }
4563#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004564
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004565 /* Now write the potentially updated record content type. */
4566 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004569 "version = [%d:%d], msglen = %d",
4570 ssl->out_hdr[0], ssl->out_hdr[1],
4571 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004574 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004575
4576 ssl->out_left += protected_record_size;
4577 ssl->out_hdr += protected_record_size;
4578 ssl_update_out_pointers( ssl, ssl->transform_out );
4579
Hanno Becker04484622018-08-06 09:49:38 +01004580 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4581 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4582 break;
4583
4584 /* The loop goes to its end iff the counter is wrapping */
4585 if( i == ssl_ep_len( ssl ) )
4586 {
4587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4588 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 }
4591
Hanno Becker67bc7c32018-08-06 11:33:50 +01004592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004593 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004594 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004595 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004596 size_t remaining;
4597 ret = ssl_get_remaining_payload_in_datagram( ssl );
4598 if( ret < 0 )
4599 {
4600 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4601 ret );
4602 return( ret );
4603 }
4604
4605 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004606 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004607 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004608 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004609 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004610 else
4611 {
Hanno Becker513815a2018-08-20 11:56:09 +01004612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004613 }
4614 }
4615#endif /* MBEDTLS_SSL_PROTO_DTLS */
4616
4617 if( ( flush == SSL_FORCE_FLUSH ) &&
4618 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 return( ret );
4622 }
4623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004625
4626 return( 0 );
4627}
4628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004630
4631static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4632{
4633 if( ssl->in_msglen < ssl->in_hslen ||
4634 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4635 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4636 {
4637 return( 1 );
4638 }
4639 return( 0 );
4640}
Hanno Becker44650b72018-08-16 12:51:11 +01004641
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004642static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004643{
4644 return( ( ssl->in_msg[9] << 16 ) |
4645 ( ssl->in_msg[10] << 8 ) |
4646 ssl->in_msg[11] );
4647}
4648
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004649static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004650{
4651 return( ( ssl->in_msg[6] << 16 ) |
4652 ( ssl->in_msg[7] << 8 ) |
4653 ssl->in_msg[8] );
4654}
4655
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004656static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004657{
4658 uint32_t msg_len, frag_off, frag_len;
4659
4660 msg_len = ssl_get_hs_total_len( ssl );
4661 frag_off = ssl_get_hs_frag_off( ssl );
4662 frag_len = ssl_get_hs_frag_len( ssl );
4663
4664 if( frag_off > msg_len )
4665 return( -1 );
4666
4667 if( frag_len > msg_len - frag_off )
4668 return( -1 );
4669
4670 if( frag_len + 12 > ssl->in_msglen )
4671 return( -1 );
4672
4673 return( 0 );
4674}
4675
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004676/*
4677 * Mark bits in bitmask (used for DTLS HS reassembly)
4678 */
4679static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4680{
4681 unsigned int start_bits, end_bits;
4682
4683 start_bits = 8 - ( offset % 8 );
4684 if( start_bits != 8 )
4685 {
4686 size_t first_byte_idx = offset / 8;
4687
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004688 /* Special case */
4689 if( len <= start_bits )
4690 {
4691 for( ; len != 0; len-- )
4692 mask[first_byte_idx] |= 1 << ( start_bits - len );
4693
4694 /* Avoid potential issues with offset or len becoming invalid */
4695 return;
4696 }
4697
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004698 offset += start_bits; /* Now offset % 8 == 0 */
4699 len -= start_bits;
4700
4701 for( ; start_bits != 0; start_bits-- )
4702 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4703 }
4704
4705 end_bits = len % 8;
4706 if( end_bits != 0 )
4707 {
4708 size_t last_byte_idx = ( offset + len ) / 8;
4709
4710 len -= end_bits; /* Now len % 8 == 0 */
4711
4712 for( ; end_bits != 0; end_bits-- )
4713 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4714 }
4715
4716 memset( mask + offset / 8, 0xFF, len / 8 );
4717}
4718
4719/*
4720 * Check that bitmask is full
4721 */
4722static int ssl_bitmask_check( unsigned char *mask, size_t len )
4723{
4724 size_t i;
4725
4726 for( i = 0; i < len / 8; i++ )
4727 if( mask[i] != 0xFF )
4728 return( -1 );
4729
4730 for( i = 0; i < len % 8; i++ )
4731 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4732 return( -1 );
4733
4734 return( 0 );
4735}
4736
Hanno Becker56e205e2018-08-16 09:06:12 +01004737/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004738static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004739 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004740{
Hanno Becker56e205e2018-08-16 09:06:12 +01004741 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004742
Hanno Becker56e205e2018-08-16 09:06:12 +01004743 alloc_len = 12; /* Handshake header */
4744 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004745
Hanno Beckerd07df862018-08-16 09:14:58 +01004746 if( add_bitmap )
4747 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004748
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004749 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004750}
Hanno Becker56e205e2018-08-16 09:06:12 +01004751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004753
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004754static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004755{
4756 return( ( ssl->in_msg[1] << 16 ) |
4757 ( ssl->in_msg[2] << 8 ) |
4758 ssl->in_msg[3] );
4759}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004760
Simon Butcher99000142016-10-13 17:21:01 +01004761int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004766 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004768 }
4769
Hanno Becker12555c62018-08-16 12:47:53 +01004770 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004773 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004774 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004777 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004778 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004779 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004780 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004781
Hanno Becker44650b72018-08-16 12:51:11 +01004782 if( ssl_check_hs_header( ssl ) != 0 )
4783 {
4784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4785 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4786 }
4787
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004788 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004789 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4790 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4791 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4792 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004793 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004794 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4795 {
4796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4797 recv_msg_seq,
4798 ssl->handshake->in_msg_seq ) );
4799 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4800 }
4801
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004802 /* Retransmit only on last message from previous flight, to avoid
4803 * too many retransmissions.
4804 * Besides, No sane server ever retransmits HelloVerifyRequest */
4805 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004809 "message_seq = %d, start_of_flight = %d",
4810 recv_msg_seq,
4811 ssl->handshake->in_flight_start_seq ) );
4812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004816 return( ret );
4817 }
4818 }
4819 else
4820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004822 "message_seq = %d, expected = %d",
4823 recv_msg_seq,
4824 ssl->handshake->in_msg_seq ) );
4825 }
4826
Hanno Becker90333da2017-10-10 11:27:13 +01004827 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004828 }
4829 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004830
Hanno Becker6d97ef52018-08-16 13:09:04 +01004831 /* Message reassembly is handled alongside buffering of future
4832 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004833 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004834 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004835 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004838 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004839 }
4840 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004841 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004842#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004843#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004844 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004845 /* With TLS we don't handle fragmentation (for now) */
4846 if( ssl->in_msglen < ssl->in_hslen )
4847 {
4848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4849 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4850 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004851 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004852#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004853
Simon Butcher99000142016-10-13 17:21:01 +01004854 return( 0 );
4855}
4856
4857void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4858{
Hanno Becker0271f962018-08-16 13:23:47 +01004859 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004860
Hanno Becker0271f962018-08-16 13:23:47 +01004861 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004862 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004863
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004864 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004866 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004867 ssl->handshake != NULL )
4868 {
Hanno Becker0271f962018-08-16 13:23:47 +01004869 unsigned offset;
4870 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004871
Hanno Becker0271f962018-08-16 13:23:47 +01004872 /* Increment handshake sequence number */
4873 hs->in_msg_seq++;
4874
4875 /*
4876 * Clear up handshake buffering and reassembly structure.
4877 */
4878
4879 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004880 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004881
4882 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004883 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4884 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004885 offset++, hs_buf++ )
4886 {
4887 *hs_buf = *(hs_buf + 1);
4888 }
4889
4890 /* Create a fresh last entry */
4891 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004892 }
4893#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004894}
4895
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004896/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004897 * DTLS anti-replay: RFC 6347 4.1.2.6
4898 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004899 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4900 * Bit n is set iff record number in_window_top - n has been seen.
4901 *
4902 * Usually, in_window_top is the last record number seen and the lsb of
4903 * in_window is set. The only exception is the initial state (record number 0
4904 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4907static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004908{
4909 ssl->in_window_top = 0;
4910 ssl->in_window = 0;
4911}
4912
4913static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4914{
4915 return( ( (uint64_t) buf[0] << 40 ) |
4916 ( (uint64_t) buf[1] << 32 ) |
4917 ( (uint64_t) buf[2] << 24 ) |
4918 ( (uint64_t) buf[3] << 16 ) |
4919 ( (uint64_t) buf[4] << 8 ) |
4920 ( (uint64_t) buf[5] ) );
4921}
4922
4923/*
4924 * Return 0 if sequence number is acceptable, -1 otherwise
4925 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004926int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004927{
4928 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4929 uint64_t bit;
4930
Hanno Becker7f376f42019-06-12 16:20:48 +01004931 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4932 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4933 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004934 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004935 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004936
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004937 if( rec_seqnum > ssl->in_window_top )
4938 return( 0 );
4939
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004940 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004941
4942 if( bit >= 64 )
4943 return( -1 );
4944
4945 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4946 return( -1 );
4947
4948 return( 0 );
4949}
4950
4951/*
4952 * Update replay window on new validated record
4953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004955{
4956 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4957
Hanno Becker7f376f42019-06-12 16:20:48 +01004958 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4959 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4960 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004961 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004962 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004963
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004964 if( rec_seqnum > ssl->in_window_top )
4965 {
4966 /* Update window_top and the contents of the window */
4967 uint64_t shift = rec_seqnum - ssl->in_window_top;
4968
4969 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004970 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004971 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004972 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004973 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004974 ssl->in_window |= 1;
4975 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004976
4977 ssl->in_window_top = rec_seqnum;
4978 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004979 else
4980 {
4981 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004982 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004983
4984 if( bit < 64 ) /* Always true, but be extra sure */
4985 ssl->in_window |= (uint64_t) 1 << bit;
4986 }
4987}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004989
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004990#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004991/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004992static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4993
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004994/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004995 * Without any SSL context, check if a datagram looks like a ClientHello with
4996 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004997 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004998 *
4999 * - if cookie is valid, return 0
5000 * - if ClientHello looks superficially valid but cookie is not,
5001 * fill obuf and set olen, then
5002 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5003 * - otherwise return a specific error code
5004 */
5005static int ssl_check_dtls_clihlo_cookie(
5006 mbedtls_ssl_cookie_write_t *f_cookie_write,
5007 mbedtls_ssl_cookie_check_t *f_cookie_check,
5008 void *p_cookie,
5009 const unsigned char *cli_id, size_t cli_id_len,
5010 const unsigned char *in, size_t in_len,
5011 unsigned char *obuf, size_t buf_len, size_t *olen )
5012{
5013 size_t sid_len, cookie_len;
5014 unsigned char *p;
5015
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005016 /*
5017 * Structure of ClientHello with record and handshake headers,
5018 * and expected values. We don't need to check a lot, more checks will be
5019 * done when actually parsing the ClientHello - skipping those checks
5020 * avoids code duplication and does not make cookie forging any easier.
5021 *
5022 * 0-0 ContentType type; copied, must be handshake
5023 * 1-2 ProtocolVersion version; copied
5024 * 3-4 uint16 epoch; copied, must be 0
5025 * 5-10 uint48 sequence_number; copied
5026 * 11-12 uint16 length; (ignored)
5027 *
5028 * 13-13 HandshakeType msg_type; (ignored)
5029 * 14-16 uint24 length; (ignored)
5030 * 17-18 uint16 message_seq; copied
5031 * 19-21 uint24 fragment_offset; copied, must be 0
5032 * 22-24 uint24 fragment_length; (ignored)
5033 *
5034 * 25-26 ProtocolVersion client_version; (ignored)
5035 * 27-58 Random random; (ignored)
5036 * 59-xx SessionID session_id; 1 byte len + sid_len content
5037 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5038 * ...
5039 *
5040 * Minimum length is 61 bytes.
5041 */
5042 if( in_len < 61 ||
5043 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5044 in[3] != 0 || in[4] != 0 ||
5045 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5046 {
5047 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5048 }
5049
5050 sid_len = in[59];
5051 if( sid_len > in_len - 61 )
5052 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5053
5054 cookie_len = in[60 + sid_len];
5055 if( cookie_len > in_len - 60 )
5056 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5057
5058 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5059 cli_id, cli_id_len ) == 0 )
5060 {
5061 /* Valid cookie */
5062 return( 0 );
5063 }
5064
5065 /*
5066 * If we get here, we've got an invalid cookie, let's prepare HVR.
5067 *
5068 * 0-0 ContentType type; copied
5069 * 1-2 ProtocolVersion version; copied
5070 * 3-4 uint16 epoch; copied
5071 * 5-10 uint48 sequence_number; copied
5072 * 11-12 uint16 length; olen - 13
5073 *
5074 * 13-13 HandshakeType msg_type; hello_verify_request
5075 * 14-16 uint24 length; olen - 25
5076 * 17-18 uint16 message_seq; copied
5077 * 19-21 uint24 fragment_offset; copied
5078 * 22-24 uint24 fragment_length; olen - 25
5079 *
5080 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5081 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5082 *
5083 * Minimum length is 28.
5084 */
5085 if( buf_len < 28 )
5086 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5087
5088 /* Copy most fields and adapt others */
5089 memcpy( obuf, in, 25 );
5090 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5091 obuf[25] = 0xfe;
5092 obuf[26] = 0xff;
5093
5094 /* Generate and write actual cookie */
5095 p = obuf + 28;
5096 if( f_cookie_write( p_cookie,
5097 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5098 {
5099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5100 }
5101
5102 *olen = p - obuf;
5103
5104 /* Go back and fill length fields */
5105 obuf[27] = (unsigned char)( *olen - 28 );
5106
5107 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5108 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5109 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5110
5111 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5112 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5113
5114 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5115}
5116
5117/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005118 * Handle possible client reconnect with the same UDP quadruplet
5119 * (RFC 6347 Section 4.2.8).
5120 *
5121 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5122 * that looks like a ClientHello.
5123 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005124 * - if the input looks like a ClientHello without cookies,
5125 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005126 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005127 * - if the input looks like a ClientHello with a valid cookie,
5128 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005129 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005130 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005131 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005132 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005133 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5134 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005135 */
5136static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5137{
5138 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005139 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005140
Hanno Becker87b56262019-07-10 14:37:41 +01005141 if( ssl->conf->f_cookie_write == NULL ||
5142 ssl->conf->f_cookie_check == NULL )
5143 {
5144 /* If we can't use cookies to verify reachability of the peer,
5145 * drop the record. */
5146 return( 0 );
5147 }
5148
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005149 ret = ssl_check_dtls_clihlo_cookie(
5150 ssl->conf->f_cookie_write,
5151 ssl->conf->f_cookie_check,
5152 ssl->conf->p_cookie,
5153 ssl->cli_id, ssl->cli_id_len,
5154 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005155 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005156
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005157 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5158
5159 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005160 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005161 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005162 * If the error is permanent we'll catch it later,
5163 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005164 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005165 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005166 }
5167
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005168 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005169 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005170 /* Got a valid cookie, partially reset context */
5171 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5172 {
5173 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5174 return( ret );
5175 }
5176
5177 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005178 }
5179
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005180 return( ret );
5181}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005182#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005183
Hanno Becker46483f12019-05-03 13:25:54 +01005184static int ssl_check_record_type( uint8_t record_type )
5185{
5186 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5187 record_type != MBEDTLS_SSL_MSG_ALERT &&
5188 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5189 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5190 {
5191 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5192 }
5193
5194 return( 0 );
5195}
5196
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005197/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005198 * ContentType type;
5199 * ProtocolVersion version;
5200 * uint16 epoch; // DTLS only
5201 * uint48 sequence_number; // DTLS only
5202 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005203 *
5204 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005205 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005206 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5207 *
5208 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005209 * 1. proceed with the record if this function returns 0
5210 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5211 * 3. return CLIENT_RECONNECT if this function return that value
5212 * 4. drop the whole datagram if this function returns anything else.
5213 * Point 2 is needed when the peer is resending, and we have already received
5214 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005215 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005216static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005217 unsigned char *buf,
5218 size_t len,
5219 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005220{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005221 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005222
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005223 size_t const rec_hdr_type_offset = 0;
5224 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005225
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005226 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5227 rec_hdr_type_len;
5228 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005229
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005230 size_t const rec_hdr_ctr_len = 8;
5231#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005232 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005233 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5234 rec_hdr_version_len;
5235
Hanno Beckera5a2b082019-05-15 14:03:01 +01005236#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005237 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5238 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005239 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005240#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5241#endif /* MBEDTLS_SSL_PROTO_DTLS */
5242
5243 size_t rec_hdr_len_offset; /* To be determined */
5244 size_t const rec_hdr_len_len = 2;
5245
5246 /*
5247 * Check minimum lengths for record header.
5248 */
5249
5250#if defined(MBEDTLS_SSL_PROTO_DTLS)
5251 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5252 {
5253 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5254 }
5255 MBEDTLS_SSL_TRANSPORT_ELSE
5256#endif /* MBEDTLS_SSL_PROTO_DTLS */
5257#if defined(MBEDTLS_SSL_PROTO_TLS)
5258 {
5259 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5260 }
5261#endif /* MBEDTLS_SSL_PROTO_DTLS */
5262
5263 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5264 {
5265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5266 (unsigned) len,
5267 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5268 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5269 }
5270
5271 /*
5272 * Parse and validate record content type
5273 */
5274
5275 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005276
5277 /* Check record content type */
5278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5279 rec->cid_len = 0;
5280
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005281 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005282 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5283 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005284 {
5285 /* Shift pointers to account for record header including CID
5286 * struct {
5287 * ContentType special_type = tls12_cid;
5288 * ProtocolVersion version;
5289 * uint16 epoch;
5290 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005291 * opaque cid[cid_length]; // Additional field compared to
5292 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005293 * uint16 length;
5294 * opaque enc_content[DTLSCiphertext.length];
5295 * } DTLSCiphertext;
5296 */
5297
5298 /* So far, we only support static CID lengths
5299 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005300 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5301 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005302
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005303 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005304 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5306 (unsigned) len,
5307 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005308 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005309 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005310
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005311 /* configured CID len is guaranteed at most 255, see
5312 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5313 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005314 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005315 }
5316 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005317#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005318 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005319 if( ssl_check_record_type( rec->type ) )
5320 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5322 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005323 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5324 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005325 }
5326
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005327 /*
5328 * Parse and validate record version
5329 */
5330
Hanno Becker8061c6e2019-07-26 08:07:03 +01005331 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5332 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5334 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005335 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005336
Hanno Becker2881d802019-05-22 14:44:53 +01005337 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5340 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005341 }
5342
Hanno Beckere965bd32019-06-12 14:04:34 +01005343 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5346 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005347 }
5348
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005349 /*
5350 * Parse/Copy record sequence number.
5351 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005352
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005353#if defined(MBEDTLS_SSL_PROTO_DTLS)
5354 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5355 {
5356 /* Copy explicit record sequence number from input buffer. */
5357 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5358 rec_hdr_ctr_len );
5359 }
5360 MBEDTLS_SSL_TRANSPORT_ELSE
5361#endif /* MBEDTLS_SSL_PROTO_DTLS */
5362#if defined(MBEDTLS_SSL_PROTO_TLS)
5363 {
5364 /* Copy implicit record sequence number from SSL context structure. */
5365 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5366 }
5367#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005368
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005369 /*
5370 * Parse record length.
5371 */
5372
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005373 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005374 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5375 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005376 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5377
Hanno Becker8b09b732019-05-08 12:03:28 +01005378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005379 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005380 rec->type,
5381 major_ver, minor_ver, rec->data_len ) );
5382
5383 rec->buf = buf;
5384 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005385
Hanno Beckerec014082019-07-26 08:20:27 +01005386 if( rec->data_len == 0 )
5387 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5388
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005389 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005390 * DTLS-related tests.
5391 * Check epoch before checking length constraint because
5392 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5393 * message gets duplicated before the corresponding Finished message,
5394 * the second ChangeCipherSpec should be discarded because it belongs
5395 * to an old epoch, but not because its length is shorter than
5396 * the minimum record length for packets using the new record transform.
5397 * Note that these two kinds of failures are handled differently,
5398 * as an unexpected record is silently skipped but an invalid
5399 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005400 */
5401#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005402 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005403 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005404 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005405
Hanno Beckere0452772019-07-10 17:12:07 +01005406 /* Check that the datagram is large enough to contain a record
5407 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005408 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005409 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5411 (unsigned) len,
5412 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005413 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5414 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005415
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005416 /* Records from other, non-matching epochs are silently discarded.
5417 * (The case of same-port Client reconnects must be considered in
5418 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005419 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005420 {
5421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5422 "expected %d, received %d",
5423 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005424
5425 /* Records from the next epoch are considered for buffering
5426 * (concretely: early Finished messages). */
5427 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5428 {
5429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5430 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5431 }
5432
Hanno Becker87b56262019-07-10 14:37:41 +01005433 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005434 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005435#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005436 /* For records from the correct epoch, check whether their
5437 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005438 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005439 {
5440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5441 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5442 }
5443#endif
5444 }
5445#endif /* MBEDTLS_SSL_PROTO_DTLS */
5446
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005447 return( 0 );
5448}
Paul Bakker5121ce52009-01-03 21:22:43 +00005449
Hanno Becker87b56262019-07-10 14:37:41 +01005450
5451#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5452static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5453{
5454 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5455
5456 /*
5457 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5458 * access the first byte of record content (handshake type), as we
5459 * have an active transform (possibly iv_len != 0), so use the
5460 * fact that the record header len is 13 instead.
5461 */
5462 if( rec_epoch == 0 &&
5463 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5464 MBEDTLS_SSL_IS_SERVER &&
5465 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5466 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5467 ssl->in_left > 13 &&
5468 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5469 {
5470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5471 "from the same port" ) );
5472 return( ssl_handle_possible_reconnect( ssl ) );
5473 }
5474
5475 return( 0 );
5476}
5477#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5478
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005479/*
5480 * If applicable, decrypt (and decompress) record content
5481 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005482static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5483 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005484{
5485 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005488 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5491 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005495 ret = mbedtls_ssl_hw_record_read( ssl );
5496 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5499 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005500 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005501
5502 if( ret == 0 )
5503 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005504 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005506 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005507 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005508 unsigned char const old_msg_type = rec->type;
5509
Hanno Becker611a83b2018-01-03 14:27:32 +00005510 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005511 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005513 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005514
Hanno Beckera5a2b082019-05-15 14:03:01 +01005515#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005516 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005517 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5518 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005519 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005521 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005522 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005523#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005524
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 return( ret );
5526 }
5527
Hanno Becker106f3da2019-07-12 09:35:58 +01005528 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005529 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005530 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005531 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005533
Paul Bakker5121ce52009-01-03 21:22:43 +00005534 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005535 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005536
Hanno Beckera5a2b082019-05-15 14:03:01 +01005537#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005538 /* We have already checked the record content type
5539 * in ssl_parse_record_header(), failing or silently
5540 * dropping the record in the case of an unknown type.
5541 *
5542 * Since with the use of CIDs, the record content type
5543 * might change during decryption, re-check the record
5544 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005545 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005546 {
5547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5548 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5549 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005550#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005551
Hanno Becker106f3da2019-07-12 09:35:58 +01005552 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005553 {
5554#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005555 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005556 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005557 {
5558 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5560 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5561 }
5562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5563
5564 ssl->nb_zero++;
5565
5566 /*
5567 * Three or more empty messages may be a DoS attack
5568 * (excessive CPU consumption).
5569 */
5570 if( ssl->nb_zero > 3 )
5571 {
5572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005573 "messages, possible DoS attack" ) );
5574 /* Treat the records as if they were not properly authenticated,
5575 * thereby failing the connection if we see more than allowed
5576 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005577 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5578 }
5579 }
5580 else
5581 ssl->nb_zero = 0;
5582
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005583 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5584#if defined(MBEDTLS_SSL_PROTO_TLS)
5585 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005586 {
5587 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005588 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005589 if( ++ssl->in_ctr[i - 1] != 0 )
5590 break;
5591
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005592 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005593 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005594 {
5595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5596 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5597 }
5598 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005599#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005600
Paul Bakker5121ce52009-01-03 21:22:43 +00005601 }
5602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005604 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005606 {
5607 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005610 return( ret );
5611 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005612 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005615#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005616 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005619 }
5620#endif
5621
Hanno Beckerf0242852019-07-09 17:30:02 +01005622 /* Check actual (decrypted) record content length against
5623 * configured maximum. */
5624 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5625 {
5626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5628 }
5629
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005630 return( 0 );
5631}
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005634
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005635/*
5636 * Read a record.
5637 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005638 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5639 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5640 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005641 */
Hanno Becker1097b342018-08-15 14:09:41 +01005642
5643/* Helper functions for mbedtls_ssl_read_record(). */
5644static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005645static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5646static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005647
Hanno Becker327c93b2018-08-15 13:56:18 +01005648int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005649 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005650{
5651 int ret;
5652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005654
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005655 if( ssl->keep_current_message == 0 )
5656 {
5657 do {
Simon Butcher99000142016-10-13 17:21:01 +01005658
Hanno Becker26994592018-08-15 14:14:59 +01005659 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005660 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005661 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005662
Hanno Beckere74d5562018-08-15 14:26:08 +01005663 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005664 {
Hanno Becker40f50842018-08-15 14:48:01 +01005665#if defined(MBEDTLS_SSL_PROTO_DTLS)
5666 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005667
Hanno Becker40f50842018-08-15 14:48:01 +01005668 /* We only check for buffered messages if the
5669 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005670 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005671 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005672 {
Hanno Becker40f50842018-08-15 14:48:01 +01005673 if( ssl_load_buffered_message( ssl ) == 0 )
5674 have_buffered = 1;
5675 }
5676
5677 if( have_buffered == 0 )
5678#endif /* MBEDTLS_SSL_PROTO_DTLS */
5679 {
5680 ret = ssl_get_next_record( ssl );
5681 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5682 continue;
5683
5684 if( ret != 0 )
5685 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005686 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005687 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005688 return( ret );
5689 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005690 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005691 }
5692
5693 ret = mbedtls_ssl_handle_message_type( ssl );
5694
Hanno Becker40f50842018-08-15 14:48:01 +01005695#if defined(MBEDTLS_SSL_PROTO_DTLS)
5696 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5697 {
5698 /* Buffer future message */
5699 ret = ssl_buffer_message( ssl );
5700 if( ret != 0 )
5701 return( ret );
5702
5703 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5704 }
5705#endif /* MBEDTLS_SSL_PROTO_DTLS */
5706
Hanno Becker90333da2017-10-10 11:27:13 +01005707 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5708 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005709
5710 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005711 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005712 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005713 return( ret );
5714 }
5715
Hanno Becker327c93b2018-08-15 13:56:18 +01005716 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005717 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005718 {
5719 mbedtls_ssl_update_handshake_status( ssl );
5720 }
Simon Butcher99000142016-10-13 17:21:01 +01005721 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005722 else
Simon Butcher99000142016-10-13 17:21:01 +01005723 {
Hanno Becker02f59072018-08-15 14:00:24 +01005724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005725 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005726 }
5727
5728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5729
5730 return( 0 );
5731}
5732
Hanno Becker40f50842018-08-15 14:48:01 +01005733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005734static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005735{
Hanno Becker40f50842018-08-15 14:48:01 +01005736 if( ssl->in_left > ssl->next_record_offset )
5737 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005738
Hanno Becker40f50842018-08-15 14:48:01 +01005739 return( 0 );
5740}
5741
5742static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5743{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005744 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005745 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005746 int ret = 0;
5747
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005748 if( hs == NULL )
5749 return( -1 );
5750
Hanno Beckere00ae372018-08-20 09:39:42 +01005751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5752
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005753 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5754 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5755 {
5756 /* Check if we have seen a ChangeCipherSpec before.
5757 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005758 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005759 {
5760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5761 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005762 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005763 }
5764
Hanno Becker39b8bc92018-08-28 17:17:13 +01005765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005766 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5767 ssl->in_msglen = 1;
5768 ssl->in_msg[0] = 1;
5769
5770 /* As long as they are equal, the exact value doesn't matter. */
5771 ssl->in_left = 0;
5772 ssl->next_record_offset = 0;
5773
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005774 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005775 goto exit;
5776 }
Hanno Becker37f95322018-08-16 13:55:32 +01005777
Hanno Beckerb8f50142018-08-28 10:01:34 +01005778#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005779 /* Debug only */
5780 {
5781 unsigned offset;
5782 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5783 {
5784 hs_buf = &hs->buffering.hs[offset];
5785 if( hs_buf->is_valid == 1 )
5786 {
5787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5788 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005789 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005790 }
5791 }
5792 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005793#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005794
5795 /* Check if we have buffered and/or fully reassembled the
5796 * next handshake message. */
5797 hs_buf = &hs->buffering.hs[0];
5798 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5799 {
5800 /* Synthesize a record containing the buffered HS message. */
5801 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5802 ( hs_buf->data[2] << 8 ) |
5803 hs_buf->data[3];
5804
5805 /* Double-check that we haven't accidentally buffered
5806 * a message that doesn't fit into the input buffer. */
5807 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5808 {
5809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5811 }
5812
5813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5814 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5815 hs_buf->data, msg_len + 12 );
5816
5817 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5818 ssl->in_hslen = msg_len + 12;
5819 ssl->in_msglen = msg_len + 12;
5820 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5821
5822 ret = 0;
5823 goto exit;
5824 }
5825 else
5826 {
5827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5828 hs->in_msg_seq ) );
5829 }
5830
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005831 ret = -1;
5832
5833exit:
5834
5835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5836 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005837}
5838
Hanno Beckera02b0b42018-08-21 17:20:27 +01005839static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5840 size_t desired )
5841{
5842 int offset;
5843 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5845 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005846
Hanno Becker01315ea2018-08-21 17:22:17 +01005847 /* Get rid of future records epoch first, if such exist. */
5848 ssl_free_buffered_record( ssl );
5849
5850 /* Check if we have enough space available now. */
5851 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5852 hs->buffering.total_bytes_buffered ) )
5853 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005855 return( 0 );
5856 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005857
Hanno Becker4f432ad2018-08-28 10:02:32 +01005858 /* We don't have enough space to buffer the next expected handshake
5859 * message. Remove buffers used for future messages to gain space,
5860 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005861 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5862 offset >= 0; offset-- )
5863 {
5864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5865 offset ) );
5866
Hanno Beckerb309b922018-08-23 13:18:05 +01005867 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005868
5869 /* Check if we have enough space available now. */
5870 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5871 hs->buffering.total_bytes_buffered ) )
5872 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005874 return( 0 );
5875 }
5876 }
5877
5878 return( -1 );
5879}
5880
Hanno Becker40f50842018-08-15 14:48:01 +01005881static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5882{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005883 int ret = 0;
5884 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5885
5886 if( hs == NULL )
5887 return( 0 );
5888
5889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5890
5891 switch( ssl->in_msgtype )
5892 {
5893 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005895
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005896 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005897 break;
5898
5899 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005900 {
5901 unsigned recv_msg_seq_offset;
5902 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5903 mbedtls_ssl_hs_buffer *hs_buf;
5904 size_t msg_len = ssl->in_hslen - 12;
5905
5906 /* We should never receive an old handshake
5907 * message - double-check nonetheless. */
5908 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5909 {
5910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5911 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5912 }
5913
5914 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5915 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5916 {
5917 /* Silently ignore -- message too far in the future */
5918 MBEDTLS_SSL_DEBUG_MSG( 2,
5919 ( "Ignore future HS message with sequence number %u, "
5920 "buffering window %u - %u",
5921 recv_msg_seq, ssl->handshake->in_msg_seq,
5922 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5923
5924 goto exit;
5925 }
5926
5927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5928 recv_msg_seq, recv_msg_seq_offset ) );
5929
5930 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5931
5932 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005933 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005934 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005935 size_t reassembly_buf_sz;
5936
Hanno Becker37f95322018-08-16 13:55:32 +01005937 hs_buf->is_fragmented =
5938 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5939
5940 /* We copy the message back into the input buffer
5941 * after reassembly, so check that it's not too large.
5942 * This is an implementation-specific limitation
5943 * and not one from the standard, hence it is not
5944 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005945 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005946 {
5947 /* Ignore message */
5948 goto exit;
5949 }
5950
Hanno Beckere0b150f2018-08-21 15:51:03 +01005951 /* Check if we have enough space to buffer the message. */
5952 if( hs->buffering.total_bytes_buffered >
5953 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5954 {
5955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5957 }
5958
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005959 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5960 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005961
5962 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5963 hs->buffering.total_bytes_buffered ) )
5964 {
5965 if( recv_msg_seq_offset > 0 )
5966 {
5967 /* If we can't buffer a future message because
5968 * of space limitations -- ignore. */
5969 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",
5970 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5971 (unsigned) hs->buffering.total_bytes_buffered ) );
5972 goto exit;
5973 }
Hanno Beckere1801392018-08-21 16:51:05 +01005974 else
5975 {
5976 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",
5977 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5978 (unsigned) hs->buffering.total_bytes_buffered ) );
5979 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005980
Hanno Beckera02b0b42018-08-21 17:20:27 +01005981 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005982 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005983 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",
5984 (unsigned) msg_len,
5985 (unsigned) reassembly_buf_sz,
5986 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005987 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005988 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5989 goto exit;
5990 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005991 }
5992
5993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5994 msg_len ) );
5995
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005996 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5997 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005998 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005999 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006000 goto exit;
6001 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006002 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006003
6004 /* Prepare final header: copy msg_type, length and message_seq,
6005 * then add standardised fragment_offset and fragment_length */
6006 memcpy( hs_buf->data, ssl->in_msg, 6 );
6007 memset( hs_buf->data + 6, 0, 3 );
6008 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
6009
6010 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006011
6012 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006013 }
6014 else
6015 {
6016 /* Make sure msg_type and length are consistent */
6017 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
6018 {
6019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6020 /* Ignore */
6021 goto exit;
6022 }
6023 }
6024
Hanno Becker4422bbb2018-08-20 09:40:19 +01006025 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006026 {
6027 size_t frag_len, frag_off;
6028 unsigned char * const msg = hs_buf->data + 12;
6029
6030 /*
6031 * Check and copy current fragment
6032 */
6033
6034 /* Validation of header fields already done in
6035 * mbedtls_ssl_prepare_handshake_record(). */
6036 frag_off = ssl_get_hs_frag_off( ssl );
6037 frag_len = ssl_get_hs_frag_len( ssl );
6038
6039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6040 frag_off, frag_len ) );
6041 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6042
6043 if( hs_buf->is_fragmented )
6044 {
6045 unsigned char * const bitmask = msg + msg_len;
6046 ssl_bitmask_set( bitmask, frag_off, frag_len );
6047 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6048 msg_len ) == 0 );
6049 }
6050 else
6051 {
6052 hs_buf->is_complete = 1;
6053 }
6054
6055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6056 hs_buf->is_complete ? "" : "not yet " ) );
6057 }
6058
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006059 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006060 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006061
6062 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006063 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006064 break;
6065 }
6066
6067exit:
6068
6069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6070 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006071}
6072#endif /* MBEDTLS_SSL_PROTO_DTLS */
6073
Hanno Becker1097b342018-08-15 14:09:41 +01006074static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006075{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006076 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006077 * Consume last content-layer message and potentially
6078 * update in_msglen which keeps track of the contents'
6079 * consumption state.
6080 *
6081 * (1) Handshake messages:
6082 * Remove last handshake message, move content
6083 * and adapt in_msglen.
6084 *
6085 * (2) Alert messages:
6086 * Consume whole record content, in_msglen = 0.
6087 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006088 * (3) Change cipher spec:
6089 * Consume whole record content, in_msglen = 0.
6090 *
6091 * (4) Application data:
6092 * Don't do anything - the record layer provides
6093 * the application data as a stream transport
6094 * and consumes through mbedtls_ssl_read only.
6095 *
6096 */
6097
6098 /* Case (1): Handshake messages */
6099 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006100 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006101 /* Hard assertion to be sure that no application data
6102 * is in flight, as corrupting ssl->in_msglen during
6103 * ssl->in_offt != NULL is fatal. */
6104 if( ssl->in_offt != NULL )
6105 {
6106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6108 }
6109
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006110 /*
6111 * Get next Handshake message in the current record
6112 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006113
Hanno Becker4a810fb2017-05-24 16:27:30 +01006114 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006115 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006116 * current handshake content: If DTLS handshake
6117 * fragmentation is used, that's the fragment
6118 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006119 * size here is faulty and should be changed at
6120 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006121 * (2) While it doesn't seem to cause problems, one
6122 * has to be very careful not to assume that in_hslen
6123 * is always <= in_msglen in a sensible communication.
6124 * Again, it's wrong for DTLS handshake fragmentation.
6125 * The following check is therefore mandatory, and
6126 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006127 * Additionally, ssl->in_hslen might be arbitrarily out of
6128 * bounds after handling a DTLS message with an unexpected
6129 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006130 */
6131 if( ssl->in_hslen < ssl->in_msglen )
6132 {
6133 ssl->in_msglen -= ssl->in_hslen;
6134 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6135 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006136
Hanno Becker4a810fb2017-05-24 16:27:30 +01006137 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6138 ssl->in_msg, ssl->in_msglen );
6139 }
6140 else
6141 {
6142 ssl->in_msglen = 0;
6143 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006144
Hanno Becker4a810fb2017-05-24 16:27:30 +01006145 ssl->in_hslen = 0;
6146 }
6147 /* Case (4): Application data */
6148 else if( ssl->in_offt != NULL )
6149 {
6150 return( 0 );
6151 }
6152 /* Everything else (CCS & Alerts) */
6153 else
6154 {
6155 ssl->in_msglen = 0;
6156 }
6157
Hanno Becker1097b342018-08-15 14:09:41 +01006158 return( 0 );
6159}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006160
Hanno Beckere74d5562018-08-15 14:26:08 +01006161static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6162{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006163 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006164 return( 1 );
6165
6166 return( 0 );
6167}
6168
Hanno Becker5f066e72018-08-16 14:56:31 +01006169#if defined(MBEDTLS_SSL_PROTO_DTLS)
6170
6171static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6172{
6173 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6174 if( hs == NULL )
6175 return;
6176
Hanno Becker01315ea2018-08-21 17:22:17 +01006177 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006178 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006179 hs->buffering.total_bytes_buffered -=
6180 hs->buffering.future_record.len;
6181
6182 mbedtls_free( hs->buffering.future_record.data );
6183 hs->buffering.future_record.data = NULL;
6184 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006185}
6186
6187static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6188{
6189 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6190 unsigned char * rec;
6191 size_t rec_len;
6192 unsigned rec_epoch;
6193
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006194 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006195 return( 0 );
6196
6197 if( hs == NULL )
6198 return( 0 );
6199
Hanno Becker5f066e72018-08-16 14:56:31 +01006200 rec = hs->buffering.future_record.data;
6201 rec_len = hs->buffering.future_record.len;
6202 rec_epoch = hs->buffering.future_record.epoch;
6203
6204 if( rec == NULL )
6205 return( 0 );
6206
Hanno Becker4cb782d2018-08-20 11:19:05 +01006207 /* Only consider loading future records if the
6208 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006209 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006210 return( 0 );
6211
Hanno Becker5f066e72018-08-16 14:56:31 +01006212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6213
6214 if( rec_epoch != ssl->in_epoch )
6215 {
6216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6217 goto exit;
6218 }
6219
6220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6221
6222 /* Double-check that the record is not too large */
6223 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6224 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6225 {
6226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6228 }
6229
6230 memcpy( ssl->in_hdr, rec, rec_len );
6231 ssl->in_left = rec_len;
6232 ssl->next_record_offset = 0;
6233
6234 ssl_free_buffered_record( ssl );
6235
6236exit:
6237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6238 return( 0 );
6239}
6240
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006241static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6242 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006243{
6244 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006245
6246 /* Don't buffer future records outside handshakes. */
6247 if( hs == NULL )
6248 return( 0 );
6249
6250 /* Only buffer handshake records (we are only interested
6251 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006252 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006253 return( 0 );
6254
6255 /* Don't buffer more than one future epoch record. */
6256 if( hs->buffering.future_record.data != NULL )
6257 return( 0 );
6258
Hanno Becker01315ea2018-08-21 17:22:17 +01006259 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006260 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006261 hs->buffering.total_bytes_buffered ) )
6262 {
6263 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 +01006264 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006265 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006266 return( 0 );
6267 }
6268
Hanno Becker5f066e72018-08-16 14:56:31 +01006269 /* Buffer record */
6270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6271 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006272 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006273
6274 /* ssl_parse_record_header() only considers records
6275 * of the next epoch as candidates for buffering. */
6276 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006277 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006278
6279 hs->buffering.future_record.data =
6280 mbedtls_calloc( 1, hs->buffering.future_record.len );
6281 if( hs->buffering.future_record.data == NULL )
6282 {
6283 /* If we run out of RAM trying to buffer a
6284 * record from the next epoch, just ignore. */
6285 return( 0 );
6286 }
6287
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006288 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006289
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006290 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006291 return( 0 );
6292}
6293
6294#endif /* MBEDTLS_SSL_PROTO_DTLS */
6295
Hanno Beckere74d5562018-08-15 14:26:08 +01006296static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006297{
6298 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006299 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006300
Hanno Becker5f066e72018-08-16 14:56:31 +01006301#if defined(MBEDTLS_SSL_PROTO_DTLS)
6302 /* We might have buffered a future record; if so,
6303 * and if the epoch matches now, load it.
6304 * On success, this call will set ssl->in_left to
6305 * the length of the buffered record, so that
6306 * the calls to ssl_fetch_input() below will
6307 * essentially be no-ops. */
6308 ret = ssl_load_buffered_record( ssl );
6309 if( ret != 0 )
6310 return( ret );
6311#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006312
Hanno Becker8b09b732019-05-08 12:03:28 +01006313 /* Ensure that we have enough space available for the default form
6314 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6315 * with no space for CIDs counted in). */
6316 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6317 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006320 return( ret );
6321 }
6322
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006323 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6324 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006327 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006328 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006329 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6330 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006331 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006332 if( ret != 0 )
6333 return( ret );
6334
6335 /* Fall through to handling of unexpected records */
6336 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6337 }
6338
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006339 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6340 {
Hanno Becker87b56262019-07-10 14:37:41 +01006341#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006342 /* Reset in pointers to default state for TLS/DTLS records,
6343 * assuming no CID and no offset between record content and
6344 * record plaintext. */
6345 ssl_update_in_pointers( ssl );
6346
Hanno Becker69412452019-07-12 08:33:49 +01006347 /* Setup internal message pointers from record structure. */
6348 ssl->in_msgtype = rec.type;
6349#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6350 ssl->in_len = ssl->in_cid + rec.cid_len;
6351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006352 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006353 ssl->in_msglen = rec.data_len;
6354
Hanno Becker87b56262019-07-10 14:37:41 +01006355 ret = ssl_check_client_reconnect( ssl );
6356 if( ret != 0 )
6357 return( ret );
6358#endif
6359
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006360 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006361 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006362
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6364 "(header)" ) );
6365 }
6366 else
6367 {
6368 /* Skip invalid record and the rest of the datagram */
6369 ssl->next_record_offset = 0;
6370 ssl->in_left = 0;
6371
6372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6373 "(header)" ) );
6374 }
6375
6376 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006377 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006378 }
Hanno Becker87b56262019-07-10 14:37:41 +01006379 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006380#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006381 {
6382 return( ret );
6383 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006384 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006387 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006388 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006389 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006390 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006391 if( ssl->next_record_offset < ssl->in_left )
6392 {
6393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6394 }
6395 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006396 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006397#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006398#if defined(MBEDTLS_SSL_PROTO_TLS)
6399 {
Hanno Beckere0452772019-07-10 17:12:07 +01006400 /*
6401 * Fetch record contents from underlying transport.
6402 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006403 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006404 if( ret != 0 )
6405 {
6406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6407 return( ret );
6408 }
6409
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006410 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006411 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006412#endif /* MBEDTLS_SSL_PROTO_TLS */
6413
6414 /*
6415 * Decrypt record contents.
6416 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006417
Hanno Beckera89610a2019-07-11 13:07:45 +01006418 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006421 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006422 {
6423 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006424 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006425 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006426 /* Except when waiting for Finished as a bad mac here
6427 * probably means something went wrong in the handshake
6428 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6429 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6430 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6431 {
6432#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6433 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6434 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006435 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006436 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6437 }
6438#endif
6439 return( ret );
6440 }
6441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006443 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6444 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6447 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006448 }
6449#endif
6450
Hanno Becker4a810fb2017-05-24 16:27:30 +01006451 /* As above, invalid records cause
6452 * dismissal of the whole datagram. */
6453
6454 ssl->next_record_offset = 0;
6455 ssl->in_left = 0;
6456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006458 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006459 }
6460
6461 return( ret );
6462 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006463 MBEDTLS_SSL_TRANSPORT_ELSE
6464#endif /* MBEDTLS_SSL_PROTO_DTLS */
6465#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006466 {
6467 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6469 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006470 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006471 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006473 }
6474#endif
6475 return( ret );
6476 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006477#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006478 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006479
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006480
6481 /* Reset in pointers to default state for TLS/DTLS records,
6482 * assuming no CID and no offset between record content and
6483 * record plaintext. */
6484 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006485#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6486 ssl->in_len = ssl->in_cid + rec.cid_len;
6487#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006488 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006489
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006490 /* The record content type may change during decryption,
6491 * so re-read it. */
6492 ssl->in_msgtype = rec.type;
6493 /* Also update the input buffer, because unfortunately
6494 * the server-side ssl_parse_client_hello() reparses the
6495 * record header when receiving a ClientHello initiating
6496 * a renegotiation. */
6497 ssl->in_hdr[0] = rec.type;
6498 ssl->in_msg = rec.buf + rec.data_offset;
6499 ssl->in_msglen = rec.data_len;
6500 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6501 ssl->in_len[1] = (unsigned char)( rec.data_len );
6502
Simon Butcher99000142016-10-13 17:21:01 +01006503 return( 0 );
6504}
6505
6506int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6507{
6508 int ret;
6509
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006510 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006511 * Handle particular types of records
6512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 {
Simon Butcher99000142016-10-13 17:21:01 +01006515 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6516 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006517 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006519 }
6520
Hanno Beckere678eaa2018-08-21 14:57:46 +01006521 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006522 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006523 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006524 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6526 ssl->in_msglen ) );
6527 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006528 }
6529
Hanno Beckere678eaa2018-08-21 14:57:46 +01006530 if( ssl->in_msg[0] != 1 )
6531 {
6532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6533 ssl->in_msg[0] ) );
6534 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6535 }
6536
6537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006538 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006539 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6540 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6541 {
6542 if( ssl->handshake == NULL )
6543 {
6544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6545 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6546 }
6547
6548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6549 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6550 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006551#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006552 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006555 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006556 if( ssl->in_msglen != 2 )
6557 {
6558 /* Note: Standard allows for more than one 2 byte alert
6559 to be packed in a single message, but Mbed TLS doesn't
6560 currently support this. */
6561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6562 ssl->in_msglen ) );
6563 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6564 }
6565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006567 ssl->in_msg[0], ssl->in_msg[1] ) );
6568
6569 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006570 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006575 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006576 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 }
6578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6580 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6583 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006585
6586#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6587 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6588 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6589 {
Hanno Becker90333da2017-10-10 11:27:13 +01006590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006591 /* Will be handled when trying to parse ServerHello */
6592 return( 0 );
6593 }
6594#endif
6595
6596#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006597 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006598 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6599 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006600 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6601 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6602 {
6603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6604 /* Will be handled in mbedtls_ssl_parse_certificate() */
6605 return( 0 );
6606 }
6607#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6608
6609 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006610 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006611 }
6612
Hanno Beckerc76c6192017-06-06 10:03:17 +01006613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006614 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006615 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006616 /* Drop unexpected ApplicationData records,
6617 * except at the beginning of renegotiations */
6618 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6619 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6620#if defined(MBEDTLS_SSL_RENEGOTIATION)
6621 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6622 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006623#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006624 )
6625 {
6626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6627 return( MBEDTLS_ERR_SSL_NON_FATAL );
6628 }
6629
6630 if( ssl->handshake != NULL &&
6631 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6632 {
6633 ssl_handshake_wrapup_free_hs_transform( ssl );
6634 }
6635 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006636#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006637
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 return( 0 );
6639}
6640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006642{
6643 int ret;
6644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006645 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6646 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006648 {
6649 return( ret );
6650 }
6651
6652 return( 0 );
6653}
6654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006656 unsigned char level,
6657 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006658{
6659 int ret;
6660
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006661 if( ssl == NULL || ssl->conf == NULL )
6662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006668 ssl->out_msglen = 2;
6669 ssl->out_msg[0] = level;
6670 ssl->out_msg[1] = message;
6671
Hanno Becker67bc7c32018-08-06 11:33:50 +01006672 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006675 return( ret );
6676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006678
6679 return( 0 );
6680}
6681
Hanno Becker17572472019-02-08 07:19:04 +00006682#if defined(MBEDTLS_X509_CRT_PARSE_C)
6683static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6684{
6685#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6686 if( session->peer_cert != NULL )
6687 {
6688 mbedtls_x509_crt_free( session->peer_cert );
6689 mbedtls_free( session->peer_cert );
6690 session->peer_cert = NULL;
6691 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006692#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006693 if( session->peer_cert_digest != NULL )
6694 {
6695 /* Zeroization is not necessary. */
6696 mbedtls_free( session->peer_cert_digest );
6697 session->peer_cert_digest = NULL;
6698 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6699 session->peer_cert_digest_len = 0;
6700 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006701#else
6702 ((void) session);
6703#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006704}
6705#endif /* MBEDTLS_X509_CRT_PARSE_C */
6706
Paul Bakker5121ce52009-01-03 21:22:43 +00006707/*
6708 * Handshake functions
6709 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006710#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006711/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006713{
Hanno Beckerdf645962019-06-26 13:02:22 +01006714 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6715 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006718
Hanno Becker5097cba2019-02-05 13:36:46 +00006719 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006722 ssl->state++;
6723 return( 0 );
6724 }
6725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006728}
6729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006731{
Hanno Beckerdf645962019-06-26 13:02:22 +01006732 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6733 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006736
Hanno Becker5097cba2019-02-05 13:36:46 +00006737 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006740 ssl->state++;
6741 return( 0 );
6742 }
6743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006746}
Gilles Peskinef9828522017-05-03 12:28:43 +02006747
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006748#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006749/* Some certificate support -> implement write and parse */
6750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006752{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006754 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006756 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6757 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006760
Hanno Becker5097cba2019-02-05 13:36:46 +00006761 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006764 ssl->state++;
6765 return( 0 );
6766 }
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006769 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6770 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006771 {
6772 if( ssl->client_auth == 0 )
6773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 ssl->state++;
6776 return( 0 );
6777 }
6778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 /*
6781 * If using SSLv3 and got no cert, send an Alert message
6782 * (otherwise an empty Certificate message will be sent).
6783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006785 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006786 {
6787 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6789 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6790 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 goto write_msg;
6794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797#endif /* MBEDTLS_SSL_CLI_C */
6798#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006799 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6804 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 }
6806 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006807#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006810
6811 /*
6812 * 0 . 0 handshake type
6813 * 1 . 3 handshake length
6814 * 4 . 6 length of all certs
6815 * 7 . 9 length of cert. 1
6816 * 10 . n-1 peer certificate
6817 * n . n+2 length of cert. 2
6818 * n+3 . ... upper level cert, etc.
6819 */
6820 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Paul Bakker29087132010-03-21 21:03:34 +00006823 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006824 {
6825 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006826 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006829 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006830 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006831 }
6832
6833 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6834 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6835 ssl->out_msg[i + 2] = (unsigned char)( n );
6836
6837 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6838 i += n; crt = crt->next;
6839 }
6840
6841 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6842 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6843 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6844
6845 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6847 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006848
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006849#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006850write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006851#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006852
6853 ssl->state++;
6854
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006855 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006856 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858 return( ret );
6859 }
6860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
Paul Bakkered27a042013-04-18 22:46:23 +02006863 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006864}
6865
Hanno Becker285ff0c2019-01-31 07:44:03 +00006866#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006867
6868#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006869static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6870 unsigned char *crt_buf,
6871 size_t crt_buf_len )
6872{
6873 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6874
6875 if( peer_crt == NULL )
6876 return( -1 );
6877
6878 if( peer_crt->raw.len != crt_buf_len )
6879 return( -1 );
6880
Hanno Becker68b856d2019-02-08 14:00:04 +00006881 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006882}
Hanno Becker5882dd02019-06-06 16:25:57 +01006883#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006884static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6885 unsigned char *crt_buf,
6886 size_t crt_buf_len )
6887{
6888 int ret;
6889 unsigned char const * const peer_cert_digest =
6890 ssl->session->peer_cert_digest;
6891 mbedtls_md_type_t const peer_cert_digest_type =
6892 ssl->session->peer_cert_digest_type;
6893 mbedtls_md_info_t const * const digest_info =
6894 mbedtls_md_info_from_type( peer_cert_digest_type );
6895 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6896 size_t digest_len;
6897
6898 if( peer_cert_digest == NULL || digest_info == NULL )
6899 return( -1 );
6900
6901 digest_len = mbedtls_md_get_size( digest_info );
6902 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6903 return( -1 );
6904
6905 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6906 if( ret != 0 )
6907 return( -1 );
6908
6909 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6910}
Hanno Becker5882dd02019-06-06 16:25:57 +01006911#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006912#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006913
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006914/*
6915 * Once the certificate message is read, parse it into a cert chain and
6916 * perform basic checks, but leave actual verification to the caller
6917 */
Hanno Becker35e41772019-02-05 15:37:23 +00006918static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6919 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006920{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006921 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006922#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6923 int crt_cnt=0;
6924#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006925 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006926 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006934 }
6935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6937 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006940 mbedtls_ssl_pend_fatal_alert( ssl,
6941 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006943 }
6944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006946
Paul Bakker5121ce52009-01-03 21:22:43 +00006947 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006949 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006950 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006951
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006952 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006956 mbedtls_ssl_pend_fatal_alert( ssl,
6957 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006959 }
6960
Hanno Becker33c3dc82019-01-30 14:46:46 +00006961 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6962 i += 3;
6963
Hanno Becker33c3dc82019-01-30 14:46:46 +00006964 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 while( i < ssl->in_hslen )
6966 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006967 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006968 if ( i + 3 > ssl->in_hslen ) {
6969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006970 mbedtls_ssl_pend_fatal_alert( ssl,
6971 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006972 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6973 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006974 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6975 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006976 if( ssl->in_msg[i] != 0 )
6977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006979 mbedtls_ssl_pend_fatal_alert( ssl,
6980 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006982 }
6983
Hanno Becker33c3dc82019-01-30 14:46:46 +00006984 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006985 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6986 | (unsigned int) ssl->in_msg[i + 2];
6987 i += 3;
6988
6989 if( n < 128 || i + n > ssl->in_hslen )
6990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006992 mbedtls_ssl_pend_fatal_alert( ssl,
6993 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006995 }
6996
Hanno Becker33c3dc82019-01-30 14:46:46 +00006997 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006998#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6999 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007000 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7001 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007002 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007003 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007004 /* During client-side renegotiation, check that the server's
7005 * end-CRTs hasn't changed compared to the initial handshake,
7006 * mitigating the triple handshake attack. On success, reuse
7007 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7009 if( ssl_check_peer_crt_unchanged( ssl,
7010 &ssl->in_msg[i],
7011 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007012 {
Hanno Becker35e41772019-02-05 15:37:23 +00007013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007014 mbedtls_ssl_pend_fatal_alert( ssl,
7015 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007016 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007017 }
Hanno Becker35e41772019-02-05 15:37:23 +00007018
7019 /* Now we can safely free the original chain. */
7020 ssl_clear_peer_cert( ssl->session );
7021 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007022#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7023
Hanno Becker33c3dc82019-01-30 14:46:46 +00007024 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007025#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007026 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007027#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007028 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007029 * it in-place from the input buffer instead of making a copy. */
7030 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7031#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007032 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007033 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007034 case 0: /*ok*/
7035 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7036 /* Ignore certificate with an unknown algorithm: maybe a
7037 prior certificate was already trusted. */
7038 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007039
Hanno Becker33c3dc82019-01-30 14:46:46 +00007040 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7041 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7042 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007043
Hanno Becker33c3dc82019-01-30 14:46:46 +00007044 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7045 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7046 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007047
Hanno Becker33c3dc82019-01-30 14:46:46 +00007048 default:
7049 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7050 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007051 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007052 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7053 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007054 }
7055
7056 i += n;
7057 }
7058
Hanno Becker35e41772019-02-05 15:37:23 +00007059 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007060 return( 0 );
7061}
7062
Hanno Beckerb8a08572019-02-05 12:49:06 +00007063#if defined(MBEDTLS_SSL_SRV_C)
7064static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7065{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007066 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007067 return( -1 );
7068
7069#if defined(MBEDTLS_SSL_PROTO_SSL3)
7070 /*
7071 * Check if the client sent an empty certificate
7072 */
Hanno Becker2881d802019-05-22 14:44:53 +01007073 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007074 {
7075 if( ssl->in_msglen == 2 &&
7076 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7077 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7078 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7079 {
7080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7081 return( 0 );
7082 }
7083
7084 return( -1 );
7085 }
7086#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7087
7088#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7089 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7090 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7091 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7092 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7093 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7094 {
7095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7096 return( 0 );
7097 }
7098
Hanno Beckerb8a08572019-02-05 12:49:06 +00007099#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7100 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007101
7102 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007103}
7104#endif /* MBEDTLS_SSL_SRV_C */
7105
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007106/* Check if a certificate message is expected.
7107 * Return either
7108 * - SSL_CERTIFICATE_EXPECTED, or
7109 * - SSL_CERTIFICATE_SKIP
7110 * indicating whether a Certificate message is expected or not.
7111 */
7112#define SSL_CERTIFICATE_EXPECTED 0
7113#define SSL_CERTIFICATE_SKIP 1
7114static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7115 int authmode )
7116{
Hanno Becker473f98f2019-06-26 10:27:32 +01007117 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007118 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007119
7120 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7121 return( SSL_CERTIFICATE_SKIP );
7122
7123#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007124 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007125 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007126 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7127 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7128 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007129 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007130 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007131
7132 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7133 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007134 ssl->session_negotiate->verify_result =
7135 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7136 return( SSL_CERTIFICATE_SKIP );
7137 }
7138 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007139#else
7140 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007141#endif /* MBEDTLS_SSL_SRV_C */
7142
7143 return( SSL_CERTIFICATE_EXPECTED );
7144}
7145
Hanno Becker3cf50612019-02-05 14:36:34 +00007146static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7147 int authmode,
7148 mbedtls_x509_crt *chain,
7149 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007150{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007151 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007152 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007153 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007154 mbedtls_x509_crt *ca_chain;
7155 mbedtls_x509_crl *ca_crl;
7156
7157 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7158 return( 0 );
7159
7160#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7161 if( ssl->handshake->sni_ca_chain != NULL )
7162 {
7163 ca_chain = ssl->handshake->sni_ca_chain;
7164 ca_crl = ssl->handshake->sni_ca_crl;
7165 }
7166 else
7167#endif
7168 {
7169 ca_chain = ssl->conf->ca_chain;
7170 ca_crl = ssl->conf->ca_crl;
7171 }
7172
7173 /*
7174 * Main check: verify certificate
7175 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007176 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007177 chain,
7178 ca_chain, ca_crl,
7179 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007180#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007181 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007182#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007183 &ssl->session_negotiate->verify_result,
7184 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7185
Hanno Becker8c13ee62019-02-26 16:48:17 +00007186 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007187 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007188 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007189 }
7190
7191#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007192 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007193 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7194#endif
7195
7196 /*
7197 * Secondary checks: always done, but change 'ret' only if it was 0
7198 */
7199
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007200#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007201 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007202 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007203#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007204 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007205#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007206 mbedtls_pk_context *pk;
7207 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7208 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007209 {
7210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007211 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007212 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007213
7214 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007215 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007216 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007217 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007218 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007219
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007220 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007221#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007222
7223 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007224 {
7225 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7226
7227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007228 if( verify_ret == 0 )
7229 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007230 }
7231 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007232#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007233
7234 if( mbedtls_ssl_check_cert_usage( chain,
7235 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007236 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7237 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007238 &ssl->session_negotiate->verify_result ) != 0 )
7239 {
7240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007241 if( verify_ret == 0 )
7242 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007243 }
7244
7245 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7246 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7247 * with details encoded in the verification flags. All other kinds
7248 * of error codes, including those from the user provided f_vrfy
7249 * functions, are treated as fatal and lead to a failure of
7250 * ssl_parse_certificate even if verification was optional. */
7251 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007252 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7253 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007254 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007255 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007256 }
7257
7258 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7259 {
7260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007261 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007262 }
7263
Hanno Becker8c13ee62019-02-26 16:48:17 +00007264 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007265 {
7266 uint8_t alert;
7267
7268 /* The certificate may have been rejected for several reasons.
7269 Pick one and send the corresponding alert. Which alert to send
7270 may be a subject of debate in some cases. */
7271 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7272 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7273 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7274 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7275 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7276 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7277 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7278 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7279 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7280 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7281 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7282 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7283 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7284 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7285 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7286 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7287 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7288 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7289 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7290 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7291 else
7292 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007293 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007294 }
7295
7296#if defined(MBEDTLS_DEBUG_C)
7297 if( ssl->session_negotiate->verify_result != 0 )
7298 {
7299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7300 ssl->session_negotiate->verify_result ) );
7301 }
7302 else
7303 {
7304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7305 }
7306#endif /* MBEDTLS_DEBUG_C */
7307
Hanno Becker8c13ee62019-02-26 16:48:17 +00007308 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007309}
7310
Hanno Becker34106f62019-02-08 14:59:05 +00007311#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007312
7313#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007314static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7315 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007316{
7317 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007318 /* Remember digest of the peer's end-CRT. */
7319 ssl->session_negotiate->peer_cert_digest =
7320 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7321 if( ssl->session_negotiate->peer_cert_digest == NULL )
7322 {
7323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7324 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007325 mbedtls_ssl_pend_fatal_alert( ssl,
7326 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007327
7328 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7329 }
7330
7331 ret = mbedtls_md( mbedtls_md_info_from_type(
7332 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7333 start, len,
7334 ssl->session_negotiate->peer_cert_digest );
7335
7336 ssl->session_negotiate->peer_cert_digest_type =
7337 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7338 ssl->session_negotiate->peer_cert_digest_len =
7339 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7340
7341 return( ret );
7342}
Hanno Becker5882dd02019-06-06 16:25:57 +01007343#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007344
7345static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7346 unsigned char *start, size_t len )
7347{
7348 unsigned char *end = start + len;
7349 int ret;
7350
7351 /* Make a copy of the peer's raw public key. */
7352 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7353 ret = mbedtls_pk_parse_subpubkey( &start, end,
7354 &ssl->handshake->peer_pubkey );
7355 if( ret != 0 )
7356 {
7357 /* We should have parsed the public key before. */
7358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7359 }
7360
7361 return( 0 );
7362}
7363#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7364
Hanno Becker3cf50612019-02-05 14:36:34 +00007365int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7366{
7367 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007368 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007369#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7370 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7371 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007372 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007373#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007374 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007375#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007376 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007377 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007378
7379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7380
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007381 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7382 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007383 {
7384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007385 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007386 }
7387
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007388#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7389 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007390 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007391 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007392 chain = ssl->handshake->ecrs_peer_cert;
7393 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007394 goto crt_verify;
7395 }
7396#endif
7397
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007398 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007399 {
7400 /* mbedtls_ssl_read_record may have sent an alert already. We
7401 let it decide whether to alert. */
7402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007403 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007404 }
7405
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007406#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007407 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7408 {
7409 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007410
Hanno Beckerb8a08572019-02-05 12:49:06 +00007411 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007412 ret = 0;
7413 else
7414 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007415
Hanno Becker613d4902019-02-05 13:11:17 +00007416 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007417 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007418#endif /* MBEDTLS_SSL_SRV_C */
7419
Hanno Becker35e41772019-02-05 15:37:23 +00007420 /* Clear existing peer CRT structure in case we tried to
7421 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007422 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007423
7424 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7425 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007426 {
7427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7428 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007429 mbedtls_ssl_pend_fatal_alert( ssl,
7430 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007431
Hanno Beckere4aeb762019-02-05 17:19:52 +00007432 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7433 goto exit;
7434 }
7435 mbedtls_x509_crt_init( chain );
7436
7437 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007438 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007439 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007440
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007441#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7442 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007443 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007444
7445crt_verify:
7446 if( ssl->handshake->ecrs_enabled)
7447 rs_ctx = &ssl->handshake->ecrs_ctx;
7448#endif
7449
Hanno Becker3cf50612019-02-05 14:36:34 +00007450 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007451 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007452 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007453 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007454
Hanno Becker3008d282019-02-05 17:02:28 +00007455#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007456 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007457 size_t pk_len;
7458 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007459
Hanno Becker34106f62019-02-08 14:59:05 +00007460 /* We parse the CRT chain without copying, so
7461 * these pointers point into the input buffer,
7462 * and are hence still valid after freeing the
7463 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007464
Hanno Becker5882dd02019-06-06 16:25:57 +01007465#if defined(MBEDTLS_SSL_RENEGOTIATION)
7466 unsigned char *crt_start;
7467 size_t crt_len;
7468
Hanno Becker34106f62019-02-08 14:59:05 +00007469 crt_start = chain->raw.p;
7470 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007471#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007472
Hanno Becker8c13ee62019-02-26 16:48:17 +00007473 pk_start = chain->cache->pk_raw.p;
7474 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007475
7476 /* Free the CRT structures before computing
7477 * digest and copying the peer's public key. */
7478 mbedtls_x509_crt_free( chain );
7479 mbedtls_free( chain );
7480 chain = NULL;
7481
Hanno Becker5882dd02019-06-06 16:25:57 +01007482#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007483 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007485 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007486#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007487
Hanno Becker34106f62019-02-08 14:59:05 +00007488 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007489 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007490 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007491 }
Hanno Becker34106f62019-02-08 14:59:05 +00007492#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7493 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007494 ssl->session_negotiate->peer_cert = chain;
7495 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007496#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007499
Hanno Becker613d4902019-02-05 13:11:17 +00007500exit:
7501
Hanno Beckere4aeb762019-02-05 17:19:52 +00007502 if( ret == 0 )
7503 ssl->state++;
7504
7505#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7506 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7507 {
7508 ssl->handshake->ecrs_peer_cert = chain;
7509 chain = NULL;
7510 }
7511#endif
7512
7513 if( chain != NULL )
7514 {
7515 mbedtls_x509_crt_free( chain );
7516 mbedtls_free( chain );
7517 }
7518
Paul Bakker5121ce52009-01-03 21:22:43 +00007519 return( ret );
7520}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007521#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007524{
7525 int ret;
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007530 ssl->out_msglen = 1;
7531 ssl->out_msg[0] = 1;
7532
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 ssl->state++;
7534
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007535 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007536 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007538 return( ret );
7539 }
7540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007542
7543 return( 0 );
7544}
7545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007547{
7548 int ret;
7549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007551
Hanno Becker327c93b2018-08-15 13:56:18 +01007552 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007555 return( ret );
7556 }
7557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007561 mbedtls_ssl_pend_fatal_alert( ssl,
7562 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007564 }
7565
Hanno Beckere678eaa2018-08-21 14:57:46 +01007566 /* CCS records are only accepted if they have length 1 and content '1',
7567 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007568
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007569 /*
7570 * Switch to our negotiated transform and session parameters for inbound
7571 * data.
7572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007574 ssl->transform_in = ssl->transform_negotiate;
7575 ssl->session_in = ssl->session_negotiate;
7576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007578 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007581 ssl_dtls_replay_reset( ssl );
7582#endif
7583
7584 /* Increment epoch */
7585 if( ++ssl->in_epoch == 0 )
7586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007588 /* This is highly unlikely to happen for legitimate reasons, so
7589 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007591 }
7592 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007593 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007595#if defined(MBEDTLS_SSL_PROTO_TLS)
7596 {
7597 memset( ssl->in_ctr, 0, 8 );
7598 }
7599#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007600
Hanno Beckerf5970a02019-05-08 09:38:41 +01007601 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7604 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007609 mbedtls_ssl_pend_fatal_alert( ssl,
7610 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007612 }
7613 }
7614#endif
7615
Paul Bakker5121ce52009-01-03 21:22:43 +00007616 ssl->state++;
7617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007619
7620 return( 0 );
7621}
7622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007624{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7626 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007627 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007628 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7631#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007632 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007635 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007638}
7639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007643
7644 /*
7645 * Free our handshake params
7646 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007647 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007649 ssl->handshake = NULL;
7650
7651 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007652 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007653 */
7654 if( ssl->transform )
7655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 mbedtls_ssl_transform_free( ssl->transform );
7657 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007658 }
7659 ssl->transform = ssl->transform_negotiate;
7660 ssl->transform_negotiate = NULL;
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007663}
7664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669#if defined(MBEDTLS_SSL_RENEGOTIATION)
7670 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007673 ssl->renego_records_seen = 0;
7674 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007675#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007676
7677 /*
7678 * Free the previous session and switch in the current one
7679 */
Paul Bakker0a597072012-09-25 21:55:46 +00007680 if( ssl->session )
7681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007683 /* RFC 7366 3.1: keep the EtM state */
7684 ssl->session_negotiate->encrypt_then_mac =
7685 ssl->session->encrypt_then_mac;
7686#endif
7687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688 mbedtls_ssl_session_free( ssl->session );
7689 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007690 }
7691 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007692 ssl->session_negotiate = NULL;
7693
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007694#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007695 /*
7696 * Add cache entry
7697 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007698 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007699 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007700 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007701 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007702 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007704 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007705#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007708 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007709 ssl->handshake->flight != NULL )
7710 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007711 /* Cancel handshake timer */
7712 ssl_set_timer( ssl, 0 );
7713
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007714 /* Keep last flight around in case we need to resend it:
7715 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007717 }
7718 else
7719#endif
7720 ssl_handshake_wrapup_free_hs_transform( ssl );
7721
Paul Bakker48916f92012-09-16 19:57:18 +00007722 ssl->state++;
7723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007725}
7726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007728{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007729 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007732
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007733 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007734
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007735 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7736 mbedtls_ssl_suite_get_mac(
7737 mbedtls_ssl_ciphersuite_from_id(
7738 mbedtls_ssl_session_get_ciphersuite(
7739 ssl->session_negotiate ) ) ),
7740 ssl, ssl->out_msg + 4,
7741 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007742
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007743 /*
7744 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7745 * may define some other value. Currently (early 2016), no defined
7746 * ciphersuite does this (and this is unlikely to change as activity has
7747 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7748 */
Hanno Becker2881d802019-05-22 14:44:53 +01007749 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007752 ssl->verify_data_len = hash_len;
7753 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007754#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007755
Paul Bakker5121ce52009-01-03 21:22:43 +00007756 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7758 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007759
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007760#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007761 /*
7762 * In case of session resuming, invert the client and server
7763 * ChangeCipherSpec messages order.
7764 */
Paul Bakker0a597072012-09-25 21:55:46 +00007765 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007768 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7769 MBEDTLS_SSL_IS_CLIENT )
7770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007772 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007773#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007775 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7776 MBEDTLS_SSL_IS_SERVER )
7777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007779 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007780#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007781 }
7782 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007783#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007784 ssl->state++;
7785
Paul Bakker48916f92012-09-16 19:57:18 +00007786 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007787 * Switch to our negotiated transform and session parameters for outbound
7788 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007793 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007794 {
7795 unsigned char i;
7796
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007797 /* Remember current epoch settings for resending */
7798 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007799 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007800
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007801 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007802 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007803
7804 /* Increment epoch */
7805 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007806 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007807 break;
7808
7809 /* The loop goes to its end iff the counter is wrapping */
7810 if( i == 0 )
7811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7813 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007814 }
7815 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007816 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007818#if defined(MBEDTLS_SSL_PROTO_TLS)
7819 {
7820 memset( ssl->cur_out_ctr, 0, 8 );
7821 }
7822#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007823
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007824 ssl->transform_out = ssl->transform_negotiate;
7825 ssl->session_out = ssl->session_negotiate;
7826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7828 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7833 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007834 }
7835 }
7836#endif
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007839 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007841#endif
7842
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007843 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007844 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 return( ret );
7847 }
7848
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007849#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007850 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007851 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7852 {
7853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7854 return( ret );
7855 }
7856#endif
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007859
7860 return( 0 );
7861}
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007864#define SSL_MAX_HASH_LEN 36
7865#else
7866#define SSL_MAX_HASH_LEN 12
7867#endif
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007870{
Paul Bakker23986e52011-04-24 08:57:21 +00007871 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007872 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007873 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007876
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007877 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7878 mbedtls_ssl_suite_get_mac(
7879 mbedtls_ssl_ciphersuite_from_id(
7880 mbedtls_ssl_session_get_ciphersuite(
7881 ssl->session_negotiate ) ) ),
7882 ssl, buf,
7883 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007884
Hanno Becker327c93b2018-08-15 13:56:18 +01007885 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007888 return( ret );
7889 }
7890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007894 mbedtls_ssl_pend_fatal_alert( ssl,
7895 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007897 }
7898
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007899 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007901 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007902 hash_len = 36;
7903 else
7904#endif
7905 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7908 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007911 mbedtls_ssl_pend_fatal_alert( ssl,
7912 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007914 }
7915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007917 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007920 mbedtls_ssl_pend_fatal_alert( ssl,
7921 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007923 }
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007926 ssl->verify_data_len = hash_len;
7927 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007928#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007929
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007930#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007931 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007934 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007938 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007940#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007941 }
7942 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007943#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007944 ssl->state++;
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007947 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007949#endif
7950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007952
7953 return( 0 );
7954}
7955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7961 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7962 mbedtls_md5_init( &handshake->fin_md5 );
7963 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007964 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7965 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7968#if defined(MBEDTLS_SHA256_C)
7969 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007970 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972#if defined(MBEDTLS_SHA512_C)
7973 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007974 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007977
Hanno Becker7e5437a2017-04-28 17:15:26 +01007978#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7979 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7980 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7981#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_DHM_C)
7984 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007985#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986#if defined(MBEDTLS_ECDH_C)
7987 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007988#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007989#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007990 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007991#if defined(MBEDTLS_SSL_CLI_C)
7992 handshake->ecjpake_cache = NULL;
7993 handshake->ecjpake_cache_len = 0;
7994#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007995#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007996
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007997#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007998 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007999#endif
8000
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008001#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8002 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8003#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008004
8005#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8006 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8007 mbedtls_pk_init( &handshake->peer_pubkey );
8008#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008009}
8010
Hanno Becker611a83b2018-01-03 14:27:32 +00008011void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8016 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008017
Hanno Becker92231322018-01-03 15:32:51 +00008018#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 mbedtls_md_init( &transform->md_ctx_enc );
8020 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008021#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008022}
8023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008027}
8028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008030{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008031 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008032 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008034 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008036 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008037 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008038
8039 /*
8040 * Either the pointers are now NULL or cleared properly and can be freed.
8041 * Now allocate missing structures.
8042 */
8043 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008044 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008045 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008046 }
Paul Bakker48916f92012-09-16 19:57:18 +00008047
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008048 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008049 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008050 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008051 }
Paul Bakker48916f92012-09-16 19:57:18 +00008052
Paul Bakker82788fb2014-10-20 13:59:19 +02008053 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008054 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008055 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008056 }
Paul Bakker48916f92012-09-16 19:57:18 +00008057
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008058 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008059 if( ssl->handshake == NULL ||
8060 ssl->transform_negotiate == NULL ||
8061 ssl->session_negotiate == NULL )
8062 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 mbedtls_free( ssl->handshake );
8066 mbedtls_free( ssl->transform_negotiate );
8067 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008068
8069 ssl->handshake = NULL;
8070 ssl->transform_negotiate = NULL;
8071 ssl->session_negotiate = NULL;
8072
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008073 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008074 }
8075
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008076 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008078 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008079 ssl_handshake_params_init( ssl->handshake );
8080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008082 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008083 {
8084 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008085
Hanno Becker2d9623f2019-06-13 12:07:05 +01008086 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008087 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8088 else
8089 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8090 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008091#endif
8092
Paul Bakker48916f92012-09-16 19:57:18 +00008093 return( 0 );
8094}
8095
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008096#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008097/* Dummy cookie callbacks for defaults */
8098static int ssl_cookie_write_dummy( void *ctx,
8099 unsigned char **p, unsigned char *end,
8100 const unsigned char *cli_id, size_t cli_id_len )
8101{
8102 ((void) ctx);
8103 ((void) p);
8104 ((void) end);
8105 ((void) cli_id);
8106 ((void) cli_id_len);
8107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008108 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008109}
8110
8111static int ssl_cookie_check_dummy( void *ctx,
8112 const unsigned char *cookie, size_t cookie_len,
8113 const unsigned char *cli_id, size_t cli_id_len )
8114{
8115 ((void) ctx);
8116 ((void) cookie);
8117 ((void) cookie_len);
8118 ((void) cli_id);
8119 ((void) cli_id_len);
8120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008122}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008123#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008124
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008125/* Once ssl->out_hdr as the address of the beginning of the
8126 * next outgoing record is set, deduce the other pointers.
8127 *
8128 * Note: For TLS, we save the implicit record sequence number
8129 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8130 * and the caller has to make sure there's space for this.
8131 */
8132
8133static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8134 mbedtls_ssl_transform *transform )
8135{
8136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008137 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008138 {
8139 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008140#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008141 ssl->out_cid = ssl->out_ctr + 8;
8142 ssl->out_len = ssl->out_cid;
8143 if( transform != NULL )
8144 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008145#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008146 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008147#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008148 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008149 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008150 MBEDTLS_SSL_TRANSPORT_ELSE
8151#endif /* MBEDTLS_SSL_PROTO_DTLS */
8152#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008153 {
8154 ssl->out_ctr = ssl->out_hdr - 8;
8155 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008156#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008157 ssl->out_cid = ssl->out_len;
8158#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008159 ssl->out_iv = ssl->out_hdr + 5;
8160 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008161#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008162
8163 /* Adjust out_msg to make space for explicit IV, if used. */
8164 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008165 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008166 {
8167 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8168 }
8169 else
8170 ssl->out_msg = ssl->out_iv;
8171}
8172
8173/* Once ssl->in_hdr as the address of the beginning of the
8174 * next incoming record is set, deduce the other pointers.
8175 *
8176 * Note: For TLS, we save the implicit record sequence number
8177 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8178 * and the caller has to make sure there's space for this.
8179 */
8180
Hanno Beckerf5970a02019-05-08 09:38:41 +01008181static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008182{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008183 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008184 * of unprotected TLS/DTLS records, with ssl->in_msg
8185 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008186 *
8187 * When decrypting a protected record, ssl->in_msg
8188 * will be shifted to point to the beginning of the
8189 * record plaintext.
8190 */
8191
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008192#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008193 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008194 {
Hanno Becker70e79282019-05-03 14:34:53 +01008195 /* This sets the header pointers to match records
8196 * without CID. When we receive a record containing
8197 * a CID, the fields are shifted accordingly in
8198 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008199 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008200#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008201 ssl->in_cid = ssl->in_ctr + 8;
8202 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008203#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008204 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008205#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008206 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008207 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008208 MBEDTLS_SSL_TRANSPORT_ELSE
8209#endif /* MBEDTLS_SSL_PROTO_DTLS */
8210#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008211 {
8212 ssl->in_ctr = ssl->in_hdr - 8;
8213 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008214#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008215 ssl->in_cid = ssl->in_len;
8216#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008217 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008218 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008219#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008220}
8221
Paul Bakker5121ce52009-01-03 21:22:43 +00008222/*
8223 * Initialize an SSL context
8224 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008225void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8226{
8227 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8228}
8229
8230/*
8231 * Setup an SSL context
8232 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008233
8234static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8235{
8236 /* Set the incoming and outgoing record pointers. */
8237#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008238 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008239 {
8240 ssl->out_hdr = ssl->out_buf;
8241 ssl->in_hdr = ssl->in_buf;
8242 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008243 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008244#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008245#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008246 {
8247 ssl->out_hdr = ssl->out_buf + 8;
8248 ssl->in_hdr = ssl->in_buf + 8;
8249 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008250#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008251
8252 /* Derive other internal pointers. */
8253 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008254 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008255}
8256
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008257int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008258 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008259{
Paul Bakker48916f92012-09-16 19:57:18 +00008260 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008261
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008262 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008263
Hanno Beckeref982d52019-07-23 15:56:18 +01008264#if defined(MBEDTLS_USE_TINYCRYPT)
8265 uECC_set_rng( &uecc_rng_wrapper );
8266#endif
8267
Paul Bakker62f2dee2012-09-28 07:31:51 +00008268 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008269 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008270 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008271
8272 /* Set to NULL in case of an error condition */
8273 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008274
Angus Grattond8213d02016-05-25 20:56:48 +10008275 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8276 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008277 {
Angus Grattond8213d02016-05-25 20:56:48 +10008278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008279 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008280 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008281 }
8282
8283 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8284 if( ssl->out_buf == NULL )
8285 {
8286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008287 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008288 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008289 }
8290
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008291 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008292
Paul Bakker48916f92012-09-16 19:57:18 +00008293 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008294 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008295
Hanno Beckerc8f52992019-07-25 11:15:08 +01008296 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008297
Paul Bakker5121ce52009-01-03 21:22:43 +00008298 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008299
8300error:
8301 mbedtls_free( ssl->in_buf );
8302 mbedtls_free( ssl->out_buf );
8303
8304 ssl->conf = NULL;
8305
8306 ssl->in_buf = NULL;
8307 ssl->out_buf = NULL;
8308
8309 ssl->in_hdr = NULL;
8310 ssl->in_ctr = NULL;
8311 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008312 ssl->in_msg = NULL;
8313
8314 ssl->out_hdr = NULL;
8315 ssl->out_ctr = NULL;
8316 ssl->out_len = NULL;
8317 ssl->out_iv = NULL;
8318 ssl->out_msg = NULL;
8319
k-stachowiak9f7798e2018-07-31 16:52:32 +02008320 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008321}
8322
8323/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008324 * Reset an initialized and used SSL context for re-use while retaining
8325 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008326 *
8327 * If partial is non-zero, keep data in the input buffer and client ID.
8328 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008329 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008330static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008331{
Paul Bakker48916f92012-09-16 19:57:18 +00008332 int ret;
8333
Hanno Becker7e772132018-08-10 12:38:21 +01008334#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8335 !defined(MBEDTLS_SSL_SRV_C)
8336 ((void) partial);
8337#endif
8338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008340
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008341 /* Cancel any possibly running timer */
8342 ssl_set_timer( ssl, 0 );
8343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_RENEGOTIATION)
8345 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008346 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008347
8348 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8350 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008351#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008353
Paul Bakker7eb013f2011-10-06 12:37:39 +00008354 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008355 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008356
8357 ssl->in_msgtype = 0;
8358 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008360 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008361 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008364 ssl_dtls_replay_reset( ssl );
8365#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008366
8367 ssl->in_hslen = 0;
8368 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008369
8370 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008371
8372 ssl->out_msgtype = 0;
8373 ssl->out_msglen = 0;
8374 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8376 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008377 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008378#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008379
Hanno Becker19859472018-08-06 09:40:20 +01008380 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8381
Paul Bakker48916f92012-09-16 19:57:18 +00008382 ssl->transform_in = NULL;
8383 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008384
Hanno Becker78640902018-08-13 16:35:15 +01008385 ssl->session_in = NULL;
8386 ssl->session_out = NULL;
8387
Angus Grattond8213d02016-05-25 20:56:48 +10008388 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008389
8390#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008391 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008392#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8393 {
8394 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008395 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008396 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8399 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8402 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8405 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008406 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008407 }
8408#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008409
Paul Bakker48916f92012-09-16 19:57:18 +00008410 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008412 mbedtls_ssl_transform_free( ssl->transform );
8413 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008414 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008415 }
Paul Bakker48916f92012-09-16 19:57:18 +00008416
Paul Bakkerc0463502013-02-14 11:19:38 +01008417 if( ssl->session )
8418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419 mbedtls_ssl_session_free( ssl->session );
8420 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008421 ssl->session = NULL;
8422 }
8423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008425 ssl->alpn_chosen = NULL;
8426#endif
8427
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008428#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008429#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008430 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008431#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008432 {
8433 mbedtls_free( ssl->cli_id );
8434 ssl->cli_id = NULL;
8435 ssl->cli_id_len = 0;
8436 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008437#endif
8438
Paul Bakker48916f92012-09-16 19:57:18 +00008439 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8440 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008441
8442 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008443}
8444
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008445/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008446 * Reset an initialized and used SSL context for re-use while retaining
8447 * all application-set variables, function pointers and data.
8448 */
8449int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8450{
8451 return( ssl_session_reset_int( ssl, 0 ) );
8452}
8453
8454/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008455 * SSL set accessors
8456 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008457#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008458void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008459{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008460 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008461}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008462#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008463
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008464void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008465{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008466 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008467}
8468
Hanno Becker7f376f42019-06-12 16:20:48 +01008469#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8470 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008471void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008472{
Hanno Becker7f376f42019-06-12 16:20:48 +01008473 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008474}
Hanno Becker7f376f42019-06-12 16:20:48 +01008475#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008476
Hanno Beckerde671542019-06-12 16:30:46 +01008477#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8478 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8479void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8480 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008481{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008482 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008483}
Hanno Beckerde671542019-06-12 16:30:46 +01008484#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008487
Hanno Becker1841b0a2018-08-24 11:13:57 +01008488void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8489 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008490{
8491 ssl->disable_datagram_packing = !allow_packing;
8492}
8493
Hanno Becker1f835fa2019-06-13 10:14:59 +01008494#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8495 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008496void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8497 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008499 conf->hs_timeout_min = min;
8500 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008501}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008502#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8503 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8504void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8505 uint32_t min, uint32_t max )
8506{
8507 ((void) conf);
8508 ((void) min);
8509 ((void) max);
8510}
8511#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8512 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8513
8514#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008515
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008516void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008517{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008518#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8519 conf->authmode = authmode;
8520#else
8521 ((void) conf);
8522 ((void) authmode);
8523#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008524}
8525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008527void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008528 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008529 void *p_vrfy )
8530{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008531 conf->f_vrfy = f_vrfy;
8532 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008535
Hanno Beckerece325c2019-06-13 15:39:27 +01008536#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008537void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008538 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008539 void *p_rng )
8540{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008541 conf->f_rng = f_rng;
8542 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008543}
Hanno Beckerece325c2019-06-13 15:39:27 +01008544#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008545
Hanno Becker14a4a442019-07-02 17:00:34 +01008546#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008547void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008548 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008549 void *p_dbg )
8550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008551 conf->f_dbg = f_dbg;
8552 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008553}
Hanno Becker14a4a442019-07-02 17:00:34 +01008554#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008555
Hanno Beckera58a8962019-06-13 16:11:15 +01008556#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8557 !defined(MBEDTLS_SSL_CONF_SEND) && \
8558 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008560 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008561 mbedtls_ssl_send_t *f_send,
8562 mbedtls_ssl_recv_t *f_recv,
8563 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008564{
Hanno Beckera58a8962019-06-13 16:11:15 +01008565 ssl->p_bio = p_bio;
8566 ssl->f_send = f_send;
8567 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008568 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008569}
Hanno Beckera58a8962019-06-13 16:11:15 +01008570#else
8571void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8572 void *p_bio )
8573{
8574 ssl->p_bio = p_bio;
8575}
8576#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008577
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008578#if defined(MBEDTLS_SSL_PROTO_DTLS)
8579void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8580{
8581 ssl->mtu = mtu;
8582}
8583#endif
8584
Hanno Becker1f835fa2019-06-13 10:14:59 +01008585#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008586void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008587{
8588 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008589}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008590#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008591
Hanno Becker0ae6b242019-06-13 16:45:36 +01008592#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8593 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008594void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8595 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008596 mbedtls_ssl_set_timer_t *f_set_timer,
8597 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008598{
8599 ssl->p_timer = p_timer;
8600 ssl->f_set_timer = f_set_timer;
8601 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008602 /* Make sure we start with no timer running */
8603 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008604}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008605#else
8606void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8607 void *p_timer )
8608{
8609 ssl->p_timer = p_timer;
8610 /* Make sure we start with no timer running */
8611 ssl_set_timer( ssl, 0 );
8612}
8613#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008614
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008615#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008616void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008617 void *p_cache,
8618 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8619 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008620{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008621 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008622 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008623 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008624}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008625#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008626
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008627#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008629{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008630 int ret;
8631
8632 if( ssl == NULL ||
8633 session == NULL ||
8634 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008635 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008638 }
8639
Hanno Becker58fccf22019-02-06 14:30:46 +00008640 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8641 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008642 return( ret );
8643
Paul Bakker0a597072012-09-25 21:55:46 +00008644 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008645
8646 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008647}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008648#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008649
Hanno Becker73f4cb12019-06-27 13:51:07 +01008650#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008651void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008652 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008653{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008654 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8655 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8656 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8657 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008658}
8659
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008660void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008661 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008662 int major, int minor )
8663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008665 return;
8666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008668 return;
8669
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008670 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008671}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008672#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008674#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008675void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008676 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008677{
8678 conf->cert_profile = profile;
8679}
8680
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008681/* Append a new keycert entry to a (possibly empty) list */
8682static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8683 mbedtls_x509_crt *cert,
8684 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008685{
niisato8ee24222018-06-25 19:05:48 +09008686 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008687
niisato8ee24222018-06-25 19:05:48 +09008688 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8689 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008690 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008691
niisato8ee24222018-06-25 19:05:48 +09008692 new_cert->cert = cert;
8693 new_cert->key = key;
8694 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008695
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008696 /* Update head is the list was null, else add to the end */
8697 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008698 {
niisato8ee24222018-06-25 19:05:48 +09008699 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008700 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008701 else
8702 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008703 mbedtls_ssl_key_cert *cur = *head;
8704 while( cur->next != NULL )
8705 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008706 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008707 }
8708
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008709 return( 0 );
8710}
8711
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008712int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008713 mbedtls_x509_crt *own_cert,
8714 mbedtls_pk_context *pk_key )
8715{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008716 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008717}
8718
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008719void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008720 mbedtls_x509_crt *ca_chain,
8721 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008722{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008723 conf->ca_chain = ca_chain;
8724 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008727
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008728#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8729int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8730 mbedtls_x509_crt *own_cert,
8731 mbedtls_pk_context *pk_key )
8732{
8733 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8734 own_cert, pk_key ) );
8735}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008736
8737void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8738 mbedtls_x509_crt *ca_chain,
8739 mbedtls_x509_crl *ca_crl )
8740{
8741 ssl->handshake->sni_ca_chain = ca_chain;
8742 ssl->handshake->sni_ca_crl = ca_crl;
8743}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008744
8745void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8746 int authmode )
8747{
8748 ssl->handshake->sni_authmode = authmode;
8749}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008750#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8751
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008752#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008753/*
8754 * Set EC J-PAKE password for current handshake
8755 */
8756int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8757 const unsigned char *pw,
8758 size_t pw_len )
8759{
8760 mbedtls_ecjpake_role role;
8761
Janos Follath8eb64132016-06-03 15:40:57 +01008762 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8764
Hanno Becker2d9623f2019-06-13 12:07:05 +01008765 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008766 role = MBEDTLS_ECJPAKE_SERVER;
8767 else
8768 role = MBEDTLS_ECJPAKE_CLIENT;
8769
8770 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8771 role,
8772 MBEDTLS_MD_SHA256,
8773 MBEDTLS_ECP_DP_SECP256R1,
8774 pw, pw_len ) );
8775}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008776#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008779int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008780 const unsigned char *psk, size_t psk_len,
8781 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008782{
Paul Bakker6db455e2013-09-18 17:29:31 +02008783 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008788
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008789 /* Identity len will be encoded on two bytes */
8790 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008791 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008792 {
8793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8794 }
8795
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008796 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008797 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008798 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008799
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008800 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008801 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008802 conf->psk_len = 0;
8803 }
8804 if( conf->psk_identity != NULL )
8805 {
8806 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008807 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008808 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008809 }
8810
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008811 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8812 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008813 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008814 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008815 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008816 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008817 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008818 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008819 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008820
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008821 conf->psk_len = psk_len;
8822 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008823
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008824 memcpy( conf->psk, psk, conf->psk_len );
8825 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008826
8827 return( 0 );
8828}
8829
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008830int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8831 const unsigned char *psk, size_t psk_len )
8832{
8833 if( psk == NULL || ssl->handshake == NULL )
8834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8835
8836 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8838
8839 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008840 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008841 mbedtls_platform_zeroize( ssl->handshake->psk,
8842 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008843 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008844 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008845 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008846
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008847 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008848 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008849
8850 ssl->handshake->psk_len = psk_len;
8851 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8852
8853 return( 0 );
8854}
8855
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008856void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008858 size_t),
8859 void *p_psk )
8860{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008861 conf->f_psk = f_psk;
8862 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008864#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008865
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008866#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008867
8868#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008869int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008870{
8871 int ret;
8872
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008873 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8874 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8875 {
8876 mbedtls_mpi_free( &conf->dhm_P );
8877 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008878 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008879 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008880
8881 return( 0 );
8882}
Hanno Becker470a8c42017-10-04 15:28:46 +01008883#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008884
Hanno Beckera90658f2017-10-04 15:29:08 +01008885int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8886 const unsigned char *dhm_P, size_t P_len,
8887 const unsigned char *dhm_G, size_t G_len )
8888{
8889 int ret;
8890
8891 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8892 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8893 {
8894 mbedtls_mpi_free( &conf->dhm_P );
8895 mbedtls_mpi_free( &conf->dhm_G );
8896 return( ret );
8897 }
8898
8899 return( 0 );
8900}
Paul Bakker5121ce52009-01-03 21:22:43 +00008901
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008902int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008903{
8904 int ret;
8905
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008906 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8907 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8908 {
8909 mbedtls_mpi_free( &conf->dhm_P );
8910 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008911 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008912 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008913
8914 return( 0 );
8915}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008916#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008917
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008918#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8919/*
8920 * Set the minimum length for Diffie-Hellman parameters
8921 */
8922void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8923 unsigned int bitlen )
8924{
8925 conf->dhm_min_bitlen = bitlen;
8926}
8927#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8928
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008929#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008930/*
8931 * Set allowed/preferred hashes for handshake signatures
8932 */
8933void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8934 const int *hashes )
8935{
Hanno Becker56595f42019-06-19 16:31:38 +01008936#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008937 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008938#else
8939 ((void) conf);
8940 ((void) hashes);
8941#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008942}
Hanno Becker947194e2017-04-07 13:25:49 +01008943#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008944
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008945#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008946#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008947/*
8948 * Set the allowed elliptic curves
8949 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008950void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008951 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008952{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008953 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008954}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008955#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008956#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008957
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008958#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008959int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008960{
Hanno Becker947194e2017-04-07 13:25:49 +01008961 /* Initialize to suppress unnecessary compiler warning */
8962 size_t hostname_len = 0;
8963
8964 /* Check if new hostname is valid before
8965 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008966 if( hostname != NULL )
8967 {
8968 hostname_len = strlen( hostname );
8969
8970 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8972 }
8973
8974 /* Now it's clear that we will overwrite the old hostname,
8975 * so we can free it safely */
8976
8977 if( ssl->hostname != NULL )
8978 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008979 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008980 mbedtls_free( ssl->hostname );
8981 }
8982
8983 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008984
Paul Bakker5121ce52009-01-03 21:22:43 +00008985 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008986 {
8987 ssl->hostname = NULL;
8988 }
8989 else
8990 {
8991 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008992 if( ssl->hostname == NULL )
8993 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008994
Hanno Becker947194e2017-04-07 13:25:49 +01008995 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008996
Hanno Becker947194e2017-04-07 13:25:49 +01008997 ssl->hostname[hostname_len] = '\0';
8998 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008999
9000 return( 0 );
9001}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009002#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009003
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009004#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009005void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009007 const unsigned char *, size_t),
9008 void *p_sni )
9009{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009010 conf->f_sni = f_sni;
9011 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009016int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009017{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009018 size_t cur_len, tot_len;
9019 const char **p;
9020
9021 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009022 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9023 * MUST NOT be truncated."
9024 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009025 */
9026 tot_len = 0;
9027 for( p = protos; *p != NULL; p++ )
9028 {
9029 cur_len = strlen( *p );
9030 tot_len += cur_len;
9031
9032 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009034 }
9035
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009036 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009037
9038 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009039}
9040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009042{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009043 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009045#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009046
Hanno Becker33b9b252019-07-05 11:23:25 +01009047#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9048 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9049void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9050 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009051{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009052 conf->max_major_ver = major;
9053 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009054}
Hanno Becker33b9b252019-07-05 11:23:25 +01009055#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9056 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009057
Hanno Becker33b9b252019-07-05 11:23:25 +01009058#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9059 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9060void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9061 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009062{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009063 conf->min_major_ver = major;
9064 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009065}
Hanno Becker33b9b252019-07-05 11:23:25 +01009066#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9067 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009070void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009071{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009072 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009073}
9074#endif
9075
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009076#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009077void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9078 char cert_req_ca_list )
9079{
9080 conf->cert_req_ca_list = cert_req_ca_list;
9081}
9082#endif
9083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009085void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009087 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009088}
9089#endif
9090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009092#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009093void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009094{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009095 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009096}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009097#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9098#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009099void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009100 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009101{
9102 conf->enforce_extended_master_secret = ems_enf;
9103}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009104#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009105#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009106
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009107#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009108void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009109{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009110 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009111}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009112#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009115int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009117 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009118 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009121 }
9122
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009123 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009124
9125 return( 0 );
9126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009129#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009130void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009132 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009137void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009138{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009139 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009140}
9141#endif
9142
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009143#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009144void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009145{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009146 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009147}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009148#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009151void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009152{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009153 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009154}
9155
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009156void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009157{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009158 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009159}
9160
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009161void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009162 const unsigned char period[8] )
9163{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009164 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009169#if defined(MBEDTLS_SSL_CLI_C)
9170void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009171{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009172 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009173}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009174#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009175
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009176#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009177void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9178 mbedtls_ssl_ticket_write_t *f_ticket_write,
9179 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9180 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009181{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009182 conf->f_ticket_write = f_ticket_write;
9183 conf->f_ticket_parse = f_ticket_parse;
9184 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009185}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009187#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009188
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009189#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9190void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9191 mbedtls_ssl_export_keys_t *f_export_keys,
9192 void *p_export_keys )
9193{
9194 conf->f_export_keys = f_export_keys;
9195 conf->p_export_keys = p_export_keys;
9196}
9197#endif
9198
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009199#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009200void mbedtls_ssl_conf_async_private_cb(
9201 mbedtls_ssl_config *conf,
9202 mbedtls_ssl_async_sign_t *f_async_sign,
9203 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9204 mbedtls_ssl_async_resume_t *f_async_resume,
9205 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009206 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009207{
9208 conf->f_async_sign_start = f_async_sign;
9209 conf->f_async_decrypt_start = f_async_decrypt;
9210 conf->f_async_resume = f_async_resume;
9211 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009212 conf->p_async_config_data = async_config_data;
9213}
9214
Gilles Peskine8f97af72018-04-26 11:46:10 +02009215void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9216{
9217 return( conf->p_async_config_data );
9218}
9219
Gilles Peskine1febfef2018-04-30 11:54:39 +02009220void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009221{
9222 if( ssl->handshake == NULL )
9223 return( NULL );
9224 else
9225 return( ssl->handshake->user_async_ctx );
9226}
9227
Gilles Peskine1febfef2018-04-30 11:54:39 +02009228void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009229 void *ctx )
9230{
9231 if( ssl->handshake != NULL )
9232 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009233}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009234#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009235
Paul Bakker5121ce52009-01-03 21:22:43 +00009236/*
9237 * SSL get accessors
9238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009239size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009240{
9241 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9242}
9243
Hanno Becker8b170a02017-10-10 11:51:19 +01009244int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9245{
9246 /*
9247 * Case A: We're currently holding back
9248 * a message for further processing.
9249 */
9250
9251 if( ssl->keep_current_message == 1 )
9252 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009254 return( 1 );
9255 }
9256
9257 /*
9258 * Case B: Further records are pending in the current datagram.
9259 */
9260
9261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009262 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009263 ssl->in_left > ssl->next_record_offset )
9264 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009266 return( 1 );
9267 }
9268#endif /* MBEDTLS_SSL_PROTO_DTLS */
9269
9270 /*
9271 * Case C: A handshake message is being processed.
9272 */
9273
Hanno Becker8b170a02017-10-10 11:51:19 +01009274 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9275 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009277 return( 1 );
9278 }
9279
9280 /*
9281 * Case D: An application data message is being processed
9282 */
9283 if( ssl->in_offt != NULL )
9284 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009286 return( 1 );
9287 }
9288
9289 /*
9290 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009291 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009292 * we implement support for multiple alerts in single records.
9293 */
9294
9295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9296 return( 0 );
9297}
9298
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009299uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009300{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009301 if( ssl->session != NULL )
9302 return( ssl->session->verify_result );
9303
9304 if( ssl->session_negotiate != NULL )
9305 return( ssl->session_negotiate->verify_result );
9306
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009307 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009308}
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009311{
Hanno Beckere02758c2019-06-26 15:31:31 +01009312 int suite;
9313
Paul Bakker926c8e42013-03-06 10:23:34 +01009314 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009315 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009316
Hanno Beckere02758c2019-06-26 15:31:31 +01009317 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9318 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009319}
9320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009322{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009324 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009325 {
Hanno Becker2881d802019-05-22 14:44:53 +01009326 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009329 return( "DTLSv1.0" );
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009332 return( "DTLSv1.2" );
9333
9334 default:
9335 return( "unknown (DTLS)" );
9336 }
9337 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009338 MBEDTLS_SSL_TRANSPORT_ELSE
9339#endif /* MBEDTLS_SSL_PROTO_DTLS */
9340#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009341 {
Hanno Becker2881d802019-05-22 14:44:53 +01009342 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009343 {
9344 case MBEDTLS_SSL_MINOR_VERSION_0:
9345 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009346
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009347 case MBEDTLS_SSL_MINOR_VERSION_1:
9348 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009349
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009350 case MBEDTLS_SSL_MINOR_VERSION_2:
9351 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009352
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009353 case MBEDTLS_SSL_MINOR_VERSION_3:
9354 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009355
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009356 default:
9357 return( "unknown" );
9358 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009359 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009360#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009361}
9362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009364{
Hanno Becker3136ede2018-08-17 15:28:19 +01009365 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009367
Hanno Becker43395762019-05-03 14:46:38 +01009368 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9369
Hanno Becker78640902018-08-13 16:35:15 +01009370 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009371 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373#if defined(MBEDTLS_ZLIB_SUPPORT)
9374 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9375 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009376#endif
9377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009379 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009380#if defined(MBEDTLS_GCM_C) || \
9381 defined(MBEDTLS_CCM_C) || \
9382 defined(MBEDTLS_CHACHAPOLY_C)
9383#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009385#endif
9386#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009388#endif
9389#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009390 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009391#endif
9392 transform_expansion =
9393 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009394 break;
9395
Hanno Beckera9d5c452019-07-25 16:47:12 +01009396#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9397 MBEDTLS_CHACHAPOLY_C */
9398
9399#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9400 case MBEDTLS_MODE_STREAM:
9401 transform_expansion = transform->maclen;
9402 break;
9403#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9404
9405#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009407 {
9408 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009409
9410 block_size = mbedtls_cipher_get_block_size(
9411 &transform->cipher_ctx_enc );
9412
Hanno Becker3136ede2018-08-17 15:28:19 +01009413 /* Expansion due to the addition of the MAC. */
9414 transform_expansion += transform->maclen;
9415
9416 /* Expansion due to the addition of CBC padding;
9417 * Theoretically up to 256 bytes, but we never use
9418 * more than the block size of the underlying cipher. */
9419 transform_expansion += block_size;
9420
9421 /* For TLS 1.1 or higher, an explicit IV is added
9422 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009423#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009424 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009425 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009426#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009427
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009428 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009429 }
9430#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009431
9432 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009435 }
9436
Hanno Beckera5a2b082019-05-15 14:03:01 +01009437#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009438 if( transform->out_cid_len != 0 )
9439 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009440#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009441
Hanno Becker43395762019-05-03 14:46:38 +01009442 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009443}
9444
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009445#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9446size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9447{
9448 size_t max_len;
9449
9450 /*
9451 * Assume mfl_code is correct since it was checked when set
9452 */
Angus Grattond8213d02016-05-25 20:56:48 +10009453 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009454
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009455 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009456 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009457 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009458 {
Angus Grattond8213d02016-05-25 20:56:48 +10009459 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009460 }
9461
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009462 /* During a handshake, use the value being negotiated */
9463 if( ssl->session_negotiate != NULL &&
9464 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9465 {
9466 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9467 }
9468
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009469 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009470}
9471#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9472
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009473#if defined(MBEDTLS_SSL_PROTO_DTLS)
9474static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9475{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009476 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009477 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009478 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9479 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9480 return ( 0 );
9481
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009482 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9483 return( ssl->mtu );
9484
9485 if( ssl->mtu == 0 )
9486 return( ssl->handshake->mtu );
9487
9488 return( ssl->mtu < ssl->handshake->mtu ?
9489 ssl->mtu : ssl->handshake->mtu );
9490}
9491#endif /* MBEDTLS_SSL_PROTO_DTLS */
9492
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009493int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9494{
9495 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9496
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009497#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9498 !defined(MBEDTLS_SSL_PROTO_DTLS)
9499 (void) ssl;
9500#endif
9501
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009502#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9503 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9504
9505 if( max_len > mfl )
9506 max_len = mfl;
9507#endif
9508
9509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009510 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009511 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009512 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009513 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9514 const size_t overhead = (size_t) ret;
9515
9516 if( ret < 0 )
9517 return( ret );
9518
9519 if( mtu <= overhead )
9520 {
9521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9522 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9523 }
9524
9525 if( max_len > mtu - overhead )
9526 max_len = mtu - overhead;
9527 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009528#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009529
Hanno Becker0defedb2018-08-10 12:35:02 +01009530#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9531 !defined(MBEDTLS_SSL_PROTO_DTLS)
9532 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009533#endif
9534
9535 return( (int) max_len );
9536}
9537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538#if defined(MBEDTLS_X509_CRT_PARSE_C)
9539const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009540{
9541 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009542 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009543
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009544#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009545 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009546#else
9547 return( NULL );
9548#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009553int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9554 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009555{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009556 if( ssl == NULL ||
9557 dst == NULL ||
9558 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009559 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009562 }
9563
Hanno Becker58fccf22019-02-06 14:30:46 +00009564 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009565}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009567
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009568const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9569{
9570 if( ssl == NULL )
9571 return( NULL );
9572
9573 return( ssl->session );
9574}
9575
Paul Bakker5121ce52009-01-03 21:22:43 +00009576/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009577 * Define ticket header determining Mbed TLS version
9578 * and structure of the ticket.
9579 */
9580
Hanno Becker41527622019-05-16 12:50:45 +01009581/*
Hanno Becker26829e92019-05-28 14:30:45 +01009582 * Define bitflag determining compile-time settings influencing
9583 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009584 */
9585
Hanno Becker26829e92019-05-28 14:30:45 +01009586#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009587#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009588#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009589#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009590#endif /* MBEDTLS_HAVE_TIME */
9591
9592#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009593#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009594#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009595#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009596#endif /* MBEDTLS_X509_CRT_PARSE_C */
9597
9598#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009599#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009600#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009601#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009602#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9603
9604#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009605#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009606#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009607#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009608#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9609
9610#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009611#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009612#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009613#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009614#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9615
9616#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009617#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009618#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009619#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009620#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9621
Hanno Becker41527622019-05-16 12:50:45 +01009622#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9623#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9624#else
9625#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9626#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9627
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009628#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9629#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9630#else
9631#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9632#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9633
Hanno Becker88440552019-07-03 14:16:13 +01009634#if defined(MBEDTLS_ZLIB_SUPPORT)
9635#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9636#else
9637#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9638#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9639
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009640#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9641#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9642#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9643#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9644#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9645#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9646#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009647#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009648#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009649
Hanno Becker26829e92019-05-28 14:30:45 +01009650#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009651 ( (uint16_t) ( \
9652 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9653 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9654 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9655 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9656 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9657 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009658 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009659 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009660 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009661
Hanno Becker557fe9f2019-05-16 12:41:07 +01009662static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009663 MBEDTLS_VERSION_MAJOR,
9664 MBEDTLS_VERSION_MINOR,
9665 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009666 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9667 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009668};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009669
9670/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009671 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009672 * (in the presentation language of TLS, RFC 8446 section 3)
9673 *
Hanno Becker26829e92019-05-28 14:30:45 +01009674 * opaque mbedtls_version[3]; // major, minor, patch
9675 * opaque session_format[2]; // version-specific 16-bit field determining
9676 * // the format of the remaining
9677 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009678 *
9679 * Note: When updating the format, remember to keep
9680 * these version+format bytes.
9681 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009682 * // In this version, `session_format` determines
9683 * // the setting of those compile-time
9684 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009685 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009686 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009687 * uint8 ciphersuite[2]; // defined by the standard
9688 * uint8 compression; // 0 or 1
9689 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009690 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009691 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009692 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009693 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9694 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9695 * case disabled: uint8_t peer_cert_digest_type;
9696 * opaque peer_cert_digest<0..2^8-1>;
9697 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009698 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009699 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009700 * uint8 mfl_code; // up to 255 according to standard
9701 * uint8 trunc_hmac; // 0 or 1
9702 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009703 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009704 * The order is the same as in the definition of the structure, except
9705 * verify_result is put before peer_cert so that all mandatory fields come
9706 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009707 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009708static int ssl_session_save( const mbedtls_ssl_session *session,
9709 unsigned char omit_header,
9710 unsigned char *buf,
9711 size_t buf_len,
9712 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009713{
9714 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009715 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009716#if defined(MBEDTLS_HAVE_TIME)
9717 uint64_t start;
9718#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009719#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009720#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009721 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009722#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009723#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009724
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009725 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009726 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009727 /*
9728 * Add version identifier
9729 */
9730
9731 used += sizeof( ssl_serialized_session_header );
9732
9733 if( used <= buf_len )
9734 {
9735 memcpy( p, ssl_serialized_session_header,
9736 sizeof( ssl_serialized_session_header ) );
9737 p += sizeof( ssl_serialized_session_header );
9738 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009739 }
9740
9741 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009742 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009743 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009744#if defined(MBEDTLS_HAVE_TIME)
9745 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009746
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009747 if( used <= buf_len )
9748 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009749 start = (uint64_t) session->start;
9750
9751 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9752 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9753 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9754 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9755 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9756 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9757 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9758 *p++ = (unsigned char)( ( start ) & 0xFF );
9759 }
9760#endif /* MBEDTLS_HAVE_TIME */
9761
9762 /*
9763 * Basic mandatory fields
9764 */
Hanno Becker88440552019-07-03 14:16:13 +01009765 {
9766 size_t const ciphersuite_len = 2;
9767#if defined(MBEDTLS_ZLIB_SUPPORT)
9768 size_t const compression_len = 1;
9769#else
9770 size_t const compression_len = 0;
9771#endif
9772 size_t const id_len_len = 1;
9773 size_t const id_len = 32;
9774 size_t const master_len = 48;
9775 size_t const verif_result_len = 4;
9776
9777 size_t const basic_len =
9778 ciphersuite_len +
9779 compression_len +
9780 id_len_len +
9781 id_len +
9782 master_len +
9783 verif_result_len;
9784
9785 used += basic_len;
9786 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009787
9788 if( used <= buf_len )
9789 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009790 const int ciphersuite =
9791 mbedtls_ssl_session_get_ciphersuite( session );
9792 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9793 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009794
Hanno Becker88440552019-07-03 14:16:13 +01009795#if defined(MBEDTLS_ZLIB_SUPPORT)
9796 *p++ = (unsigned char)(
9797 mbedtls_ssl_session_get_compression( session ) );
9798#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009799
9800 *p++ = (unsigned char)( session->id_len & 0xFF );
9801 memcpy( p, session->id, 32 );
9802 p += 32;
9803
9804 memcpy( p, session->master, 48 );
9805 p += 48;
9806
9807 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9808 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9809 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9810 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009811 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009812
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009813 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009814 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009815 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009816#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009817#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009818 if( session->peer_cert == NULL )
9819 cert_len = 0;
9820 else
9821 cert_len = session->peer_cert->raw.len;
9822
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009823 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009824
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009825 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009826 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009827 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9828 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9829 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9830
9831 if( session->peer_cert != NULL )
9832 {
9833 memcpy( p, session->peer_cert->raw.p, cert_len );
9834 p += cert_len;
9835 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009836 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009837
Hanno Becker5882dd02019-06-06 16:25:57 +01009838#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009839 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009840 if( session->peer_cert_digest != NULL )
9841 {
9842 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9843 if( used <= buf_len )
9844 {
9845 *p++ = (unsigned char) session->peer_cert_digest_type;
9846 *p++ = (unsigned char) session->peer_cert_digest_len;
9847 memcpy( p, session->peer_cert_digest,
9848 session->peer_cert_digest_len );
9849 p += session->peer_cert_digest_len;
9850 }
9851 }
9852 else
9853 {
9854 used += 2;
9855 if( used <= buf_len )
9856 {
9857 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9858 *p++ = 0;
9859 }
9860 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009861#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009862#endif /* MBEDTLS_X509_CRT_PARSE_C */
9863
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009864 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009865 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009866 */
9867#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009868 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009869
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009870 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009871 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009872 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9873 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9874 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9875
9876 if( session->ticket != NULL )
9877 {
9878 memcpy( p, session->ticket, session->ticket_len );
9879 p += session->ticket_len;
9880 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009881
9882 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9883 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9884 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9885 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009886 }
9887#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9888
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009889 /*
9890 * Misc extension-related info
9891 */
9892#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9893 used += 1;
9894
9895 if( used <= buf_len )
9896 *p++ = session->mfl_code;
9897#endif
9898
9899#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9900 used += 1;
9901
9902 if( used <= buf_len )
9903 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9904#endif
9905
9906#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9907 used += 1;
9908
9909 if( used <= buf_len )
9910 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9911#endif
9912
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009913 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009914 *olen = used;
9915
9916 if( used > buf_len )
9917 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009918
9919 return( 0 );
9920}
9921
9922/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009923 * Public wrapper for ssl_session_save()
9924 */
9925int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9926 unsigned char *buf,
9927 size_t buf_len,
9928 size_t *olen )
9929{
9930 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9931}
9932
9933/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009934 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009935 *
9936 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009937 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009938 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009939static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009940 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009941 const unsigned char *buf,
9942 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009943{
9944 const unsigned char *p = buf;
9945 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009946 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009947#if defined(MBEDTLS_HAVE_TIME)
9948 uint64_t start;
9949#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009950#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009951#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009952 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009953#endif
9954#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009955
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009956 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009957 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009958 /*
9959 * Check version identifier
9960 */
9961
9962 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9964
9965 if( memcmp( p, ssl_serialized_session_header,
9966 sizeof( ssl_serialized_session_header ) ) != 0 )
9967 {
9968 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9969 }
9970 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009971 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009972
9973 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009974 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009975 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009976#if defined(MBEDTLS_HAVE_TIME)
9977 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9979
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009980 start = ( (uint64_t) p[0] << 56 ) |
9981 ( (uint64_t) p[1] << 48 ) |
9982 ( (uint64_t) p[2] << 40 ) |
9983 ( (uint64_t) p[3] << 32 ) |
9984 ( (uint64_t) p[4] << 24 ) |
9985 ( (uint64_t) p[5] << 16 ) |
9986 ( (uint64_t) p[6] << 8 ) |
9987 ( (uint64_t) p[7] );
9988 p += 8;
9989
9990 session->start = (time_t) start;
9991#endif /* MBEDTLS_HAVE_TIME */
9992
9993 /*
9994 * Basic mandatory fields
9995 */
Hanno Becker88440552019-07-03 14:16:13 +01009996 {
9997 size_t const ciphersuite_len = 2;
9998#if defined(MBEDTLS_ZLIB_SUPPORT)
9999 size_t const compression_len = 1;
10000#else
10001 size_t const compression_len = 0;
10002#endif
10003 size_t const id_len_len = 1;
10004 size_t const id_len = 32;
10005 size_t const master_len = 48;
10006 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010007
Hanno Becker88440552019-07-03 14:16:13 +010010008 size_t const basic_len =
10009 ciphersuite_len +
10010 compression_len +
10011 id_len_len +
10012 id_len +
10013 master_len +
10014 verif_result_len;
10015
10016 if( basic_len > (size_t)( end - p ) )
10017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10018 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010019
Hanno Beckere02758c2019-06-26 15:31:31 +010010020 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010021 p += 2;
10022
Hanno Becker73f4cb12019-06-27 13:51:07 +010010023#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010024 session->ciphersuite = ciphersuite;
10025#else
10026 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010027 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010028 {
10029 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10030 }
10031#endif
10032
Hanno Becker88440552019-07-03 14:16:13 +010010033#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010034 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010035#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010036
10037 session->id_len = *p++;
10038 memcpy( session->id, p, 32 );
10039 p += 32;
10040
10041 memcpy( session->master, p, 48 );
10042 p += 48;
10043
10044 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10045 ( (uint32_t) p[1] << 16 ) |
10046 ( (uint32_t) p[2] << 8 ) |
10047 ( (uint32_t) p[3] );
10048 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010049
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010050 /* Immediately clear invalid pointer values that have been read, in case
10051 * we exit early before we replaced them with valid ones. */
10052#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010053#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010054 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010055#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010056 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010057#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010058#endif
10059#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10060 session->ticket = NULL;
10061#endif
10062
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010063 /*
10064 * Peer certificate
10065 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010066#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010067#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010068 if( 3 > (size_t)( end - p ) )
10069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10070
10071 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10072 p += 3;
10073
10074 if( cert_len == 0 )
10075 {
10076 session->peer_cert = NULL;
10077 }
10078 else
10079 {
10080 int ret;
10081
10082 if( cert_len > (size_t)( end - p ) )
10083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10084
10085 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10086
10087 if( session->peer_cert == NULL )
10088 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10089
10090 mbedtls_x509_crt_init( session->peer_cert );
10091
10092 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10093 p, cert_len ) ) != 0 )
10094 {
10095 mbedtls_x509_crt_free( session->peer_cert );
10096 mbedtls_free( session->peer_cert );
10097 session->peer_cert = NULL;
10098 return( ret );
10099 }
10100
10101 p += cert_len;
10102 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010103#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010104 /* Deserialize CRT digest from the end of the ticket. */
10105 if( 2 > (size_t)( end - p ) )
10106 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10107
10108 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10109 session->peer_cert_digest_len = (size_t) *p++;
10110
10111 if( session->peer_cert_digest_len != 0 )
10112 {
Hanno Becker2326d202019-06-06 14:54:55 +010010113 const mbedtls_md_info_t *md_info =
10114 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10115 if( md_info == NULL )
10116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10117 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10119
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010120 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10121 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10122
10123 session->peer_cert_digest =
10124 mbedtls_calloc( 1, session->peer_cert_digest_len );
10125 if( session->peer_cert_digest == NULL )
10126 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10127
10128 memcpy( session->peer_cert_digest, p,
10129 session->peer_cert_digest_len );
10130 p += session->peer_cert_digest_len;
10131 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010132#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010133#endif /* MBEDTLS_X509_CRT_PARSE_C */
10134
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010135 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010136 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010137 */
10138#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10139 if( 3 > (size_t)( end - p ) )
10140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10141
10142 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10143 p += 3;
10144
10145 if( session->ticket_len != 0 )
10146 {
10147 if( session->ticket_len > (size_t)( end - p ) )
10148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10149
10150 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10151 if( session->ticket == NULL )
10152 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10153
10154 memcpy( session->ticket, p, session->ticket_len );
10155 p += session->ticket_len;
10156 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010157
10158 if( 4 > (size_t)( end - p ) )
10159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10160
10161 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10162 ( (uint32_t) p[1] << 16 ) |
10163 ( (uint32_t) p[2] << 8 ) |
10164 ( (uint32_t) p[3] );
10165 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010166#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10167
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010168 /*
10169 * Misc extension-related info
10170 */
10171#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10172 if( 1 > (size_t)( end - p ) )
10173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10174
10175 session->mfl_code = *p++;
10176#endif
10177
10178#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10179 if( 1 > (size_t)( end - p ) )
10180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10181
10182 session->trunc_hmac = *p++;
10183#endif
10184
10185#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10186 if( 1 > (size_t)( end - p ) )
10187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10188
10189 session->encrypt_then_mac = *p++;
10190#endif
10191
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010192 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010193 if( p != end )
10194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10195
10196 return( 0 );
10197}
10198
10199/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010200 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010201 */
10202int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10203 const unsigned char *buf,
10204 size_t len )
10205{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010206 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010207
10208 if( ret != 0 )
10209 mbedtls_ssl_session_free( session );
10210
10211 return( ret );
10212}
10213
10214/*
Paul Bakker1961b702013-01-25 14:49:24 +010010215 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010220
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010221 if( ssl == NULL || ssl->conf == NULL )
10222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010225 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010229 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010231#endif
10232
Hanno Beckerb82350b2019-07-26 07:24:05 +010010233 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010234 return( ret );
10235}
10236
10237/*
10238 * Perform the SSL handshake
10239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010241{
10242 int ret = 0;
10243
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010244 if( ssl == NULL || ssl->conf == NULL )
10245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010252
10253 if( ret != 0 )
10254 break;
10255 }
10256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010258
10259 return( ret );
10260}
10261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262#if defined(MBEDTLS_SSL_RENEGOTIATION)
10263#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010264/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010265 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010268{
10269 int ret;
10270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010272
10273 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10275 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010276
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010277 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010278 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010280 return( ret );
10281 }
10282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010284
10285 return( 0 );
10286}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010288
10289/*
10290 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291 * - any side: calling mbedtls_ssl_renegotiate(),
10292 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10293 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010294 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010295 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010296 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010299{
10300 int ret;
10301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010303
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010304 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10305 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010306
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010307 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10308 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010310 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010312 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010313 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10314 MBEDTLS_SSL_IS_SERVER )
10315 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010316 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010317 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010318 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010319 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010320 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010321 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010322 }
10323#endif
10324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10326 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010331 return( ret );
10332 }
10333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010335
10336 return( 0 );
10337}
10338
10339/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010340 * Renegotiate current connection on client,
10341 * or request renegotiation on server
10342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010346
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010347 if( ssl == NULL || ssl->conf == NULL )
10348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010350#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010351 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010352 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010357 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010358
10359 /* Did we already try/start sending HelloRequest? */
10360 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010362
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010363 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010364 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010368 /*
10369 * On client, either start the renegotiation process or,
10370 * if already in progress, continue the handshake
10371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010376
10377 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010380 return( ret );
10381 }
10382 }
10383 else
10384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010388 return( ret );
10389 }
10390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010392
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010393 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010394}
10395
10396/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010397 * Check record counters and renegotiate if they're above the limit.
10398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010400{
Andres AG2196c7f2016-12-15 17:01:16 +000010401 size_t ep_len = ssl_ep_len( ssl );
10402 int in_ctr_cmp;
10403 int out_ctr_cmp;
10404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10406 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010407 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010408 {
10409 return( 0 );
10410 }
10411
Andres AG2196c7f2016-12-15 17:01:16 +000010412 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10413 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010414 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010415 ssl->conf->renego_period + ep_len, 8 - ep_len );
10416
10417 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010418 {
10419 return( 0 );
10420 }
10421
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010426
10427/*
10428 * Receive application data decrypted from the SSL layer
10429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010431{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010432 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010433 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010434
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010435 if( ssl == NULL || ssl->conf == NULL )
10436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010441 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010444 return( ret );
10445
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010446 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010448 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010449 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010450 return( ret );
10451 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010452 }
10453#endif
10454
Hanno Becker4a810fb2017-05-24 16:27:30 +010010455 /*
10456 * Check if renegotiation is necessary and/or handshake is
10457 * in process. If yes, perform/continue, and fall through
10458 * if an unexpected packet is received while the client
10459 * is waiting for the ServerHello.
10460 *
10461 * (There is no equivalent to the last condition on
10462 * the server-side as it is not treated as within
10463 * a handshake while waiting for the ClientHello
10464 * after a renegotiation request.)
10465 */
10466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010468 ret = ssl_check_ctr_renegotiate( ssl );
10469 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10470 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010473 return( ret );
10474 }
10475#endif
10476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010477 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010480 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10481 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010484 return( ret );
10485 }
10486 }
10487
Hanno Beckere41158b2017-10-23 13:30:32 +010010488 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010489 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010490 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010491 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010492 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10493 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010494 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010495 ssl_set_timer( ssl,
10496 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010497 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010498
Hanno Becker327c93b2018-08-15 13:56:18 +010010499 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010500 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010501 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10502 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010503
Hanno Becker4a810fb2017-05-24 16:27:30 +010010504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10505 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010506 }
10507
10508 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010510 {
10511 /*
10512 * OpenSSL sends empty messages to randomize the IV
10513 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010514 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010517 return( 0 );
10518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010520 return( ret );
10521 }
10522 }
10523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010524 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010527
Hanno Becker4a810fb2017-05-24 16:27:30 +010010528 /*
10529 * - For client-side, expect SERVER_HELLO_REQUEST.
10530 * - For server-side, expect CLIENT_HELLO.
10531 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10532 */
10533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010534#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010535 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10536 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010538 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010541
10542 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010543#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010544 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010545 {
10546 continue;
10547 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010548 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010549#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010550#if defined(MBEDTLS_SSL_PROTO_TLS)
10551 {
10552 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10553 }
10554#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010555 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010556#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010557
Hanno Becker4a810fb2017-05-24 16:27:30 +010010558#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010559 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10560 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010561 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010564
10565 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010567 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010568 {
10569 continue;
10570 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010571 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010572#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010573#if defined(MBEDTLS_SSL_PROTO_TLS)
10574 {
10575 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10576 }
10577#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010578 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010579#endif /* MBEDTLS_SSL_SRV_C */
10580
Hanno Becker21df7f92017-10-17 11:03:26 +010010581#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010582 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010583 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10584 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010585 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010586 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10587 {
10588 /*
10589 * Accept renegotiation request
10590 */
Paul Bakker48916f92012-09-16 19:57:18 +000010591
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010592 /* DTLS clients need to know renego is server-initiated */
10593#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010594 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010595 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10596 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010597 {
10598 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10599 }
10600#endif
10601 ret = ssl_start_renegotiation( ssl );
10602 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10603 ret != 0 )
10604 {
10605 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10606 return( ret );
10607 }
10608 }
10609 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010610#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010611 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010612 /*
10613 * Refuse renegotiation
10614 */
10615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010618#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010619 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010620 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010621 /* SSLv3 does not have a "no_renegotiation" warning, so
10622 we send a fatal alert and abort the connection. */
10623 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10624 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10625 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010626 }
10627 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010628#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10629#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10630 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010631 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010632 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010633 ret = mbedtls_ssl_send_alert_message( ssl,
10634 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10635 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10636 if( ret != 0 )
10637 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010638 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010639 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010640#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10641 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010645 }
Paul Bakker48916f92012-09-16 19:57:18 +000010646 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010647
Hanno Becker90333da2017-10-10 11:27:13 +010010648 /* At this point, we don't know whether the renegotiation has been
10649 * completed or not. The cases to consider are the following:
10650 * 1) The renegotiation is complete. In this case, no new record
10651 * has been read yet.
10652 * 2) The renegotiation is incomplete because the client received
10653 * an application data record while awaiting the ServerHello.
10654 * 3) The renegotiation is incomplete because the client received
10655 * a non-handshake, non-application data message while awaiting
10656 * the ServerHello.
10657 * In each of these case, looping will be the proper action:
10658 * - For 1), the next iteration will read a new record and check
10659 * if it's application data.
10660 * - For 2), the loop condition isn't satisfied as application data
10661 * is present, hence continue is the same as break
10662 * - For 3), the loop condition is satisfied and read_record
10663 * will re-deliver the message that was held back by the client
10664 * when expecting the ServerHello.
10665 */
10666 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010667 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010668#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010669 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010670 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010671 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010672 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010673 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010676 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010678 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010679 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10684 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010687 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010688 }
10689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010690 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10693 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010694 }
10695
10696 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010697
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010698 /* We're going to return something now, cancel timer,
10699 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010701 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010702
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010703#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010704 /* If we requested renego but received AppData, resend HelloRequest.
10705 * Do it now, after setting in_offt, to avoid taking this branch
10706 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010708 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10709 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010710 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010711 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010712 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010715 return( ret );
10716 }
10717 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010719#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010720 }
10721
10722 n = ( len < ssl->in_msglen )
10723 ? len : ssl->in_msglen;
10724
10725 memcpy( buf, ssl->in_offt, n );
10726 ssl->in_msglen -= n;
10727
10728 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010729 {
10730 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010731 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010732 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010733 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010734 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010735 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010736 /* more data available */
10737 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010738 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010741
Paul Bakker23986e52011-04-24 08:57:21 +000010742 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010743}
10744
10745/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010746 * Send application data to be encrypted by the SSL layer, taking care of max
10747 * fragment length and buffer size.
10748 *
10749 * According to RFC 5246 Section 6.2.1:
10750 *
10751 * Zero-length fragments of Application data MAY be sent as they are
10752 * potentially useful as a traffic analysis countermeasure.
10753 *
10754 * Therefore, it is possible that the input message length is 0 and the
10755 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010756 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010757static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010758 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010759{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010760 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10761 const size_t max_len = (size_t) ret;
10762
10763 if( ret < 0 )
10764 {
10765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10766 return( ret );
10767 }
10768
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010769 if( len > max_len )
10770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010772 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010775 "maximum fragment length: %d > %d",
10776 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010778 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010779 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010780#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010781#if defined(MBEDTLS_SSL_PROTO_TLS)
10782 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010783 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010784 }
10785#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010786 }
Paul Bakker887bd502011-06-08 13:10:54 +000010787
Paul Bakker5121ce52009-01-03 21:22:43 +000010788 if( ssl->out_left != 0 )
10789 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010790 /*
10791 * The user has previously tried to send the data and
10792 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10793 * written. In this case, we expect the high-level write function
10794 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010799 return( ret );
10800 }
10801 }
Paul Bakker887bd502011-06-08 13:10:54 +000010802 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010803 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010804 /*
10805 * The user is trying to send a message the first time, so we need to
10806 * copy the data into the internal buffers and setup the data structure
10807 * to keep track of partial writes
10808 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010809 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010810 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010811 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010812
Hanno Becker67bc7c32018-08-06 11:33:50 +010010813 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010816 return( ret );
10817 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010818 }
10819
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010820 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010821}
10822
10823/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010824 * Write application data, doing 1/n-1 splitting if necessary.
10825 *
10826 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010827 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010828 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010831static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010832 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010833{
10834 int ret;
10835
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010836 if( ssl->conf->cbc_record_splitting ==
10837 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010838 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010839 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010840 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10841 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010842 {
10843 return( ssl_write_real( ssl, buf, len ) );
10844 }
10845
10846 if( ssl->split_done == 0 )
10847 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010848 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010849 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010850 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010851 }
10852
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010853 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10854 return( ret );
10855 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010856
10857 return( ret + 1 );
10858}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010860
10861/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010862 * Write application data (public-facing wrapper)
10863 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010864int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010865{
10866 int ret;
10867
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010869
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010870 if( ssl == NULL || ssl->conf == NULL )
10871 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10872
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010873#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010874 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10875 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010876 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010877 return( ret );
10878 }
10879#endif
10880
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010881 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010882 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010883 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010884 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010886 return( ret );
10887 }
10888 }
10889
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010890#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010891 ret = ssl_write_split( ssl, buf, len );
10892#else
10893 ret = ssl_write_real( ssl, buf, len );
10894#endif
10895
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010897
10898 return( ret );
10899}
10900
10901/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010902 * Notify the peer that the connection is being closed
10903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010904int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010905{
10906 int ret;
10907
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010908 if( ssl == NULL || ssl->conf == NULL )
10909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010912
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010913 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010918 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10919 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10920 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010923 return( ret );
10924 }
10925 }
10926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010928
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010929 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010930}
10931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010933{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010934 if( transform == NULL )
10935 return;
10936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010937#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010938 deflateEnd( &transform->ctx_deflate );
10939 inflateEnd( &transform->ctx_inflate );
10940#endif
10941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010942 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10943 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010944
Hanno Becker92231322018-01-03 15:32:51 +000010945#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946 mbedtls_md_free( &transform->md_ctx_enc );
10947 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010948#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010949
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010950 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010951}
10952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953#if defined(MBEDTLS_X509_CRT_PARSE_C)
10954static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010957
10958 while( cur != NULL )
10959 {
10960 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010961 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010962 cur = next;
10963 }
10964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010965#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010966
Hanno Becker0271f962018-08-16 13:23:47 +010010967#if defined(MBEDTLS_SSL_PROTO_DTLS)
10968
10969static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10970{
10971 unsigned offset;
10972 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10973
10974 if( hs == NULL )
10975 return;
10976
Hanno Becker283f5ef2018-08-24 09:34:47 +010010977 ssl_free_buffered_record( ssl );
10978
Hanno Becker0271f962018-08-16 13:23:47 +010010979 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010980 ssl_buffering_free_slot( ssl, offset );
10981}
10982
10983static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10984 uint8_t slot )
10985{
10986 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10987 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010988
10989 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10990 return;
10991
Hanno Beckere605b192018-08-21 15:59:07 +010010992 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010993 {
Hanno Beckere605b192018-08-21 15:59:07 +010010994 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010995 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010996 mbedtls_free( hs_buf->data );
10997 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010998 }
10999}
11000
11001#endif /* MBEDTLS_SSL_PROTO_DTLS */
11002
Gilles Peskine9b562d52018-04-25 20:32:43 +020011003void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011004{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011005 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11006
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011007 if( handshake == NULL )
11008 return;
11009
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011010#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11011 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11012 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011013 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011014 handshake->async_in_progress = 0;
11015 }
11016#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11017
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011018#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11019 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11020 mbedtls_md5_free( &handshake->fin_md5 );
11021 mbedtls_sha1_free( &handshake->fin_sha1 );
11022#endif
11023#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11024#if defined(MBEDTLS_SHA256_C)
11025 mbedtls_sha256_free( &handshake->fin_sha256 );
11026#endif
11027#if defined(MBEDTLS_SHA512_C)
11028 mbedtls_sha512_free( &handshake->fin_sha512 );
11029#endif
11030#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032#if defined(MBEDTLS_DHM_C)
11033 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035#if defined(MBEDTLS_ECDH_C)
11036 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011037#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011038#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011039 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011040#if defined(MBEDTLS_SSL_CLI_C)
11041 mbedtls_free( handshake->ecjpake_cache );
11042 handshake->ecjpake_cache = NULL;
11043 handshake->ecjpake_cache_len = 0;
11044#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011045#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011046
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011047#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11048 if( handshake->psk != NULL )
11049 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011050 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011051 mbedtls_free( handshake->psk );
11052 }
11053#endif
11054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011055#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11056 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011057 /*
11058 * Free only the linked list wrapper, not the keys themselves
11059 * since the belong to the SNI callback
11060 */
11061 if( handshake->sni_key_cert != NULL )
11062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011063 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011064
11065 while( cur != NULL )
11066 {
11067 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011069 cur = next;
11070 }
11071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011073
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011074#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011075 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011076 if( handshake->ecrs_peer_cert != NULL )
11077 {
11078 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11079 mbedtls_free( handshake->ecrs_peer_cert );
11080 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011081#endif
11082
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011083#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11084 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11085 mbedtls_pk_free( &handshake->peer_pubkey );
11086#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011088#if defined(MBEDTLS_SSL_PROTO_DTLS)
11089 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011090 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011091 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011092#endif
11093
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011094 mbedtls_platform_zeroize( handshake,
11095 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011096}
11097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011098void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011099{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011100 if( session == NULL )
11101 return;
11102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011103#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011104 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011105#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011106
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011107#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011108 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011109#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011110
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011111 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011112}
11113
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011114#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011115
11116#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11117#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11118#else
11119#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11120#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11121
11122#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11123#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11124#else
11125#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11126#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11127
11128#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11129#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11130#else
11131#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11132#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11133
11134#if defined(MBEDTLS_SSL_ALPN)
11135#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11136#else
11137#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11138#endif /* MBEDTLS_SSL_ALPN */
11139
11140#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11141#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11142#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11143#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11144
11145#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11146 ( (uint32_t) ( \
11147 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11148 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11149 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11150 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11151 0u ) )
11152
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011153static unsigned char ssl_serialized_context_header[] = {
11154 MBEDTLS_VERSION_MAJOR,
11155 MBEDTLS_VERSION_MINOR,
11156 MBEDTLS_VERSION_PATCH,
11157 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11158 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011159 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11160 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11161 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011162};
11163
Paul Bakker5121ce52009-01-03 21:22:43 +000011164/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011165 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011166 *
11167 * The format of the serialized data is:
11168 * (in the presentation language of TLS, RFC 8446 section 3)
11169 *
11170 * // header
11171 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011172 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011173 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011174 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011175 * Note: When updating the format, remember to keep these
11176 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011177 *
11178 * // session sub-structure
11179 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11180 * // transform sub-structure
11181 * uint8 random[64]; // ServerHello.random+ClientHello.random
11182 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11183 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11184 * // fields from ssl_context
11185 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11186 * uint64 in_window_top; // DTLS: last validated record seq_num
11187 * uint64 in_window; // DTLS: bitmask for replay protection
11188 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11189 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11190 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11191 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11192 *
11193 * Note that many fields of the ssl_context or sub-structures are not
11194 * serialized, as they fall in one of the following categories:
11195 *
11196 * 1. forced value (eg in_left must be 0)
11197 * 2. pointer to dynamically-allocated memory (eg session, transform)
11198 * 3. value can be re-derived from other data (eg session keys from MS)
11199 * 4. value was temporary (eg content of input buffer)
11200 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011201 */
11202int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11203 unsigned char *buf,
11204 size_t buf_len,
11205 size_t *olen )
11206{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011207 unsigned char *p = buf;
11208 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011209 size_t session_len;
11210 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011211
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011212 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011213 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11214 * this function's documentation.
11215 *
11216 * These are due to assumptions/limitations in the implementation. Some of
11217 * them are likely to stay (no handshake in progress) some might go away
11218 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011219 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011220 /* The initial handshake must be over */
11221 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011223 if( ssl->handshake != NULL )
11224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11225 /* Double-check that sub-structures are indeed ready */
11226 if( ssl->transform == NULL || ssl->session == NULL )
11227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11228 /* There must be no pending incoming or outgoing data */
11229 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11231 if( ssl->out_left != 0 )
11232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11233 /* Protocol must be DLTS, not TLS */
11234 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11236 /* Version must be 1.2 */
11237 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11239 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11241 /* We must be using an AEAD ciphersuite */
11242 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11244 /* Renegotiation must not be enabled */
11245 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011247
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011248 /*
11249 * Version and format identifier
11250 */
11251 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011252
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011253 if( used <= buf_len )
11254 {
11255 memcpy( p, ssl_serialized_context_header,
11256 sizeof( ssl_serialized_context_header ) );
11257 p += sizeof( ssl_serialized_context_header );
11258 }
11259
11260 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011261 * Session (length + data)
11262 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011263 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011264 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11265 return( ret );
11266
11267 used += 4 + session_len;
11268 if( used <= buf_len )
11269 {
11270 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11271 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11272 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11273 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11274
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011275 ret = ssl_session_save( ssl->session, 1,
11276 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011277 if( ret != 0 )
11278 return( ret );
11279
11280 p += session_len;
11281 }
11282
11283 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011284 * Transform
11285 */
11286 used += sizeof( ssl->transform->randbytes );
11287 if( used <= buf_len )
11288 {
11289 memcpy( p, ssl->transform->randbytes,
11290 sizeof( ssl->transform->randbytes ) );
11291 p += sizeof( ssl->transform->randbytes );
11292 }
11293
11294#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11295 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11296 if( used <= buf_len )
11297 {
11298 *p++ = ssl->transform->in_cid_len;
11299 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11300 p += ssl->transform->in_cid_len;
11301
11302 *p++ = ssl->transform->out_cid_len;
11303 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11304 p += ssl->transform->out_cid_len;
11305 }
11306#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11307
11308 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011309 * Saved fields from top-level ssl_context structure
11310 */
11311#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11312 used += 4;
11313 if( used <= buf_len )
11314 {
11315 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11316 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11317 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11318 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11319 }
11320#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11321
11322#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11323 used += 16;
11324 if( used <= buf_len )
11325 {
11326 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11327 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11328 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11329 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11330 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11331 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11332 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11333 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11334
11335 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11336 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11337 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11338 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11339 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11340 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11341 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11342 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11343 }
11344#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11345
11346#if defined(MBEDTLS_SSL_PROTO_DTLS)
11347 used += 1;
11348 if( used <= buf_len )
11349 {
11350 *p++ = ssl->disable_datagram_packing;
11351 }
11352#endif /* MBEDTLS_SSL_PROTO_DTLS */
11353
11354 used += 8;
11355 if( used <= buf_len )
11356 {
11357 memcpy( p, ssl->cur_out_ctr, 8 );
11358 p += 8;
11359 }
11360
11361#if defined(MBEDTLS_SSL_PROTO_DTLS)
11362 used += 2;
11363 if( used <= buf_len )
11364 {
11365 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11366 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11367 }
11368#endif /* MBEDTLS_SSL_PROTO_DTLS */
11369
11370#if defined(MBEDTLS_SSL_ALPN)
11371 {
11372 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011373 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011374 : 0;
11375
11376 used += 1 + alpn_len;
11377 if( used <= buf_len )
11378 {
11379 *p++ = alpn_len;
11380
11381 if( ssl->alpn_chosen != NULL )
11382 {
11383 memcpy( p, ssl->alpn_chosen, alpn_len );
11384 p += alpn_len;
11385 }
11386 }
11387 }
11388#endif /* MBEDTLS_SSL_ALPN */
11389
11390 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011391 * Done
11392 */
11393 *olen = used;
11394
11395 if( used > buf_len )
11396 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011397
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011398 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11399
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011400 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011401}
11402
11403/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011404 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011405 *
11406 * This internal version is wrapped by a public function that cleans up in
11407 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011408 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011409static int ssl_context_load( mbedtls_ssl_context *ssl,
11410 const unsigned char *buf,
11411 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011412{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011413 const unsigned char *p = buf;
11414 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011415 size_t session_len;
11416 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011417
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011418 /*
11419 * The context should have been freshly setup or reset.
11420 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011421 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011422 * renegotiating, or if the user mistakenly loaded a session first.)
11423 */
11424 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11425 ssl->session != NULL )
11426 {
11427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11428 }
11429
11430 /*
11431 * We can't check that the config matches the initial one, but we can at
11432 * least check it matches the requirements for serializing.
11433 */
11434 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011435 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11436 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11437 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11438 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11439 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11440 MBEDTLS_SSL_MINOR_VERSION_3 ||
11441 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11442 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011443 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011444 {
11445 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11446 }
11447
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011448 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11449
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011450 /*
11451 * Check version identifier
11452 */
11453 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11454 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11455
11456 if( memcmp( p, ssl_serialized_context_header,
11457 sizeof( ssl_serialized_context_header ) ) != 0 )
11458 {
11459 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11460 }
11461 p += sizeof( ssl_serialized_context_header );
11462
11463 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011464 * Session
11465 */
11466 if( (size_t)( end - p ) < 4 )
11467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11468
11469 session_len = ( (size_t) p[0] << 24 ) |
11470 ( (size_t) p[1] << 16 ) |
11471 ( (size_t) p[2] << 8 ) |
11472 ( (size_t) p[3] );
11473 p += 4;
11474
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011475 /* This has been allocated by ssl_handshake_init(), called by
11476 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11477 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011478 ssl->session_in = ssl->session;
11479 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011480 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011481
11482 if( (size_t)( end - p ) < session_len )
11483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11484
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011485 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011486 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011487 {
11488 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011489 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011490 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011491
11492 p += session_len;
11493
11494 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011495 * Transform
11496 */
11497
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011498 /* This has been allocated by ssl_handshake_init(), called by
11499 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11500 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011501 ssl->transform_in = ssl->transform;
11502 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011503 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011504
11505 /* Read random bytes and populate structure */
11506 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11508
11509 ret = ssl_populate_transform( ssl->transform,
11510 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11511 ssl->session->master,
11512#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11513#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11514 ssl->session->encrypt_then_mac,
11515#endif
11516#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11517 ssl->session->trunc_hmac,
11518#endif
11519#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11520#if defined(MBEDTLS_ZLIB_SUPPORT)
11521 ssl->session->compression,
11522#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011523 p, /* currently pointing to randbytes */
11524 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11525 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11526 ssl );
11527 if( ret != 0 )
11528 return( ret );
11529
11530 p += sizeof( ssl->transform->randbytes );
11531
11532#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11533 /* Read connection IDs and store them */
11534 if( (size_t)( end - p ) < 1 )
11535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11536
11537 ssl->transform->in_cid_len = *p++;
11538
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011539 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11541
11542 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11543 p += ssl->transform->in_cid_len;
11544
11545 ssl->transform->out_cid_len = *p++;
11546
11547 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11549
11550 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11551 p += ssl->transform->out_cid_len;
11552#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11553
11554 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011555 * Saved fields from top-level ssl_context structure
11556 */
11557#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11558 if( (size_t)( end - p ) < 4 )
11559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11560
11561 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11562 ( (uint32_t) p[1] << 16 ) |
11563 ( (uint32_t) p[2] << 8 ) |
11564 ( (uint32_t) p[3] );
11565 p += 4;
11566#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11567
11568#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11569 if( (size_t)( end - p ) < 16 )
11570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11571
11572 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11573 ( (uint64_t) p[1] << 48 ) |
11574 ( (uint64_t) p[2] << 40 ) |
11575 ( (uint64_t) p[3] << 32 ) |
11576 ( (uint64_t) p[4] << 24 ) |
11577 ( (uint64_t) p[5] << 16 ) |
11578 ( (uint64_t) p[6] << 8 ) |
11579 ( (uint64_t) p[7] );
11580 p += 8;
11581
11582 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11583 ( (uint64_t) p[1] << 48 ) |
11584 ( (uint64_t) p[2] << 40 ) |
11585 ( (uint64_t) p[3] << 32 ) |
11586 ( (uint64_t) p[4] << 24 ) |
11587 ( (uint64_t) p[5] << 16 ) |
11588 ( (uint64_t) p[6] << 8 ) |
11589 ( (uint64_t) p[7] );
11590 p += 8;
11591#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11592
11593#if defined(MBEDTLS_SSL_PROTO_DTLS)
11594 if( (size_t)( end - p ) < 1 )
11595 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11596
11597 ssl->disable_datagram_packing = *p++;
11598#endif /* MBEDTLS_SSL_PROTO_DTLS */
11599
11600 if( (size_t)( end - p ) < 8 )
11601 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11602
11603 memcpy( ssl->cur_out_ctr, p, 8 );
11604 p += 8;
11605
11606#if defined(MBEDTLS_SSL_PROTO_DTLS)
11607 if( (size_t)( end - p ) < 2 )
11608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11609
11610 ssl->mtu = ( p[0] << 8 ) | p[1];
11611 p += 2;
11612#endif /* MBEDTLS_SSL_PROTO_DTLS */
11613
11614#if defined(MBEDTLS_SSL_ALPN)
11615 {
11616 uint8_t alpn_len;
11617 const char **cur;
11618
11619 if( (size_t)( end - p ) < 1 )
11620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11621
11622 alpn_len = *p++;
11623
11624 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11625 {
11626 /* alpn_chosen should point to an item in the configured list */
11627 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11628 {
11629 if( strlen( *cur ) == alpn_len &&
11630 memcmp( p, cur, alpn_len ) == 0 )
11631 {
11632 ssl->alpn_chosen = *cur;
11633 break;
11634 }
11635 }
11636 }
11637
11638 /* can only happen on conf mismatch */
11639 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11641
11642 p += alpn_len;
11643 }
11644#endif /* MBEDTLS_SSL_ALPN */
11645
11646 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011647 * Forced fields from top-level ssl_context structure
11648 *
11649 * Most of them already set to the correct value by mbedtls_ssl_init() and
11650 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11651 */
11652 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11653
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011654#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011655 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011656#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11657#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011658 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011659#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011660
Hanno Becker83985822019-08-30 10:42:49 +010011661 /* Adjust pointers for header fields of outgoing records to
11662 * the given transform, accounting for explicit IV and CID. */
11663 ssl_update_out_pointers( ssl, ssl->transform );
11664
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011665#if defined(MBEDTLS_SSL_PROTO_DTLS)
11666 ssl->in_epoch = 1;
11667#endif
11668
11669 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11670 * which we don't want - otherwise we'd end up freeing the wrong transform
11671 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11672 if( ssl->handshake != NULL )
11673 {
11674 mbedtls_ssl_handshake_free( ssl );
11675 mbedtls_free( ssl->handshake );
11676 ssl->handshake = NULL;
11677 }
11678
11679 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011680 * Done - should have consumed entire buffer
11681 */
11682 if( p != end )
11683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011684
11685 return( 0 );
11686}
11687
11688/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011689 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011690 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011691int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011692 const unsigned char *buf,
11693 size_t len )
11694{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011695 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011696
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011697 if( ret != 0 )
11698 mbedtls_ssl_free( context );
11699
11700 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011701}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011702#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011703
11704/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011705 * Free an SSL context
11706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011707void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011708{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011709 if( ssl == NULL )
11710 return;
11711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011713
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011714 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011715 {
Angus Grattond8213d02016-05-25 20:56:48 +100011716 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011717 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011718 }
11719
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011720 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011721 {
Angus Grattond8213d02016-05-25 20:56:48 +100011722 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011723 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011724 }
11725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011726#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011727 if( ssl->compress_buf != NULL )
11728 {
Angus Grattond8213d02016-05-25 20:56:48 +100011729 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011730 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011731 }
11732#endif
11733
Paul Bakker48916f92012-09-16 19:57:18 +000011734 if( ssl->transform )
11735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011736 mbedtls_ssl_transform_free( ssl->transform );
11737 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011738 }
11739
11740 if( ssl->handshake )
11741 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011742 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011743 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11744 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011746 mbedtls_free( ssl->handshake );
11747 mbedtls_free( ssl->transform_negotiate );
11748 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011749 }
11750
Paul Bakkerc0463502013-02-14 11:19:38 +010011751 if( ssl->session )
11752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011753 mbedtls_ssl_session_free( ssl->session );
11754 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011755 }
11756
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011757#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011758 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011759 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011760 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011761 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011762 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011763#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011765#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11766 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11769 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011770 }
11771#endif
11772
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011773#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011774 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011775#endif
11776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011778
Paul Bakker86f04f42013-02-14 11:20:09 +010011779 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011780 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011781}
11782
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011783/*
11784 * Initialze mbedtls_ssl_config
11785 */
11786void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11787{
11788 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011789
11790#if !defined(MBEDTLS_SSL_PROTO_TLS)
11791 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11792#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011793}
11794
Simon Butcherc97b6972015-12-27 23:48:17 +000011795#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011796#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011797static int ssl_preset_default_hashes[] = {
11798#if defined(MBEDTLS_SHA512_C)
11799 MBEDTLS_MD_SHA512,
11800 MBEDTLS_MD_SHA384,
11801#endif
11802#if defined(MBEDTLS_SHA256_C)
11803 MBEDTLS_MD_SHA256,
11804 MBEDTLS_MD_SHA224,
11805#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011806#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011807 MBEDTLS_MD_SHA1,
11808#endif
11809 MBEDTLS_MD_NONE
11810};
Simon Butcherc97b6972015-12-27 23:48:17 +000011811#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011812#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011813
Hanno Becker73f4cb12019-06-27 13:51:07 +010011814#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011815static int ssl_preset_suiteb_ciphersuites[] = {
11816 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11817 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11818 0
11819};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011820#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011821
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011822#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011823#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011824static int ssl_preset_suiteb_hashes[] = {
11825 MBEDTLS_MD_SHA256,
11826 MBEDTLS_MD_SHA384,
11827 MBEDTLS_MD_NONE
11828};
11829#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011830#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011831
Hanno Beckerc1096e72019-06-19 12:30:41 +010011832#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011833static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011834#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011835 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011836#endif
11837#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011838 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011839#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011840 MBEDTLS_ECP_DP_NONE
11841};
11842#endif
11843
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011844/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011845 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011846 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011847int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011848 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011849{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011850#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011851 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011852#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011853
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011854 /* Use the functions here so that they are covered in tests,
11855 * but otherwise access member directly for efficiency */
11856 mbedtls_ssl_conf_endpoint( conf, endpoint );
11857 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011858
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011859 /*
11860 * Things that are common to all presets
11861 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011862#if defined(MBEDTLS_SSL_CLI_C)
11863 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11864 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011865#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011866 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011867#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011868#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11869 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11870#endif
11871 }
11872#endif
11873
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011874#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011875 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011876#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011877
11878#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11879 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11880#endif
11881
11882#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011883#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011884 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011885#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11886#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011887 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011888 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011889#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011890#endif
11891
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011892#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11893 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11894#endif
11895
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011896#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011897 conf->f_cookie_write = ssl_cookie_write_dummy;
11898 conf->f_cookie_check = ssl_cookie_check_dummy;
11899#endif
11900
Hanno Becker7f376f42019-06-12 16:20:48 +010011901#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11902 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011903 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11904#endif
11905
Janos Follath088ce432017-04-10 12:42:31 +010011906#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011907#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011908 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011909#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11910#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011911
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011913#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011914 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011915#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11916#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011917 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011918#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11919#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011920
11921#if defined(MBEDTLS_SSL_RENEGOTIATION)
11922 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011923 memset( conf->renego_period, 0x00, 2 );
11924 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011925#endif
11926
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011927#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11928 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11929 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011930 const unsigned char dhm_p[] =
11931 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11932 const unsigned char dhm_g[] =
11933 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11934
Hanno Beckera90658f2017-10-04 15:29:08 +010011935 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11936 dhm_p, sizeof( dhm_p ),
11937 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011938 {
11939 return( ret );
11940 }
11941 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011942#endif
11943
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011944 /*
11945 * Preset-specific defaults
11946 */
11947 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011948 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011949 /*
11950 * NSA Suite B
11951 */
11952 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011953#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011954 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011955#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11956#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011957 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011958#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11959#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011960 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011961#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11962#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011963 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011964#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011965
Hanno Becker73f4cb12019-06-27 13:51:07 +010011966#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011967 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11968 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11969 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11970 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11971 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011972#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011973
11974#if defined(MBEDTLS_X509_CRT_PARSE_C)
11975 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011976#endif
11977
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011978#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011979#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011980 conf->sig_hashes = ssl_preset_suiteb_hashes;
11981#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011982#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011983
11984#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011985#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011986 conf->curve_list = ssl_preset_suiteb_curves;
11987#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011988#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011989 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011990
11991 /*
11992 * Default
11993 */
11994 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011995#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011996 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11997 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11998 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11999 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012000#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12001#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012002 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12003 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12004 MBEDTLS_SSL_MIN_MINOR_VERSION :
12005 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012007 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012008 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12009#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012010#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12011#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12012 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12013#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12014#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12015 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12016#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012017
Hanno Becker73f4cb12019-06-27 13:51:07 +010012018#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012019 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
12020 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
12021 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
12022 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
12023 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012024#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012025
12026#if defined(MBEDTLS_X509_CRT_PARSE_C)
12027 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12028#endif
12029
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012030#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012031#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012032 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012033#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012034#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012035
12036#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012037#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012038 conf->curve_list = mbedtls_ecp_grp_id_list();
12039#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012040#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012041
12042#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12043 conf->dhm_min_bitlen = 1024;
12044#endif
12045 }
12046
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012047 return( 0 );
12048}
12049
12050/*
12051 * Free mbedtls_ssl_config
12052 */
12053void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12054{
12055#if defined(MBEDTLS_DHM_C)
12056 mbedtls_mpi_free( &conf->dhm_P );
12057 mbedtls_mpi_free( &conf->dhm_G );
12058#endif
12059
12060#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12061 if( conf->psk != NULL )
12062 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012063 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012064 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012065 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012066 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012067 }
12068
12069 if( conf->psk_identity != NULL )
12070 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012071 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012072 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012073 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012074 conf->psk_identity_len = 0;
12075 }
12076#endif
12077
12078#if defined(MBEDTLS_X509_CRT_PARSE_C)
12079 ssl_key_cert_free( conf->key_cert );
12080#endif
12081
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012082 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012083}
12084
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012085#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012086 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12087 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012088/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012089 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012091unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012093#if defined(MBEDTLS_RSA_C)
12094 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12095 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012096#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012097#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012098 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12099 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012101 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012102}
12103
Hanno Becker7e5437a2017-04-28 17:15:26 +010012104unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12105{
12106 switch( type ) {
12107 case MBEDTLS_PK_RSA:
12108 return( MBEDTLS_SSL_SIG_RSA );
12109 case MBEDTLS_PK_ECDSA:
12110 case MBEDTLS_PK_ECKEY:
12111 return( MBEDTLS_SSL_SIG_ECDSA );
12112 default:
12113 return( MBEDTLS_SSL_SIG_ANON );
12114 }
12115}
12116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012117mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012118{
12119 switch( sig )
12120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012121#if defined(MBEDTLS_RSA_C)
12122 case MBEDTLS_SSL_SIG_RSA:
12123 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012124#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012125#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012126 case MBEDTLS_SSL_SIG_ECDSA:
12127 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012128#endif
12129 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012130 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012131 }
12132}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012133#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012134
Hanno Becker7e5437a2017-04-28 17:15:26 +010012135#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12136 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12137
12138/* Find an entry in a signature-hash set matching a given hash algorithm. */
12139mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12140 mbedtls_pk_type_t sig_alg )
12141{
12142 switch( sig_alg )
12143 {
12144 case MBEDTLS_PK_RSA:
12145 return( set->rsa );
12146 case MBEDTLS_PK_ECDSA:
12147 return( set->ecdsa );
12148 default:
12149 return( MBEDTLS_MD_NONE );
12150 }
12151}
12152
12153/* Add a signature-hash-pair to a signature-hash set */
12154void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12155 mbedtls_pk_type_t sig_alg,
12156 mbedtls_md_type_t md_alg )
12157{
12158 switch( sig_alg )
12159 {
12160 case MBEDTLS_PK_RSA:
12161 if( set->rsa == MBEDTLS_MD_NONE )
12162 set->rsa = md_alg;
12163 break;
12164
12165 case MBEDTLS_PK_ECDSA:
12166 if( set->ecdsa == MBEDTLS_MD_NONE )
12167 set->ecdsa = md_alg;
12168 break;
12169
12170 default:
12171 break;
12172 }
12173}
12174
12175/* Allow exactly one hash algorithm for each signature. */
12176void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12177 mbedtls_md_type_t md_alg )
12178{
12179 set->rsa = md_alg;
12180 set->ecdsa = md_alg;
12181}
12182
12183#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12184 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12185
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012186/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012187 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012189mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012190{
12191 switch( hash )
12192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012193#if defined(MBEDTLS_MD5_C)
12194 case MBEDTLS_SSL_HASH_MD5:
12195 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012197#if defined(MBEDTLS_SHA1_C)
12198 case MBEDTLS_SSL_HASH_SHA1:
12199 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012201#if defined(MBEDTLS_SHA256_C)
12202 case MBEDTLS_SSL_HASH_SHA224:
12203 return( MBEDTLS_MD_SHA224 );
12204 case MBEDTLS_SSL_HASH_SHA256:
12205 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012207#if defined(MBEDTLS_SHA512_C)
12208 case MBEDTLS_SSL_HASH_SHA384:
12209 return( MBEDTLS_MD_SHA384 );
12210 case MBEDTLS_SSL_HASH_SHA512:
12211 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012212#endif
12213 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012214 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012215 }
12216}
12217
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012218/*
12219 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12220 */
12221unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12222{
12223 switch( md )
12224 {
12225#if defined(MBEDTLS_MD5_C)
12226 case MBEDTLS_MD_MD5:
12227 return( MBEDTLS_SSL_HASH_MD5 );
12228#endif
12229#if defined(MBEDTLS_SHA1_C)
12230 case MBEDTLS_MD_SHA1:
12231 return( MBEDTLS_SSL_HASH_SHA1 );
12232#endif
12233#if defined(MBEDTLS_SHA256_C)
12234 case MBEDTLS_MD_SHA224:
12235 return( MBEDTLS_SSL_HASH_SHA224 );
12236 case MBEDTLS_MD_SHA256:
12237 return( MBEDTLS_SSL_HASH_SHA256 );
12238#endif
12239#if defined(MBEDTLS_SHA512_C)
12240 case MBEDTLS_MD_SHA384:
12241 return( MBEDTLS_SSL_HASH_SHA384 );
12242 case MBEDTLS_MD_SHA512:
12243 return( MBEDTLS_SSL_HASH_SHA512 );
12244#endif
12245 default:
12246 return( MBEDTLS_SSL_HASH_NONE );
12247 }
12248}
12249
Hanno Beckeree902df2019-08-23 13:47:47 +010012250#if defined(MBEDTLS_USE_TINYCRYPT)
12251/*
12252 * Check if a curve proposed by the peer is in our list.
12253 * Return 0 if we're willing to use it, -1 otherwise.
12254 */
12255int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12256 mbedtls_uecc_group_id grp_id )
12257{
12258 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12259 if( own_ec_id == grp_id )
12260 return( 0 );
12261 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12262
12263 return( -1 );
12264}
12265#endif /* MBEDTLS_USE_TINYCRYPT */
12266
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012267#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012268/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012269 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012270 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012271 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012272int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12273 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012274{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012275 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12276 if( own_ec_id == grp_id )
12277 return( 0 );
12278 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012279
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012280 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012281}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012282#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012283
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012284#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012285/*
12286 * Check if a hash proposed by the peer is in our list.
12287 * Return 0 if we're willing to use it, -1 otherwise.
12288 */
12289int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12290 mbedtls_md_type_t md )
12291{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012292 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12293 if( md_alg == md )
12294 return( 0 );
12295 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012296
12297 return( -1 );
12298}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012299#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012301#if defined(MBEDTLS_X509_CRT_PARSE_C)
12302int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012303 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012304 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012305 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012306{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012307 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012308#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012309 int usage = 0;
12310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012311#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012312 const char *ext_oid;
12313 size_t ext_len;
12314#endif
12315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012316#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12317 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012318 ((void) cert);
12319 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012320 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012321#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012323#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12324 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012325 {
12326 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012327 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012329 case MBEDTLS_KEY_EXCHANGE_RSA:
12330 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012331 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012332 break;
12333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012334 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12335 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12336 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12337 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012338 break;
12339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012340 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12341 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012342 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012343 break;
12344
12345 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012346 case MBEDTLS_KEY_EXCHANGE_NONE:
12347 case MBEDTLS_KEY_EXCHANGE_PSK:
12348 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12349 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012350 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012351 usage = 0;
12352 }
12353 }
12354 else
12355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012356 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12357 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012358 }
12359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012360 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012361 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012362 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012363 ret = -1;
12364 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012365#else
12366 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012367#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012369#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12370 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012372 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12373 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012374 }
12375 else
12376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012377 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12378 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012379 }
12380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012381 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012382 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012383 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012384 ret = -1;
12385 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012386#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012387
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012388 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012390#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012391
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012392#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12393 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12394int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12395 unsigned char *output,
12396 unsigned char *data, size_t data_len )
12397{
12398 int ret = 0;
12399 mbedtls_md5_context mbedtls_md5;
12400 mbedtls_sha1_context mbedtls_sha1;
12401
12402 mbedtls_md5_init( &mbedtls_md5 );
12403 mbedtls_sha1_init( &mbedtls_sha1 );
12404
12405 /*
12406 * digitally-signed struct {
12407 * opaque md5_hash[16];
12408 * opaque sha_hash[20];
12409 * };
12410 *
12411 * md5_hash
12412 * MD5(ClientHello.random + ServerHello.random
12413 * + ServerParams);
12414 * sha_hash
12415 * SHA(ClientHello.random + ServerHello.random
12416 * + ServerParams);
12417 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012418 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012419 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012421 goto exit;
12422 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012423 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012424 ssl->handshake->randbytes, 64 ) ) != 0 )
12425 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012427 goto exit;
12428 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012429 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012430 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012432 goto exit;
12433 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012434 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012435 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012437 goto exit;
12438 }
12439
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012440 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012441 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012443 goto exit;
12444 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012445 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012446 ssl->handshake->randbytes, 64 ) ) != 0 )
12447 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012449 goto exit;
12450 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012451 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012452 data_len ) ) != 0 )
12453 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012455 goto exit;
12456 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012457 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012458 output + 16 ) ) != 0 )
12459 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012461 goto exit;
12462 }
12463
12464exit:
12465 mbedtls_md5_free( &mbedtls_md5 );
12466 mbedtls_sha1_free( &mbedtls_sha1 );
12467
12468 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012469 mbedtls_ssl_pend_fatal_alert( ssl,
12470 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012471
12472 return( ret );
12473
12474}
12475#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12476 MBEDTLS_SSL_PROTO_TLS1_1 */
12477
12478#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12479 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12480int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012481 unsigned char *hash, size_t *hashlen,
12482 unsigned char *data, size_t data_len,
12483 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012484{
12485 int ret = 0;
12486 mbedtls_md_context_t ctx;
12487 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012488 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012489
12490 mbedtls_md_init( &ctx );
12491
12492 /*
12493 * digitally-signed struct {
12494 * opaque client_random[32];
12495 * opaque server_random[32];
12496 * ServerDHParams params;
12497 * };
12498 */
12499 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12500 {
12501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12502 goto exit;
12503 }
12504 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12505 {
12506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12507 goto exit;
12508 }
12509 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12510 {
12511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12512 goto exit;
12513 }
12514 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12515 {
12516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12517 goto exit;
12518 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012519 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012520 {
12521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12522 goto exit;
12523 }
12524
12525exit:
12526 mbedtls_md_free( &ctx );
12527
12528 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012529 mbedtls_ssl_pend_fatal_alert( ssl,
12530 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012531
12532 return( ret );
12533}
12534#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12535 MBEDTLS_SSL_PROTO_TLS1_2 */
12536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012537#endif /* MBEDTLS_SSL_TLS_C */