blob: 850bcb1723d42d2657dbeb1b1949901caebd003f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010079 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010090 }
91
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300500 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300510 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100663 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100665 int ret;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 hs = ( slen + 1 ) / 2;
673 S1 = secret;
674 S2 = secret + slen - hs;
675
676 nb = strlen( label );
677 memcpy( tmp + 20, label, nb );
678 memcpy( tmp + 20 + nb, random, rlen );
679 nb += rlen;
680
681 /*
682 * First compute P_md5(secret,label+random)[0..dlen]
683 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100684 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
685 MBEDTLS_MD_INVALID_HANDLE )
686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100688 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100691 return( ret );
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
694 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
695 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000696
697 for( i = 0; i < dlen; i += 16 )
698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_md_hmac_reset ( &md_ctx );
700 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
701 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_md_hmac_reset ( &md_ctx );
704 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
705 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
708
709 for( j = 0; j < k; j++ )
710 dstbuf[i + j] = h_i[j];
711 }
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100714
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 /*
716 * XOR out with P_sha1(secret,label+random)[0..dlen]
717 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100718 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
719 MBEDTLS_MD_INVALID_HANDLE )
720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100722 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100725 return( ret );
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
728 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
729 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
731 for( i = 0; i < dlen; i += 20 )
732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_md_hmac_reset ( &md_ctx );
734 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
735 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_md_hmac_reset ( &md_ctx );
738 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
739 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740
741 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
742
743 for( j = 0; j < k; j++ )
744 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
745 }
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100748
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500749 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
750 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752 return( 0 );
753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100757#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
758MBEDTLS_ALWAYS_INLINE static inline
759#else
760static
761#endif
762int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 const unsigned char *secret, size_t slen,
764 const char *label,
765 const unsigned char *random, size_t rlen,
766 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000767{
768 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100769 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000770 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100772 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100774 int ret;
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000777
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100778 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
779 MBEDTLS_MD_INVALID_HANDLE )
780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100782 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100785
786 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788
789 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100790 memcpy( tmp + md_len, label, nb );
791 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792 nb += rlen;
793
794 /*
795 * Compute P_<hash>(secret, label + random)[0..dlen]
796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100798 return( ret );
799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
801 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
802 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100803
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100804 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_hmac_reset ( &md_ctx );
807 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
808 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_md_hmac_reset ( &md_ctx );
811 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
812 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000813
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100814 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
816 for( j = 0; j < k; j++ )
817 dstbuf[i + j] = h_i[j];
818 }
819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100821
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500822 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
823 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000824
825 return( 0 );
826}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100829MBEDTLS_NO_INLINE static int tls_prf_sha256(
830 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100831 const char *label,
832 const unsigned char *random, size_t rlen,
833 unsigned char *dstbuf, size_t dlen )
834{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100836 label, random, rlen, dstbuf, dlen ) );
837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100841MBEDTLS_NO_INLINE static int tls_prf_sha384(
842 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200843 const char *label,
844 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000845 unsigned char *dstbuf, size_t dlen )
846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100848 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#endif /* MBEDTLS_SHA512_C */
851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000852
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100853/*
854 * Call the appropriate PRF function
855 */
Hanno Becker2793f742019-08-16 14:28:43 +0100856MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100857 mbedtls_md_type_t hash,
858 const unsigned char *secret, size_t slen,
859 const char *label,
860 const unsigned char *random, size_t rlen,
861 unsigned char *dstbuf, size_t dlen )
862{
863#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
864 (void) hash;
865#endif
866
867#if defined(MBEDTLS_SSL_PROTO_SSL3)
868 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
869 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
870 else
871#endif
872#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100873 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100874 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
875 else
876#endif
877#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
878#if defined(MBEDTLS_SHA512_C)
879 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
880 hash == MBEDTLS_MD_SHA384 )
881 {
882 return( tls_prf_sha384( secret, slen, label, random, rlen,
883 dstbuf, dlen ) );
884 }
885 else
886#endif
887#if defined(MBEDTLS_SHA256_C)
888 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
889 {
890 return( tls_prf_sha256( secret, slen, label, random, rlen,
891 dstbuf, dlen ) );
892 }
893#endif
894#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
895
896 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
897}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200898
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100899#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100900MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100901 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
902{
903 const char *sender;
904 mbedtls_md5_context md5;
905 mbedtls_sha1_context sha1;
906
907 unsigned char padbuf[48];
908 unsigned char md5sum[16];
909 unsigned char sha1sum[20];
910
911 mbedtls_ssl_session *session = ssl->session_negotiate;
912 if( !session )
913 session = ssl->session;
914
915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
916
917 mbedtls_md5_init( &md5 );
918 mbedtls_sha1_init( &sha1 );
919
920 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
921 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
922
923 /*
924 * SSLv3:
925 * hash =
926 * MD5( master + pad2 +
927 * MD5( handshake + sender + master + pad1 ) )
928 * + SHA1( master + pad2 +
929 * SHA1( handshake + sender + master + pad1 ) )
930 */
931
932#if !defined(MBEDTLS_MD5_ALT)
933 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
934 md5.state, sizeof( md5.state ) );
935#endif
936
937#if !defined(MBEDTLS_SHA1_ALT)
938 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
939 sha1.state, sizeof( sha1.state ) );
940#endif
941
942 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
943 : "SRVR";
944
945 memset( padbuf, 0x36, 48 );
946
947 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
948 mbedtls_md5_update_ret( &md5, session->master, 48 );
949 mbedtls_md5_update_ret( &md5, padbuf, 48 );
950 mbedtls_md5_finish_ret( &md5, md5sum );
951
952 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
953 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
954 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
955 mbedtls_sha1_finish_ret( &sha1, sha1sum );
956
957 memset( padbuf, 0x5C, 48 );
958
959 mbedtls_md5_starts_ret( &md5 );
960 mbedtls_md5_update_ret( &md5, session->master, 48 );
961 mbedtls_md5_update_ret( &md5, padbuf, 48 );
962 mbedtls_md5_update_ret( &md5, md5sum, 16 );
963 mbedtls_md5_finish_ret( &md5, buf );
964
965 mbedtls_sha1_starts_ret( &sha1 );
966 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
967 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
968 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
969 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
970
971 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
972
973 mbedtls_md5_free( &md5 );
974 mbedtls_sha1_free( &sha1 );
975
976 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
977 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
978 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
979
980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
981}
982#endif /* MBEDTLS_SSL_PROTO_SSL3 */
983
984#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100985MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100986 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
987{
988 int len = 12;
989 const char *sender;
990 mbedtls_md5_context md5;
991 mbedtls_sha1_context sha1;
992 unsigned char padbuf[36];
993
994 mbedtls_ssl_session *session = ssl->session_negotiate;
995 if( !session )
996 session = ssl->session;
997
998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
999
1000 mbedtls_md5_init( &md5 );
1001 mbedtls_sha1_init( &sha1 );
1002
1003 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1004 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1005
1006 /*
1007 * TLSv1:
1008 * hash = PRF( master, finished_label,
1009 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1010 */
1011
1012#if !defined(MBEDTLS_MD5_ALT)
1013 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1014 md5.state, sizeof( md5.state ) );
1015#endif
1016
1017#if !defined(MBEDTLS_SHA1_ALT)
1018 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1019 sha1.state, sizeof( sha1.state ) );
1020#endif
1021
1022 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1023 ? "client finished"
1024 : "server finished";
1025
1026 mbedtls_md5_finish_ret( &md5, padbuf );
1027 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1028
1029 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1030 mbedtls_ssl_suite_get_mac(
1031 mbedtls_ssl_ciphersuite_from_id(
1032 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1033 session->master, 48, sender,
1034 padbuf, 36, buf, len );
1035
1036 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1037
1038 mbedtls_md5_free( &md5 );
1039 mbedtls_sha1_free( &sha1 );
1040
1041 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1042
1043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1044}
1045#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1046
1047#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1048#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001049MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001050 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1051{
1052 int len = 12;
1053 const char *sender;
1054 mbedtls_sha256_context sha256;
1055 unsigned char padbuf[32];
1056
1057 mbedtls_ssl_session *session = ssl->session_negotiate;
1058 if( !session )
1059 session = ssl->session;
1060
1061 mbedtls_sha256_init( &sha256 );
1062
1063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1064
1065 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1066
1067 /*
1068 * TLSv1.2:
1069 * hash = PRF( master, finished_label,
1070 * Hash( handshake ) )[0.11]
1071 */
1072
1073#if !defined(MBEDTLS_SHA256_ALT)
1074 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1075 sha256.state, sizeof( sha256.state ) );
1076#endif
1077
1078 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1079 ? "client finished"
1080 : "server finished";
1081
1082 mbedtls_sha256_finish_ret( &sha256, padbuf );
1083
1084 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1085 mbedtls_ssl_suite_get_mac(
1086 mbedtls_ssl_ciphersuite_from_id(
1087 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1088 session->master, 48, sender,
1089 padbuf, 32, buf, len );
1090
1091 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1092
1093 mbedtls_sha256_free( &sha256 );
1094
1095 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1096
1097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1098}
1099#endif /* MBEDTLS_SHA256_C */
1100
1101#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001102MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001103 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1104{
1105 int len = 12;
1106 const char *sender;
1107 mbedtls_sha512_context sha512;
1108 unsigned char padbuf[48];
1109
1110 mbedtls_ssl_session *session = ssl->session_negotiate;
1111 if( !session )
1112 session = ssl->session;
1113
1114 mbedtls_sha512_init( &sha512 );
1115
1116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1117
1118 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1119
1120 /*
1121 * TLSv1.2:
1122 * hash = PRF( master, finished_label,
1123 * Hash( handshake ) )[0.11]
1124 */
1125
1126#if !defined(MBEDTLS_SHA512_ALT)
1127 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1128 sha512.state, sizeof( sha512.state ) );
1129#endif
1130
1131 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1132 ? "client finished"
1133 : "server finished";
1134
1135 mbedtls_sha512_finish_ret( &sha512, padbuf );
1136
1137 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1138 mbedtls_ssl_suite_get_mac(
1139 mbedtls_ssl_ciphersuite_from_id(
1140 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1141 session->master, 48, sender,
1142 padbuf, 48, buf, len );
1143
1144 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1145
1146 mbedtls_sha512_free( &sha512 );
1147
1148 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1149
1150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1151}
1152#endif /* MBEDTLS_SHA512_C */
1153#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1154
Hanno Becker2793f742019-08-16 14:28:43 +01001155MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1156 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001157 mbedtls_md_type_t hash,
1158 mbedtls_ssl_context *ssl,
1159 unsigned char *buf,
1160 int from )
1161{
1162#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1163 (void) hash;
1164#endif
1165
1166#if defined(MBEDTLS_SSL_PROTO_SSL3)
1167 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1168 ssl_calc_finished_ssl( ssl, buf, from );
1169 else
1170#endif
1171#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001172 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001173 ssl_calc_finished_tls( ssl, buf, from );
1174 else
1175#endif
1176#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1177#if defined(MBEDTLS_SHA512_C)
1178 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1179 hash == MBEDTLS_MD_SHA384 )
1180 {
1181 ssl_calc_finished_tls_sha384( ssl, buf, from );
1182 }
1183 else
1184#endif
1185#if defined(MBEDTLS_SHA256_C)
1186 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1187 ssl_calc_finished_tls_sha256( ssl, buf, from );
1188 else
1189#endif
1190#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1191 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1192
1193 return( 0 );
1194}
1195
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001196/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001197 * Populate a transform structure with session keys and all the other
1198 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001199 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001200 * Parameters:
1201 * - [in/out]: transform: structure to populate
1202 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001203 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001204 * - [in] ciphersuite
1205 * - [in] master
1206 * - [in] encrypt_then_mac
1207 * - [in] trunc_hmac
1208 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001209 * - [in] tls_prf: pointer to PRF to use for key derivation
1210 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001211 * - [in] minor_ver: SSL/TLS minor version
1212 * - [in] endpoint: client or server
1213 * - [in] ssl: optionally used for:
1214 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1215 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1216 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001217 */
Hanno Becker298a4702019-08-16 10:21:32 +01001218/* Force compilers to inline this function if it's used only
1219 * from one place, because at least ARMC5 doesn't do that
1220 * automatically. */
1221#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1222MBEDTLS_ALWAYS_INLINE static inline
1223#else
1224static
1225#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1226int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001227 int ciphersuite,
1228 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001229#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001230#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1231 int encrypt_then_mac,
1232#endif
1233#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1234 int trunc_hmac,
1235#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001236#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001237#if defined(MBEDTLS_ZLIB_SUPPORT)
1238 int compression,
1239#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001240 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001241 int minor_ver,
1242 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001243 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001244{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001245 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246 unsigned char keyblk[256];
1247 unsigned char *key1;
1248 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001249 unsigned char *mac_enc;
1250 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001251 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001252 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001253 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001254 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001256 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001257
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001258#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1259 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1260 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001261 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001262 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001263#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001264
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001265 /*
1266 * Some data just needs copying into the structure
1267 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001268#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1269 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001270 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001271#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001272
1273#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001274 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001275#else
1276 ((void) minor_ver);
1277#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001279#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1280 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1281#endif
1282
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001283 /*
1284 * Get various info structures
1285 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001286 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001287 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001288 {
1289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001290 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1292 }
1293
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 cipher_info = mbedtls_cipher_info_from_type(
1295 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 if( cipher_info == NULL )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001301 }
1302
Hanno Becker473f98f2019-06-26 10:27:32 +01001303 md_info = mbedtls_md_info_from_type(
1304 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001305 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001308 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001310 }
1311
Hanno Beckera5a2b082019-05-15 14:03:01 +01001312#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001313 /* Copy own and peer's CID if the use of the CID
1314 * extension has been negotiated. */
1315 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1316 {
1317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001318
Hanno Becker4932f9f2019-05-03 15:23:51 +01001319 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001320 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001321 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001322 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001323
1324 transform->out_cid_len = ssl->handshake->peer_cid_len;
1325 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1326 ssl->handshake->peer_cid_len );
1327 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1328 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001329 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001330#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001331
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001333 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001334 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001335 ret = ssl_prf( minor_ver,
1336 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1337 master, 48, "key expansion", randbytes, 64,
1338 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001339 if( ret != 0 )
1340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001342 return( ret );
1343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001346 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001347 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001348 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 /*
1352 * Determine the appropriate key, IV and MAC length.
1353 */
Paul Bakker68884e32013-01-07 18:20:04 +01001354
Hanno Beckere7f2df02017-12-27 08:17:40 +00001355 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001356
Hanno Beckerf1229442018-01-03 15:32:31 +00001357#if defined(MBEDTLS_GCM_C) || \
1358 defined(MBEDTLS_CCM_C) || \
1359 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001361 cipher_info->mode == MBEDTLS_MODE_CCM ||
1362 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001364 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001365 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001366 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1367 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001368
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001369 /* All modes haves 96-bit IVs;
1370 * GCM and CCM has 4 implicit and 8 explicit bytes
1371 * ChachaPoly has all 12 bytes implicit
1372 */
Paul Bakker68884e32013-01-07 18:20:04 +01001373 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001374 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1375 transform->fixed_ivlen = 12;
1376 else
1377 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001378 }
1379 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001380#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1381#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1382 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1383 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001384 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001385 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1387 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001390 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001392
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001393 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001394 mac_key_len = mbedtls_md_get_size( md_info );
1395 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001398 /*
1399 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1400 * (rfc 6066 page 13 or rfc 2104 section 4),
1401 * so we only need to adjust the length here.
1402 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001403 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001406
1407#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1408 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001409 * HMAC implementation which also truncates the key
1410 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001411 mac_key_len = transform->maclen;
1412#endif
1413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001415
1416 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001417 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001418 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001419 else
1420#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1421 {
1422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1424 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Hanno Beckera9d5c452019-07-25 16:47:12 +01001426 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001427 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001428 (unsigned) transform->ivlen,
1429 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
1431 /*
1432 * Finally setup the cipher contexts, IVs and MAC secrets.
1433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001435 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001437 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001438 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Paul Bakker68884e32013-01-07 18:20:04 +01001440 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001441 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001442
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001443 /*
1444 * This is not used in TLS v1.1.
1445 */
Paul Bakker48916f92012-09-16 19:57:18 +00001446 iv_copy_len = ( transform->fixed_ivlen ) ?
1447 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001448 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1449 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001450 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 }
1452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_SSL_CLI_C */
1454#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001455 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001457 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001458 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001459
Hanno Becker81c7b182017-11-09 18:39:33 +00001460 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001461 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001462
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001463 /*
1464 * This is not used in TLS v1.1.
1465 */
Paul Bakker48916f92012-09-16 19:57:18 +00001466 iv_copy_len = ( transform->fixed_ivlen ) ?
1467 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001468 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1469 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001470 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001472 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1476 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478
Hanno Becker92231322018-01-03 15:32:51 +00001479#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001481 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001482 {
Hanno Becker92231322018-01-03 15:32:51 +00001483 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001487 }
1488
Hanno Becker81c7b182017-11-09 18:39:33 +00001489 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1490 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001491 }
1492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1494#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1495 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001496 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001497 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001498 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1499 For AEAD-based ciphersuites, there is nothing to do here. */
1500 if( mac_key_len != 0 )
1501 {
1502 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1503 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1504 }
Paul Bakker68884e32013-01-07 18:20:04 +01001505 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001506 else
1507#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1510 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001511 }
Hanno Becker92231322018-01-03 15:32:51 +00001512#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1515 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001516 {
1517 int ret = 0;
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001520
Hanno Beckere7f2df02017-12-27 08:17:40 +00001521 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001522 transform->iv_enc, transform->iv_dec,
1523 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001524 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001525 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1528 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001529 }
1530 }
Hanno Becker92231322018-01-03 15:32:51 +00001531#else
1532 ((void) mac_dec);
1533 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001535
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001536#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1537 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001538 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001539 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001540 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001541 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001542 iv_copy_len );
1543 }
1544#endif
1545
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001546 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001547 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001550 return( ret );
1551 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001552
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001553 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 cipher_info ) ) != 0 )
1555 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 return( ret );
1558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001561 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 return( ret );
1566 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001569 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001573 return( ret );
1574 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_CIPHER_MODE_CBC)
1577 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1580 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001583 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001584 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1587 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001590 return( ret );
1591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001595 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001596
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001597 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001599 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001602
Paul Bakker48916f92012-09-16 19:57:18 +00001603 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1604 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001605
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001606 if( deflateInit( &transform->ctx_deflate,
1607 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001608 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1611 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001612 }
1613 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 return( 0 );
1617}
1618
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001619#if defined(MBEDTLS_SSL_PROTO_SSL3)
1620static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1621 unsigned char hash[36],
1622 size_t *hlen )
1623{
1624 mbedtls_md5_context md5;
1625 mbedtls_sha1_context sha1;
1626 unsigned char pad_1[48];
1627 unsigned char pad_2[48];
1628
1629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1630
1631 mbedtls_md5_init( &md5 );
1632 mbedtls_sha1_init( &sha1 );
1633
1634 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1635 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1636
1637 memset( pad_1, 0x36, 48 );
1638 memset( pad_2, 0x5C, 48 );
1639
1640 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1641 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1642 mbedtls_md5_finish_ret( &md5, hash );
1643
1644 mbedtls_md5_starts_ret( &md5 );
1645 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1646 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1647 mbedtls_md5_update_ret( &md5, hash, 16 );
1648 mbedtls_md5_finish_ret( &md5, hash );
1649
1650 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1651 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1652 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1653
1654 mbedtls_sha1_starts_ret( &sha1 );
1655 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1656 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1657 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1658 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1659
1660 *hlen = 36;
1661
1662 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1664
1665 mbedtls_md5_free( &md5 );
1666 mbedtls_sha1_free( &sha1 );
1667
1668 return;
1669}
1670#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1671
1672#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1673static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1674 unsigned char hash[36],
1675 size_t *hlen )
1676{
1677 mbedtls_md5_context md5;
1678 mbedtls_sha1_context sha1;
1679
1680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1681
1682 mbedtls_md5_init( &md5 );
1683 mbedtls_sha1_init( &sha1 );
1684
1685 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1686 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1687
1688 mbedtls_md5_finish_ret( &md5, hash );
1689 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1690
1691 *hlen = 36;
1692
1693 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1695
1696 mbedtls_md5_free( &md5 );
1697 mbedtls_sha1_free( &sha1 );
1698
1699 return;
1700}
1701#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1702
1703#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1704#if defined(MBEDTLS_SHA256_C)
1705static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1706 unsigned char hash[32],
1707 size_t *hlen )
1708{
1709 mbedtls_sha256_context sha256;
1710
1711 mbedtls_sha256_init( &sha256 );
1712
1713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1714
1715 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1716 mbedtls_sha256_finish_ret( &sha256, hash );
1717
1718 *hlen = 32;
1719
1720 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1722
1723 mbedtls_sha256_free( &sha256 );
1724
1725 return;
1726}
1727#endif /* MBEDTLS_SHA256_C */
1728
1729#if defined(MBEDTLS_SHA512_C)
1730static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1731 unsigned char hash[48],
1732 size_t *hlen )
1733{
1734 mbedtls_sha512_context sha512;
1735
1736 mbedtls_sha512_init( &sha512 );
1737
1738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1739
1740 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1741 mbedtls_sha512_finish_ret( &sha512, hash );
1742
1743 *hlen = 48;
1744
1745 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1747
1748 mbedtls_sha512_free( &sha512 );
1749
1750 return;
1751}
1752#endif /* MBEDTLS_SHA512_C */
1753#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1754
Hanno Becker2f41b242019-08-15 17:29:43 +01001755int mbedtls_ssl_calc_verify( int minor_ver,
1756 mbedtls_md_type_t hash,
1757 mbedtls_ssl_context const *ssl,
1758 unsigned char *dst,
1759 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001760{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001761#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1762 (void) hash;
1763#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001765#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001766 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001767 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001768 else
1769#endif
1770#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001771 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001772 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001773 else
1774#endif
1775#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1776#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001777 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1778 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001780 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001781 }
1782 else
1783#endif
1784#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001785 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001786 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001787 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001788 }
1789 else
1790#endif
1791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1792 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1794 }
1795
1796 return( 0 );
1797}
1798
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001800 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001801 *
1802 * Parameters:
1803 * [in/out] handshake
1804 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1805 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001806 * [out] master
1807 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001808 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001809static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001810 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001811 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001812{
1813 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001814
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001815/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1816/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1817/* (void) ssl; */
1818/* #endif */
1819
1820 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1821 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001822
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001823#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001824 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001825 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1827 return( 0 );
1828 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001829#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001830
1831 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1832 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001833
1834#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001835 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1836 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001837 {
1838 unsigned char session_hash[48];
1839 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001840
Hanno Becker2f41b242019-08-15 17:29:43 +01001841 mbedtls_ssl_calc_verify(
1842 mbedtls_ssl_get_minor_ver( ssl ),
1843 mbedtls_ssl_suite_get_mac( ciphersuite ),
1844 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001845
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001846 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1847 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001848
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001849 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1850 mbedtls_ssl_suite_get_mac( ciphersuite ),
1851 handshake->premaster, handshake->pmslen,
1852 "extended master secret",
1853 session_hash, hash_len,
1854 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001855 }
1856 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001857#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001858 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001859 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1860 mbedtls_ssl_suite_get_mac( ciphersuite ),
1861 handshake->premaster, handshake->pmslen,
1862 "master secret",
1863 handshake->randbytes, 64,
1864 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001865 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001866 if( ret != 0 )
1867 {
1868 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1869 return( ret );
1870 }
1871
1872 mbedtls_platform_zeroize( handshake->premaster,
1873 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001874
1875 return( 0 );
1876}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001877
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001878int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1879{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001880 int ret;
1881
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1883
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001884 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001885 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001886 ssl->session_negotiate->master,
1887 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001888 if( ret != 0 )
1889 {
1890 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1891 return( ret );
1892 }
1893
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001894 /* Swap the client and server random values:
1895 * - MS derivation wanted client+server (RFC 5246 8.1)
1896 * - key derivation wants server+client (RFC 5246 6.3) */
1897 {
1898 unsigned char tmp[64];
1899 memcpy( tmp, ssl->handshake->randbytes, 64 );
1900 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1901 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1902 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1903 }
1904
1905 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001906 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001907 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1908 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001909#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001910#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001912#endif
1913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001914 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001915#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001916#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001917#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001918 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001919#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001920 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001921 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001922 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1923 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001924 if( ret != 0 )
1925 {
1926 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1927 return( ret );
1928 }
1929
1930 /* We no longer need Server/ClientHello.random values */
1931 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1932 sizeof( ssl->handshake->randbytes ) );
1933
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001934 /* Allocate compression buffer */
1935#if defined(MBEDTLS_ZLIB_SUPPORT)
1936 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1937 ssl->compress_buf == NULL )
1938 {
1939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1940 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1941 if( ssl->compress_buf == NULL )
1942 {
1943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001944 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001945 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1946 }
1947 }
1948#endif
1949
Paul Bakker5121ce52009-01-03 21:22:43 +00001950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1951
1952 return( 0 );
1953}
1954
Hanno Becker09d23642019-07-22 17:18:18 +01001955int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1956{
1957 int ret;
1958
1959 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1960 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1961
Hanno Beckera3c2c172019-07-23 16:51:57 +01001962#if defined(MBEDTLS_USE_TINYCRYPT)
1963 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001964 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001965 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001966 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1967 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1968 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1969 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1970 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001971 {
1972 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001973 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001974
1975 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1976 ssl->handshake->ecdh_privkey,
1977 ssl->handshake->premaster,
1978 uecc_curve ) )
1979 {
1980 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1981 }
1982
1983 ssl->handshake->pmslen = NUM_ECC_BYTES;
1984 }
1985 else
1986#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001987#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1988 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1989 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1990 {
1991 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1992 ssl->handshake->premaster,
1993 MBEDTLS_PREMASTER_SIZE,
1994 &ssl->handshake->pmslen,
1995 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001996 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001997 {
1998 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1999 return( ret );
2000 }
2001
2002 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2003 }
2004 else
2005#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002006#if defined(MBEDTLS_ECDH_C) && \
2007 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2008 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2009 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2010 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002011 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2012 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2013 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2014 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2015 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2016 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2017 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2018 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2019 {
2020 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2021 &ssl->handshake->pmslen,
2022 ssl->handshake->premaster,
2023 MBEDTLS_MPI_MAX_SIZE,
2024 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002025 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002026 {
2027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2028 return( ret );
2029 }
2030
2031 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2032 }
2033 else
2034#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2035 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2036 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2037 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2038#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2039 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2040 {
2041 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002042 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002043 {
2044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2045 return( ret );
2046 }
2047 }
2048 else
2049#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2050#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2051 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2052 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2053 {
2054 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2055 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2056 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002057 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002058 if( ret != 0 )
2059 {
2060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2061 return( ret );
2062 }
2063 }
2064 else
2065#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2066#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2067 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2068 == MBEDTLS_KEY_EXCHANGE_RSA )
2069 {
2070 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002071 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002072 * ssl_rsa_generate_partial_pms(). Only the
2073 * PMS length needs to be set. */
2074 ssl->handshake->pmslen = 48;
2075 }
2076 else
2077#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2078 {
2079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2080 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2081 }
2082
2083 return( 0 );
2084}
2085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2087int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002088{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002089 unsigned char *p = ssl->handshake->premaster;
2090 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002091 const unsigned char *psk = ssl->conf->psk;
2092 size_t psk_len = ssl->conf->psk_len;
2093
2094 /* If the psk callback was called, use its result */
2095 if( ssl->handshake->psk != NULL )
2096 {
2097 psk = ssl->handshake->psk;
2098 psk_len = ssl->handshake->psk_len;
2099 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002100
2101 /*
2102 * PMS = struct {
2103 * opaque other_secret<0..2^16-1>;
2104 * opaque psk<0..2^16-1>;
2105 * };
2106 * with "other_secret" depending on the particular key exchange
2107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2109 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002110 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002111 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002113
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002114 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002115
2116 if( end < p || (size_t)( end - p ) < psk_len )
2117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2118
2119 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002120 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002121 }
2122 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2124#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2125 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002126 {
2127 /*
2128 * other_secret already set by the ClientKeyExchange message,
2129 * and is 48 bytes long
2130 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002131 if( end - p < 2 )
2132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2133
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002134 *p++ = 0;
2135 *p++ = 48;
2136 p += 48;
2137 }
2138 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2140#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2141 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002142 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002143 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002144 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002145
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002146 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002148 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002149 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002150 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002153 return( ret );
2154 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002155 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002156 p += len;
2157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002159 }
2160 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2162#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2163 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002164 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002165 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002166 size_t zlen;
2167
Hanno Becker982da7e2019-09-02 09:47:39 +01002168#if defined(MBEDTLS_USE_TINYCRYPT)
2169 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
2170 ((void) ret);
2171
2172 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2173 ssl->handshake->ecdh_privkey,
2174 p + 2,
2175 uecc_curve ) )
2176 {
2177 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
2178 }
2179
2180 zlen = NUM_ECC_BYTES;
2181#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002183 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002184 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002185 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188 return( ret );
2189 }
2190
Hanno Becker982da7e2019-09-02 09:47:39 +01002191 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2192 MBEDTLS_DEBUG_ECDH_Z );
2193#endif /* MBEDTLS_USE_TINYCRYPT */
2194
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002195 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002196 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002197 }
2198 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002203 }
2204
2205 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002206 if( end - p < 2 )
2207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002208
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002209 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002210
2211 if( end < p || (size_t)( end - p ) < psk_len )
2212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2213
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002214 memcpy( p, psk, psk_len );
2215 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002216
2217 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2218
2219 return( 0 );
2220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002224/*
2225 * SSLv3.0 MAC functions
2226 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002227#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002228static void ssl_mac( mbedtls_md_context_t *md_ctx,
2229 const unsigned char *secret,
2230 const unsigned char *buf, size_t len,
2231 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002232 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002233{
2234 unsigned char header[11];
2235 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002236 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2238 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002239
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002240 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002242 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002243 else
Paul Bakker68884e32013-01-07 18:20:04 +01002244 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
2246 memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002247 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002248 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Paul Bakker68884e32013-01-07 18:20:04 +01002250 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mbedtls_md_starts( md_ctx );
2252 mbedtls_md_update( md_ctx, secret, md_size );
2253 mbedtls_md_update( md_ctx, padding, padlen );
2254 mbedtls_md_update( md_ctx, header, 11 );
2255 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002256 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Paul Bakker68884e32013-01-07 18:20:04 +01002258 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 mbedtls_md_starts( md_ctx );
2260 mbedtls_md_update( md_ctx, secret, md_size );
2261 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002262 mbedtls_md_update( md_ctx, out, md_size );
2263 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002264}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002266
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002267/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002268 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002269#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002270 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2271 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2272 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2273/* This function makes sure every byte in the memory region is accessed
2274 * (in ascending addresses order) */
2275static void ssl_read_memory( unsigned char *p, size_t len )
2276{
2277 unsigned char acc = 0;
2278 volatile unsigned char force;
2279
2280 for( ; len != 0; p++, len-- )
2281 acc ^= *p;
2282
2283 force = acc;
2284 (void) force;
2285}
2286#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2287
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002288/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002290 */
Hanno Becker3307b532017-12-27 21:37:21 +00002291
Hanno Beckera5a2b082019-05-15 14:03:01 +01002292#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002293/* This functions transforms a DTLS plaintext fragment and a record content
2294 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002295 *
2296 * struct {
2297 * opaque content[DTLSPlaintext.length];
2298 * ContentType real_type;
2299 * uint8 zeros[length_of_padding];
2300 * } DTLSInnerPlaintext;
2301 *
2302 * Input:
2303 * - `content`: The beginning of the buffer holding the
2304 * plaintext to be wrapped.
2305 * - `*content_size`: The length of the plaintext in Bytes.
2306 * - `max_len`: The number of Bytes available starting from
2307 * `content`. This must be `>= *content_size`.
2308 * - `rec_type`: The desired record content type.
2309 *
2310 * Output:
2311 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2312 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2313 *
2314 * Returns:
2315 * - `0` on success.
2316 * - A negative error code if `max_len` didn't offer enough space
2317 * for the expansion.
2318 */
2319static int ssl_cid_build_inner_plaintext( unsigned char *content,
2320 size_t *content_size,
2321 size_t remaining,
2322 uint8_t rec_type )
2323{
2324 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002325 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2326 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2327 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002328
2329 /* Write real content type */
2330 if( remaining == 0 )
2331 return( -1 );
2332 content[ len ] = rec_type;
2333 len++;
2334 remaining--;
2335
2336 if( remaining < pad )
2337 return( -1 );
2338 memset( content + len, 0, pad );
2339 len += pad;
2340 remaining -= pad;
2341
2342 *content_size = len;
2343 return( 0 );
2344}
2345
Hanno Becker7dc25772019-05-20 15:08:01 +01002346/* This function parses a DTLSInnerPlaintext structure.
2347 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002348static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2349 size_t *content_size,
2350 uint8_t *rec_type )
2351{
2352 size_t remaining = *content_size;
2353
2354 /* Determine length of padding by skipping zeroes from the back. */
2355 do
2356 {
2357 if( remaining == 0 )
2358 return( -1 );
2359 remaining--;
2360 } while( content[ remaining ] == 0 );
2361
2362 *content_size = remaining;
2363 *rec_type = content[ remaining ];
2364
2365 return( 0 );
2366}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002367#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002368
Hanno Becker99abf512019-05-20 14:50:53 +01002369/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002370 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002371static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002372 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002373 mbedtls_record *rec )
2374{
Hanno Becker99abf512019-05-20 14:50:53 +01002375 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002376 *
2377 * additional_data = seq_num + TLSCompressed.type +
2378 * TLSCompressed.version + TLSCompressed.length;
2379 *
Hanno Becker99abf512019-05-20 14:50:53 +01002380 * For the CID extension, this is extended as follows
2381 * (quoting draft-ietf-tls-dtls-connection-id-05,
2382 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002383 *
2384 * additional_data = seq_num + DTLSPlaintext.type +
2385 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002386 * cid +
2387 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002388 * length_of_DTLSInnerPlaintext;
2389 */
2390
Hanno Becker3307b532017-12-27 21:37:21 +00002391 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2392 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002393 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002394
Hanno Beckera5a2b082019-05-15 14:03:01 +01002395#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002396 if( rec->cid_len != 0 )
2397 {
2398 memcpy( add_data + 11, rec->cid, rec->cid_len );
2399 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002400 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2401 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002402 *add_data_len = 13 + 1 + rec->cid_len;
2403 }
2404 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002405#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002406 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002407 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002408 *add_data_len = 13;
2409 }
Hanno Becker3307b532017-12-27 21:37:21 +00002410}
2411
Hanno Becker611a83b2018-01-03 14:27:32 +00002412int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2413 mbedtls_ssl_transform *transform,
2414 mbedtls_record *rec,
2415 int (*f_rng)(void *, unsigned char *, size_t),
2416 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002419 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002420 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002421 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002422 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002423 size_t post_avail;
2424
2425 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002426#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002427 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002428 ((void) ssl);
2429#endif
2430
2431 /* The PRNG is used for dynamic IV generation that's used
2432 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2433#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2434 ( defined(MBEDTLS_AES_C) || \
2435 defined(MBEDTLS_ARIA_C) || \
2436 defined(MBEDTLS_CAMELLIA_C) ) && \
2437 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2438 ((void) f_rng);
2439 ((void) p_rng);
2440#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Hanno Becker3307b532017-12-27 21:37:21 +00002444 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002445 {
Hanno Becker3307b532017-12-27 21:37:21 +00002446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2447 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2448 }
Hanno Becker505089d2019-05-01 09:45:57 +01002449 if( rec == NULL
2450 || rec->buf == NULL
2451 || rec->buf_len < rec->data_offset
2452 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002453#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002454 || rec->cid_len != 0
2455#endif
2456 )
Hanno Becker3307b532017-12-27 21:37:21 +00002457 {
2458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002460 }
2461
Hanno Becker3307b532017-12-27 21:37:21 +00002462 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002463 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002465 data, rec->data_len );
2466
2467 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2468
2469 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2470 {
2471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2472 (unsigned) rec->data_len,
2473 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2475 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002476
Hanno Beckera5a2b082019-05-15 14:03:01 +01002477#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002478 /*
2479 * Add CID information
2480 */
2481 rec->cid_len = transform->out_cid_len;
2482 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2483 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002484
2485 if( rec->cid_len != 0 )
2486 {
2487 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002488 * Wrap plaintext into DTLSInnerPlaintext structure.
2489 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002490 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002491 * Note that this changes `rec->data_len`, and hence
2492 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002493 */
2494 if( ssl_cid_build_inner_plaintext( data,
2495 &rec->data_len,
2496 post_avail,
2497 rec->type ) != 0 )
2498 {
2499 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2500 }
2501
2502 rec->type = MBEDTLS_SSL_MSG_CID;
2503 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002504#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002505
Hanno Becker92c930f2019-04-29 17:31:37 +01002506 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002509 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002511#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 if( mode == MBEDTLS_MODE_STREAM ||
2513 ( mode == MBEDTLS_MODE_CBC
2514#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002515 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002516#endif
2517 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 {
Hanno Becker3307b532017-12-27 21:37:21 +00002519 if( post_avail < transform->maclen )
2520 {
2521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2522 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2523 }
2524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002526 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2527 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002528 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002529 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002530 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2531 data, rec->data_len, rec->ctr, rec->type, mac );
2532 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002533 }
2534 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2537 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002538 if( mbedtls_ssl_ver_geq(
2539 mbedtls_ssl_transform_get_minor_ver( transform ),
2540 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002541 {
Hanno Becker992b6872017-11-09 18:57:39 +00002542 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2543
Hanno Beckere83efe62019-04-29 13:52:53 +01002544 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002545
Hanno Becker3307b532017-12-27 21:37:21 +00002546 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002547 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002548 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2549 data, rec->data_len );
2550 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2551 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2552
2553 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002554 }
2555 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002556#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002560 }
2561
Hanno Becker3307b532017-12-27 21:37:21 +00002562 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2563 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002564
Hanno Becker3307b532017-12-27 21:37:21 +00002565 rec->data_len += transform->maclen;
2566 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002567 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002568 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002569#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002571 /*
2572 * Encrypt
2573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2575 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002577 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002578 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002580 "including %d bytes of padding",
2581 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
Hanno Becker3307b532017-12-27 21:37:21 +00002583 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2584 transform->iv_enc, transform->ivlen,
2585 data, rec->data_len,
2586 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002589 return( ret );
2590 }
2591
Hanno Becker3307b532017-12-27 21:37:21 +00002592 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2595 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 }
Paul Bakker68884e32013-01-07 18:20:04 +01002598 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002600
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002601#if defined(MBEDTLS_GCM_C) || \
2602 defined(MBEDTLS_CCM_C) || \
2603 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002605 mode == MBEDTLS_MODE_CCM ||
2606 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002607 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002608 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002610 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002611
Hanno Becker3307b532017-12-27 21:37:21 +00002612 /* Check that there's space for both the authentication tag
2613 * and the explicit IV before and after the record content. */
2614 if( post_avail < transform->taglen ||
2615 rec->data_offset < explicit_iv_len )
2616 {
2617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2618 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2619 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002620
Paul Bakker68884e32013-01-07 18:20:04 +01002621 /*
2622 * Generate IV
2623 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002624 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2625 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002626 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002627 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002628 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2629 explicit_iv_len );
2630 /* Prefix record content with explicit IV. */
2631 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 }
2633 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2634 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002635 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002636 unsigned char i;
2637
2638 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2639
2640 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002641 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002642 }
2643 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002644 {
2645 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002648 }
2649
Hanno Beckere83efe62019-04-29 13:52:53 +01002650 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002651
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002652 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2653 iv, transform->ivlen );
2654 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002655 data - explicit_iv_len, explicit_iv_len );
2656 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002657 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002659 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002660 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002661
Paul Bakker68884e32013-01-07 18:20:04 +01002662 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002663 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002664 */
Hanno Becker3307b532017-12-27 21:37:21 +00002665
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002666 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002667 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002668 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002669 data, rec->data_len, /* source */
2670 data, &rec->data_len, /* destination */
2671 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002674 return( ret );
2675 }
2676
Hanno Becker3307b532017-12-27 21:37:21 +00002677 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2678 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002679
Hanno Becker3307b532017-12-27 21:37:21 +00002680 rec->data_len += transform->taglen + explicit_iv_len;
2681 rec->data_offset -= explicit_iv_len;
2682 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002683 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2687#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002688 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002691 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002692 size_t padlen, i;
2693 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002694
Hanno Becker3307b532017-12-27 21:37:21 +00002695 /* Currently we're always using minimal padding
2696 * (up to 255 bytes would be allowed). */
2697 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2698 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 padlen = 0;
2700
Hanno Becker3307b532017-12-27 21:37:21 +00002701 /* Check there's enough space in the buffer for the padding. */
2702 if( post_avail < padlen + 1 )
2703 {
2704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2705 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2706 }
2707
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002709 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Hanno Becker3307b532017-12-27 21:37:21 +00002711 rec->data_len += padlen + 1;
2712 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002715 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002716 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2717 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002718 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002719 if( mbedtls_ssl_ver_geq(
2720 mbedtls_ssl_transform_get_minor_ver( transform ),
2721 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002722 {
Hanno Becker3307b532017-12-27 21:37:21 +00002723 if( f_rng == NULL )
2724 {
2725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2727 }
2728
2729 if( rec->data_offset < transform->ivlen )
2730 {
2731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2732 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2733 }
2734
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002735 /*
2736 * Generate IV
2737 */
Hanno Becker3307b532017-12-27 21:37:21 +00002738 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002739 if( ret != 0 )
2740 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002741
Hanno Becker3307b532017-12-27 21:37:21 +00002742 memcpy( data - transform->ivlen, transform->iv_enc,
2743 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002744
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002745 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002749 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002750 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002751 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Hanno Becker3307b532017-12-27 21:37:21 +00002753 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2754 transform->iv_enc,
2755 transform->ivlen,
2756 data, rec->data_len,
2757 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002760 return( ret );
2761 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002762
Hanno Becker3307b532017-12-27 21:37:21 +00002763 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2766 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002767 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002770 if( mbedtls_ssl_ver_lt(
2771 mbedtls_ssl_transform_get_minor_ver( transform ),
2772 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002773 {
2774 /*
2775 * Save IV in SSL3 and TLS1
2776 */
Hanno Becker3307b532017-12-27 21:37:21 +00002777 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2778 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 }
Hanno Becker3307b532017-12-27 21:37:21 +00002780 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002781#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002782 {
2783 data -= transform->ivlen;
2784 rec->data_offset -= transform->ivlen;
2785 rec->data_len += transform->ivlen;
2786 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002789 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002790 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002791 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2792
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002793 /*
2794 * MAC(MAC_write_key, seq_num +
2795 * TLSCipherText.type +
2796 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002797 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002798 * IV + // except for TLS 1.0
2799 * ENC(content + padding + padding_length));
2800 */
Hanno Becker3307b532017-12-27 21:37:21 +00002801
2802 if( post_avail < transform->maclen)
2803 {
2804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2805 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2806 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002807
Hanno Beckere83efe62019-04-29 13:52:53 +01002808 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002811 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002812 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002813
Hanno Becker3307b532017-12-27 21:37:21 +00002814 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002815 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002816 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2817 data, rec->data_len );
2818 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2819 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002820
Hanno Becker3307b532017-12-27 21:37:21 +00002821 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002822
Hanno Becker3307b532017-12-27 21:37:21 +00002823 rec->data_len += transform->maclen;
2824 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002825 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002829 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002831 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837 /* Make extra sure authentication was performed, exactly once */
2838 if( auth_done != 1 )
2839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2841 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002842 }
2843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
2846 return( 0 );
2847}
2848
Hanno Becker40478be2019-07-12 08:23:59 +01002849int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002850 mbedtls_ssl_transform *transform,
2851 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002852{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002853 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002855 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002856#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002857 size_t padlen = 0, correct = 1;
2858#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002859 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002860 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002861 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002862
Hanno Becker611a83b2018-01-03 14:27:32 +00002863#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002864 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002865 ((void) ssl);
2866#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002869 if( rec == NULL ||
2870 rec->buf == NULL ||
2871 rec->buf_len < rec->data_offset ||
2872 rec->buf_len - rec->data_offset < rec->data_len )
2873 {
2874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002876 }
2877
Hanno Becker4c6876b2017-12-27 21:28:58 +00002878 data = rec->buf + rec->data_offset;
2879 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
Hanno Beckera5a2b082019-05-15 14:03:01 +01002881#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002882 /*
2883 * Match record's CID with incoming CID.
2884 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002885 if( rec->cid_len != transform->in_cid_len ||
2886 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2887 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002888 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002889 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002890#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2893 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002894 {
2895 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002896 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2897 transform->iv_dec,
2898 transform->ivlen,
2899 data, rec->data_len,
2900 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002903 return( ret );
2904 }
2905
Hanno Becker4c6876b2017-12-27 21:28:58 +00002906 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2909 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002910 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 }
Paul Bakker68884e32013-01-07 18:20:04 +01002912 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002914#if defined(MBEDTLS_GCM_C) || \
2915 defined(MBEDTLS_CCM_C) || \
2916 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002918 mode == MBEDTLS_MODE_CCM ||
2919 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002920 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002921 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002922 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002923
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002924 /*
2925 * Prepare IV from explicit and implicit data.
2926 */
2927
2928 /* Check that there's enough space for the explicit IV
2929 * (at the beginning of the record) and the MAC (at the
2930 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002931 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002934 "+ taglen (%d)", rec->data_len,
2935 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002937 }
Paul Bakker68884e32013-01-07 18:20:04 +01002938
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002939#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002940 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2941 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002942 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002943
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002944 /* Fixed */
2945 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2946 /* Explicit */
2947 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002948 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002949 else
2950#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2951#if defined(MBEDTLS_CHACHAPOLY_C)
2952 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002953 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002954 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002955 unsigned char i;
2956
2957 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2958
2959 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002960 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002961 }
2962 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002963#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002964 {
2965 /* Reminder if we ever add an AEAD mode with a different size */
2966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2967 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2968 }
2969
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002970 /* Group changes to data, data_len, and add_data, because
2971 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002972 data += explicit_iv_len;
2973 rec->data_offset += explicit_iv_len;
2974 rec->data_len -= explicit_iv_len + transform->taglen;
2975
Hanno Beckere83efe62019-04-29 13:52:53 +01002976 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002977 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002978 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002979
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002980 /* Because of the check above, we know that there are
2981 * explicit_iv_len Bytes preceeding data, and taglen
2982 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002983 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002984 * mbedtls_cipher_auth_decrypt() below. */
2985
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002986 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002987 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002988 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002989
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002990 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002991 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002992 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002993 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2994 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002995 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002996 data, rec->data_len,
2997 data, &olen,
2998 data + rec->data_len,
2999 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3004 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003005
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003006 return( ret );
3007 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003008 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003009
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003010 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003011 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3014 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003015 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3019#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003020 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003023 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003024
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 /*
Paul Bakker45829992013-01-03 14:52:21 +01003026 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003028#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003029 if( mbedtls_ssl_ver_geq(
3030 mbedtls_ssl_transform_get_minor_ver( transform ),
3031 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003032 {
3033 /* The ciphertext is prefixed with the CBC IV. */
3034 minlen += transform->ivlen;
3035 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003036#endif
Paul Bakker45829992013-01-03 14:52:21 +01003037
Hanno Becker4c6876b2017-12-27 21:28:58 +00003038 /* Size considerations:
3039 *
3040 * - The CBC cipher text must not be empty and hence
3041 * at least of size transform->ivlen.
3042 *
3043 * Together with the potential IV-prefix, this explains
3044 * the first of the two checks below.
3045 *
3046 * - The record must contain a MAC, either in plain or
3047 * encrypted, depending on whether Encrypt-then-MAC
3048 * is used or not.
3049 * - If it is, the message contains the IV-prefix,
3050 * the CBC ciphertext, and the MAC.
3051 * - If it is not, the padded plaintext, and hence
3052 * the CBC ciphertext, has at least length maclen + 1
3053 * because there is at least the padding length byte.
3054 *
3055 * As the CBC ciphertext is not empty, both cases give the
3056 * lower bound minlen + maclen + 1 on the record size, which
3057 * we test for in the second check below.
3058 */
3059 if( rec->data_len < minlen + transform->ivlen ||
3060 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003063 "+ 1 ) ( + expl IV )", rec->data_len,
3064 transform->ivlen,
3065 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003067 }
3068
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003069 /*
3070 * Authenticate before decrypt if enabled
3071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003073 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003074 {
Hanno Becker992b6872017-11-09 18:57:39 +00003075 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003078
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003079 /* Update data_len in tandem with add_data.
3080 *
3081 * The subtraction is safe because of the previous check
3082 * data_len >= minlen + maclen + 1.
3083 *
3084 * Afterwards, we know that data + data_len is followed by at
3085 * least maclen Bytes, which justifies the call to
3086 * mbedtls_ssl_safer_memcmp() below.
3087 *
3088 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003089 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003090 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003091
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003092 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003093 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3094 add_data_len );
3095 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3096 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003097 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3098 data, rec->data_len );
3099 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3100 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003101
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3103 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003104 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003105 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003106
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003107 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003108 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3109 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003113 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003114 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003117
3118 /*
3119 * Check length sanity
3120 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003121
3122 /* We know from above that data_len > minlen >= 0,
3123 * so the following check in particular implies that
3124 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003125 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003128 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003130 }
3131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003133 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003134 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003135 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003136 if( mbedtls_ssl_ver_geq(
3137 mbedtls_ssl_transform_get_minor_ver( transform ),
3138 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003139 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003140 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003141 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003142
Hanno Becker4c6876b2017-12-27 21:28:58 +00003143 data += transform->ivlen;
3144 rec->data_offset += transform->ivlen;
3145 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003148
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003149 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3150
Hanno Becker4c6876b2017-12-27 21:28:58 +00003151 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3152 transform->iv_dec, transform->ivlen,
3153 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003156 return( ret );
3157 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003158
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003159 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003160 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003164 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003167 if( mbedtls_ssl_ver_lt(
3168 mbedtls_ssl_transform_get_minor_ver( transform ),
3169 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003170 {
3171 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003172 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3173 * records is equivalent to CBC decryption of the concatenation
3174 * of the records; in other words, IVs are maintained across
3175 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003176 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003177 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3178 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003179 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003180#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003181
Hanno Becker4c6876b2017-12-27 21:28:58 +00003182 /* Safe since data_len >= minlen + maclen + 1, so after having
3183 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003184 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3185 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003186 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003187
Hanno Becker4c6876b2017-12-27 21:28:58 +00003188 if( auth_done == 1 )
3189 {
3190 correct *= ( rec->data_len >= padlen + 1 );
3191 padlen *= ( rec->data_len >= padlen + 1 );
3192 }
3193 else
Paul Bakker45829992013-01-03 14:52:21 +01003194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003196 if( rec->data_len < transform->maclen + padlen + 1 )
3197 {
3198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3199 rec->data_len,
3200 transform->maclen,
3201 padlen + 1 ) );
3202 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003203#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003204
3205 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3206 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003208
Hanno Becker4c6876b2017-12-27 21:28:58 +00003209 padlen++;
3210
3211 /* Regardless of the validity of the padding,
3212 * we have data_len >= padlen here. */
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003215 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3216 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003217 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003218 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220#if defined(MBEDTLS_SSL_DEBUG_ALL)
3221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003222 "should be no more than %d",
3223 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003224#endif
Paul Bakker45829992013-01-03 14:52:21 +01003225 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 }
3227 }
3228 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3230#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3231 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003232 if( mbedtls_ssl_ver_gt(
3233 mbedtls_ssl_transform_get_minor_ver( transform ),
3234 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003236 /* The padding check involves a series of up to 256
3237 * consecutive memory reads at the end of the record
3238 * plaintext buffer. In order to hide the length and
3239 * validity of the padding, always perform exactly
3240 * `min(256,plaintext_len)` reads (but take into account
3241 * only the last `padlen` bytes for the padding check). */
3242 size_t pad_count = 0;
3243 size_t real_count = 0;
3244 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003245
Hanno Becker4c6876b2017-12-27 21:28:58 +00003246 /* Index of first padding byte; it has been ensured above
3247 * that the subtraction is safe. */
3248 size_t const padding_idx = rec->data_len - padlen;
3249 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3250 size_t const start_idx = rec->data_len - num_checks;
3251 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003252
Hanno Becker4c6876b2017-12-27 21:28:58 +00003253 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003254 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003255 real_count |= ( idx >= padding_idx );
3256 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003257 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003258 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003261 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003263#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003264 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003266 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3268 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3271 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003272 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003273
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274 /* If the padding was found to be invalid, padlen == 0
3275 * and the subtraction is safe. If the padding was found valid,
3276 * padlen hasn't been changed and the previous assertion
3277 * data_len >= padlen still holds. */
3278 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003282 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003287
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003288#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003290 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003291#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
3293 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003294 * Authenticate if not done yet.
3295 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003297#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003298 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003299 {
Hanno Becker992b6872017-11-09 18:57:39 +00003300 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003301
Hanno Becker4c6876b2017-12-27 21:28:58 +00003302 /* If the initial value of padlen was such that
3303 * data_len < maclen + padlen + 1, then padlen
3304 * got reset to 1, and the initial check
3305 * data_len >= minlen + maclen + 1
3306 * guarantees that at this point we still
3307 * have at least data_len >= maclen.
3308 *
3309 * If the initial value of padlen was such that
3310 * data_len >= maclen + padlen + 1, then we have
3311 * subtracted either padlen + 1 (if the padding was correct)
3312 * or 0 (if the padding was incorrect) since then,
3313 * hence data_len >= maclen in any case.
3314 */
3315 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003316 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003319 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3320 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003321 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003322 ssl_mac( &transform->md_ctx_dec,
3323 transform->mac_dec,
3324 data, rec->data_len,
3325 rec->ctr, rec->type,
3326 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003327 }
3328 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3330#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3331 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003332 if( mbedtls_ssl_ver_gt(
3333 mbedtls_ssl_transform_get_minor_ver( transform ),
3334 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003335 {
3336 /*
3337 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003338 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003339 *
3340 * Known timing attacks:
3341 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3342 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003343 * To compensate for different timings for the MAC calculation
3344 * depending on how much padding was removed (which is determined
3345 * by padlen), process extra_run more blocks through the hash
3346 * function.
3347 *
3348 * The formula in the paper is
3349 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3350 * where L1 is the size of the header plus the decrypted message
3351 * plus CBC padding and L2 is the size of the header plus the
3352 * decrypted message. This is for an underlying hash function
3353 * with 64-byte blocks.
3354 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3355 * correctly. We round down instead of up, so -56 is the correct
3356 * value for our calculations instead of -55.
3357 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003358 * Repeat the formula rather than defining a block_size variable.
3359 * This avoids requiring division by a variable at runtime
3360 * (which would be marginally less efficient and would require
3361 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003362 */
3363 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003364 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003365
3366 /*
3367 * The next two sizes are the minimum and maximum values of
3368 * in_msglen over all padlen values.
3369 *
3370 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003371 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003372 *
3373 * Note that max_len + maclen is never more than the buffer
3374 * length, as we previously did in_msglen -= maclen too.
3375 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003376 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003377 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3378
Hanno Becker4c6876b2017-12-27 21:28:58 +00003379 memset( tmp, 0, sizeof( tmp ) );
3380
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003381 switch( mbedtls_md_get_type(
3382 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003383 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003384#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3385 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003386 case MBEDTLS_MD_MD5:
3387 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003388 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003389 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003390 extra_run =
3391 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3392 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003393 break;
3394#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003395#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003396 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003397 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003398 extra_run =
3399 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3400 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003401 break;
3402#endif
3403 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003405 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3406 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003407
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003408 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003409
Hanno Beckere83efe62019-04-29 13:52:53 +01003410 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3411 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003412 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3413 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003414 /* Make sure we access everything even when padlen > 0. This
3415 * makes the synchronisation requirements for just-in-time
3416 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003417 ssl_read_memory( data + rec->data_len, padlen );
3418 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003419
3420 /* Call mbedtls_md_process at least once due to cache attacks
3421 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003422 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003423 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003424
Hanno Becker4c6876b2017-12-27 21:28:58 +00003425 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003426
3427 /* Make sure we access all the memory that could contain the MAC,
3428 * before we check it in the next code block. This makes the
3429 * synchronisation requirements for just-in-time Prime+Probe
3430 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003431 ssl_read_memory( data + min_len,
3432 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003433 }
3434 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3436 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3439 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003442#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003443 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3444 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003446
Hanno Becker4c6876b2017-12-27 21:28:58 +00003447 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3448 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450#if defined(MBEDTLS_SSL_DEBUG_ALL)
3451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003452#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003453 correct = 0;
3454 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003455 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003456 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003457
3458 /*
3459 * Finally check the correct flag
3460 */
3461 if( correct == 0 )
3462 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003463#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003464
3465 /* Make extra sure authentication was performed, exactly once */
3466 if( auth_done != 1 )
3467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
Hanno Beckera5a2b082019-05-15 14:03:01 +01003472#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003473 if( rec->cid_len != 0 )
3474 {
3475 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3476 &rec->type );
3477 if( ret != 0 )
3478 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3479 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003480#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
3484 return( 0 );
3485}
3486
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003487#undef MAC_NONE
3488#undef MAC_PLAINTEXT
3489#undef MAC_CIPHERTEXT
3490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003492/*
3493 * Compression/decompression functions
3494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003496{
3497 int ret;
3498 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003499 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003500 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003501 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003504
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003505 if( len_pre == 0 )
3506 return( 0 );
3507
Paul Bakker2770fbd2012-07-03 13:30:23 +00003508 memcpy( msg_pre, ssl->out_msg, len_pre );
3509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003511 ssl->out_msglen ) );
3512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514 ssl->out_msg, ssl->out_msglen );
3515
Paul Bakker48916f92012-09-16 19:57:18 +00003516 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3517 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3518 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003519 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520
Paul Bakker48916f92012-09-16 19:57:18 +00003521 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003522 if( ret != Z_OK )
3523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3525 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003526 }
3527
Angus Grattond8213d02016-05-25 20:56:48 +10003528 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003529 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003532 ssl->out_msglen ) );
3533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003535 ssl->out_msg, ssl->out_msglen );
3536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003538
3539 return( 0 );
3540}
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003543{
3544 int ret;
3545 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003546 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003548 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003551
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003552 if( len_pre == 0 )
3553 return( 0 );
3554
Paul Bakker2770fbd2012-07-03 13:30:23 +00003555 memcpy( msg_pre, ssl->in_msg, len_pre );
3556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003558 ssl->in_msglen ) );
3559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003561 ssl->in_msg, ssl->in_msglen );
3562
Paul Bakker48916f92012-09-16 19:57:18 +00003563 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3564 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3565 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003566 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003567 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003568
Paul Bakker48916f92012-09-16 19:57:18 +00003569 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003570 if( ret != Z_OK )
3571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3573 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003574 }
3575
Angus Grattond8213d02016-05-25 20:56:48 +10003576 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003577 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003580 ssl->in_msglen ) );
3581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003583 ssl->in_msg, ssl->in_msglen );
3584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003586
3587 return( 0 );
3588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3592static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594#if defined(MBEDTLS_SSL_PROTO_DTLS)
3595static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003596{
3597 /* If renegotiation is not enforced, retransmit until we would reach max
3598 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003599 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003600 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003601 uint32_t ratio =
3602 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3603 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003604 unsigned char doublings = 1;
3605
3606 while( ratio != 0 )
3607 {
3608 ++doublings;
3609 ratio >>= 1;
3610 }
3611
3612 if( ++ssl->renego_records_seen > doublings )
3613 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003615 return( 0 );
3616 }
3617 }
3618
3619 return( ssl_write_hello_request( ssl ) );
3620}
3621#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003623
Paul Bakker5121ce52009-01-03 21:22:43 +00003624/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003625 * Fill the input message buffer by appending data to it.
3626 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003627 *
3628 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3629 * available (from this read and/or a previous one). Otherwise, an error code
3630 * is returned (possibly EOF or WANT_READ).
3631 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003632 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3633 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3634 * since we always read a whole datagram at once.
3635 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003636 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003637 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003640{
Paul Bakker23986e52011-04-24 08:57:21 +00003641 int ret;
3642 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Hanno Beckera58a8962019-06-13 16:11:15 +01003646 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3647 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003650 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003652 }
3653
Angus Grattond8213d02016-05-25 20:56:48 +10003654 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003658 }
3659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003661 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003663 uint32_t timeout;
3664
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003665 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003666 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3667 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003668 {
3669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3670 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3672 }
3673
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003674 /*
3675 * The point is, we need to always read a full datagram at once, so we
3676 * sometimes read more then requested, and handle the additional data.
3677 * It could be the rest of the current record (while fetching the
3678 * header) and/or some other records in the same datagram.
3679 */
3680
3681 /*
3682 * Move to the next record in the already read datagram if applicable
3683 */
3684 if( ssl->next_record_offset != 0 )
3685 {
3686 if( ssl->in_left < ssl->next_record_offset )
3687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003690 }
3691
3692 ssl->in_left -= ssl->next_record_offset;
3693
3694 if( ssl->in_left != 0 )
3695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003697 ssl->next_record_offset ) );
3698 memmove( ssl->in_hdr,
3699 ssl->in_hdr + ssl->next_record_offset,
3700 ssl->in_left );
3701 }
3702
3703 ssl->next_record_offset = 0;
3704 }
3705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003708
3709 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003710 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003711 */
3712 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003715 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003716 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003717
3718 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003719 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003720 * are not at the beginning of a new record, the caller did something
3721 * wrong.
3722 */
3723 if( ssl->in_left != 0 )
3724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003727 }
3728
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003729 /*
3730 * Don't even try to read if time's out already.
3731 * This avoids by-passing the timer when repeatedly receiving messages
3732 * that will end up being dropped.
3733 */
3734 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003735 {
3736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003737 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003738 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003739 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003740 {
Angus Grattond8213d02016-05-25 20:56:48 +10003741 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003744 timeout = ssl->handshake->retransmit_timeout;
3745 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003746 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003749
Hanno Beckera58a8962019-06-13 16:11:15 +01003750 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3751 {
3752 ret = mbedtls_ssl_get_recv_timeout( ssl )
3753 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3754 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003755 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003756 {
3757 ret = mbedtls_ssl_get_recv( ssl )
3758 ( ssl->p_bio, ssl->in_hdr, len );
3759 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003762
3763 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003765 }
3766
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003767 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003770 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003773 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003774 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003777 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003778 }
3779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003783 return( ret );
3784 }
3785
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003786 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003789 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3790 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003792 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003793 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003796 return( ret );
3797 }
3798
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003799 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003800 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003801#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003802 }
3803
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 if( ret < 0 )
3805 return( ret );
3806
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003807 ssl->in_left = ret;
3808 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003809 MBEDTLS_SSL_TRANSPORT_ELSE
3810#endif /* MBEDTLS_SSL_PROTO_DTLS */
3811#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003814 ssl->in_left, nb_want ) );
3815
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003816 while( ssl->in_left < nb_want )
3817 {
3818 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003819
3820 if( ssl_check_timer( ssl ) != 0 )
3821 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3822 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003823 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003824 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003825 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003826 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3827 ssl->in_hdr + ssl->in_left, len,
3828 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003829 }
3830 else
3831 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003832 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003833 ssl->in_hdr + ssl->in_left, len );
3834 }
3835 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003838 ssl->in_left, nb_want ) );
3839 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003840
3841 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003843
3844 if( ret < 0 )
3845 return( ret );
3846
mohammad160352aecb92018-03-28 23:41:40 -07003847 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003848 {
Darryl Green11999bb2018-03-13 15:22:58 +00003849 MBEDTLS_SSL_DEBUG_MSG( 1,
3850 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003851 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3853 }
3854
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003855 ssl->in_left += ret;
3856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003858#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
3862 return( 0 );
3863}
3864
3865/*
3866 * Flush any data not yet written
3867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003869{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003870 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003871 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003874
Hanno Beckera58a8962019-06-13 16:11:15 +01003875 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003878 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003880 }
3881
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003882 /* Avoid incrementing counter if data is flushed */
3883 if( ssl->out_left == 0 )
3884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003886 return( 0 );
3887 }
3888
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 while( ssl->out_left > 0 )
3890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003892 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003893
Hanno Becker2b1e3542018-08-06 11:19:13 +01003894 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003895 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003898
3899 if( ret <= 0 )
3900 return( ret );
3901
mohammad160352aecb92018-03-28 23:41:40 -07003902 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003903 {
Darryl Green11999bb2018-03-13 15:22:58 +00003904 MBEDTLS_SSL_DEBUG_MSG( 1,
3905 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003906 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3908 }
3909
Paul Bakker5121ce52009-01-03 21:22:43 +00003910 ssl->out_left -= ret;
3911 }
3912
Hanno Becker2b1e3542018-08-06 11:19:13 +01003913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003914 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003915 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003916 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003917 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003918 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003919#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003920#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003921 {
3922 ssl->out_hdr = ssl->out_buf + 8;
3923 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003924#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003925 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003928
3929 return( 0 );
3930}
3931
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003932/*
3933 * Functions to handle the DTLS retransmission state machine
3934 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003936/*
3937 * Append current handshake message to current outgoing flight
3938 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003940{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3943 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3944 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003945
3946 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003947 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003948 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003951 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952 }
3953
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003954 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003958 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003959 }
3960
3961 /* Copy current handshake message with headers */
3962 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3963 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003964 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003965 msg->next = NULL;
3966
3967 /* Append to the current flight */
3968 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003969 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970 else
3971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003973 while( cur->next != NULL )
3974 cur = cur->next;
3975 cur->next = msg;
3976 }
3977
Hanno Becker3b235902018-08-06 09:54:53 +01003978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003979 return( 0 );
3980}
3981
3982/*
3983 * Free the current flight of handshake messages
3984 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 mbedtls_ssl_flight_item *cur = flight;
3988 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003989
3990 while( cur != NULL )
3991 {
3992 next = cur->next;
3993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 mbedtls_free( cur->p );
3995 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003996
3997 cur = next;
3998 }
3999}
4000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4002static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004003#endif
4004
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004005/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004006 * Swap transform_out and out_ctr with the alternative ones
4007 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004011 unsigned char tmp_out_ctr[8];
4012
4013 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004016 return;
4017 }
4018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004020
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004021 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004022 tmp_transform = ssl->transform_out;
4023 ssl->transform_out = ssl->handshake->alt_transform_out;
4024 ssl->handshake->alt_transform_out = tmp_transform;
4025
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004026 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01004027 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4028 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004029 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004030
4031 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004032 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4035 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4040 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004041 }
4042 }
4043#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004044}
4045
4046/*
4047 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004048 */
4049int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4050{
4051 int ret = 0;
4052
4053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4054
4055 ret = mbedtls_ssl_flight_transmit( ssl );
4056
4057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4058
4059 return( ret );
4060}
4061
4062/*
4063 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064 *
4065 * Need to remember the current message in case flush_output returns
4066 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004067 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004069int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004070{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004071 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004074 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004075 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004077
4078 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004079 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004080 ssl_swap_epochs( ssl );
4081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004083 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004084
4085 while( ssl->handshake->cur_msg != NULL )
4086 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004087 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004088 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004089
Hanno Beckere1dcb032018-08-17 16:47:58 +01004090 int const is_finished =
4091 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4092 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4093
Hanno Becker04da1892018-08-14 13:22:10 +01004094 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4095 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4096
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004097 /* Swap epochs before sending Finished: we can't do it after
4098 * sending ChangeCipherSpec, in case write returns WANT_READ.
4099 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004100 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004101 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004103 ssl_swap_epochs( ssl );
4104 }
4105
Hanno Becker67bc7c32018-08-06 11:33:50 +01004106 ret = ssl_get_remaining_payload_in_datagram( ssl );
4107 if( ret < 0 )
4108 return( ret );
4109 max_frag_len = (size_t) ret;
4110
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004111 /* CCS is copied as is, while HS messages may need fragmentation */
4112 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4113 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004114 if( max_frag_len == 0 )
4115 {
4116 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4117 return( ret );
4118
4119 continue;
4120 }
4121
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004122 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004123 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004124 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004125
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004126 /* Update position inside current message */
4127 ssl->handshake->cur_msg_p += cur->len;
4128 }
4129 else
4130 {
4131 const unsigned char * const p = ssl->handshake->cur_msg_p;
4132 const size_t hs_len = cur->len - 12;
4133 const size_t frag_off = p - ( cur->p + 12 );
4134 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004135 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004136
Hanno Beckere1dcb032018-08-17 16:47:58 +01004137 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004138 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004139 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004140 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004141
Hanno Becker67bc7c32018-08-06 11:33:50 +01004142 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4143 return( ret );
4144
4145 continue;
4146 }
4147 max_hs_frag_len = max_frag_len - 12;
4148
4149 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4150 max_hs_frag_len : rem_len;
4151
4152 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004153 {
4154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004155 (unsigned) cur_hs_frag_len,
4156 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004157 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004158
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004159 /* Messages are stored with handshake headers as if not fragmented,
4160 * copy beginning of headers then fill fragmentation fields.
4161 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4162 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004163
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004164 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4165 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4166 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004167
4168 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4169
Hanno Becker3f7b9732018-08-28 09:53:25 +01004170 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004171 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4172 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004173 ssl->out_msgtype = cur->type;
4174
4175 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004176 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004177 }
4178
4179 /* If done with the current message move to the next one if any */
4180 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4181 {
4182 if( cur->next != NULL )
4183 {
4184 ssl->handshake->cur_msg = cur->next;
4185 ssl->handshake->cur_msg_p = cur->next->p + 12;
4186 }
4187 else
4188 {
4189 ssl->handshake->cur_msg = NULL;
4190 ssl->handshake->cur_msg_p = NULL;
4191 }
4192 }
4193
4194 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004195 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004198 return( ret );
4199 }
4200 }
4201
Hanno Becker67bc7c32018-08-06 11:33:50 +01004202 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4203 return( ret );
4204
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004205 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4207 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004208 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004210 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004211 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4212 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004213
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004215
4216 return( 0 );
4217}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004218
4219/*
4220 * To be called when the last message of an incoming flight is received.
4221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004223{
4224 /* We won't need to resend that one any more */
4225 ssl_flight_free( ssl->handshake->flight );
4226 ssl->handshake->flight = NULL;
4227 ssl->handshake->cur_msg = NULL;
4228
4229 /* The next incoming flight will start with this msg_seq */
4230 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4231
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004232 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004233 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004234
Hanno Becker0271f962018-08-16 13:23:47 +01004235 /* Clear future message buffering structure. */
4236 ssl_buffering_free( ssl );
4237
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004238 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004239 ssl_set_timer( ssl, 0 );
4240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004241 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4242 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004245 }
4246 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004248}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004249
4250/*
4251 * To be called when the last message of an outgoing flight is send.
4252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004254{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004255 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004256 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4259 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004262 }
4263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004267
Paul Bakker5121ce52009-01-03 21:22:43 +00004268/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004269 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004270 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004271
4272/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004273 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004274 *
4275 * - fill in handshake headers
4276 * - update handshake checksum
4277 * - DTLS: save message for resending
4278 * - then pass to the record layer
4279 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004280 * DTLS: except for HelloRequest, messages are only queued, and will only be
4281 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004282 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004283 * Inputs:
4284 * - ssl->out_msglen: 4 + actual handshake message len
4285 * (4 is the size of handshake headers for TLS)
4286 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4287 * - ssl->out_msg + 4: the handshake message body
4288 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004289 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004290 * - ssl->out_msglen: the length of the record contents
4291 * (including handshake headers but excluding record headers)
4292 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004293 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004294int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004295{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004296 int ret;
4297 const size_t hs_len = ssl->out_msglen - 4;
4298 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4301
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004302 /*
4303 * Sanity checks
4304 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004305 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004306 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4307 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004308 /* In SSLv3, the client might send a NoCertificate alert. */
4309#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004310 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004311 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004312 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4313 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004314#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4315 {
4316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4318 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004320
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004321 /* Whenever we send anything different from a
4322 * HelloRequest we should be in a handshake - double check. */
4323 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4324 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004325 ssl->handshake == NULL )
4326 {
4327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004331#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004332 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004333 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004335 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004338 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004339#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004340
Hanno Beckerb50a2532018-08-06 11:52:54 +01004341 /* Double-check that we did not exceed the bounds
4342 * of the outgoing record buffer.
4343 * This should never fail as the various message
4344 * writing functions must obey the bounds of the
4345 * outgoing record buffer, but better be safe.
4346 *
4347 * Note: We deliberately do not check for the MTU or MFL here.
4348 */
4349 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4350 {
4351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4352 "size %u, maximum %u",
4353 (unsigned) ssl->out_msglen,
4354 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4355 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4356 }
4357
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004358 /*
4359 * Fill handshake headers
4360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004363 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004364
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004365 /*
4366 * DTLS has additional fields in the Handshake layer,
4367 * between the length field and the actual payload:
4368 * uint16 message_seq;
4369 * uint24 fragment_offset;
4370 * uint24 fragment_length;
4371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004373 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004374 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004375 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004376 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004377 {
4378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4379 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004380 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004381 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4383 }
4384
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004385 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004386 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004387
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004388 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004389 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004390 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004391 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4392 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004393 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004394 }
4395 else
4396 {
4397 ssl->out_msg[4] = 0;
4398 ssl->out_msg[5] = 0;
4399 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004400
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004401 /* Handshake hashes are computed without fragmentation,
4402 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004403 memset( ssl->out_msg + 6, 0x00, 3 );
4404 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004405 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004407
Hanno Becker0207e532018-08-28 10:28:28 +01004408 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004409 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004410 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 }
4412
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004413 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004415 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004416 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4417 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004418 {
4419 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004422 return( ret );
4423 }
4424 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004425 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004426#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004427 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004428 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004429 {
4430 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4431 return( ret );
4432 }
4433 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004434
4435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4436
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004437 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004438}
4439
4440/*
4441 * Record layer functions
4442 */
4443
4444/*
4445 * Write current record.
4446 *
4447 * Uses:
4448 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4449 * - ssl->out_msglen: length of the record content (excl headers)
4450 * - ssl->out_msg: record content
4451 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004452int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004453{
4454 int ret, done = 0;
4455 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004456 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004457
4458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004461 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004463 {
4464 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004467 return( ret );
4468 }
4469
4470 len = ssl->out_msglen;
4471 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004472#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4475 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479 ret = mbedtls_ssl_hw_record_write( ssl );
4480 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4483 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004484 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004485
4486 if( ret == 0 )
4487 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004490 if( !done )
4491 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004492 unsigned i;
4493 size_t protected_record_size;
4494
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004495 /* Skip writing the record content type to after the encryption,
4496 * as it may change when using the CID extension. */
4497
Hanno Becker2881d802019-05-22 14:44:53 +01004498 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4499 mbedtls_ssl_get_minor_ver( ssl ),
4500 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004501
Hanno Becker19859472018-08-06 09:40:20 +01004502 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004503 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004504
Paul Bakker48916f92012-09-16 19:57:18 +00004505 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004506 {
Hanno Becker3307b532017-12-27 21:37:21 +00004507 mbedtls_record rec;
4508
4509 rec.buf = ssl->out_iv;
4510 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4511 ( ssl->out_iv - ssl->out_buf );
4512 rec.data_len = ssl->out_msglen;
4513 rec.data_offset = ssl->out_msg - rec.buf;
4514
4515 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004516 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4517 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004518 ssl->conf->transport, rec.ver );
4519 rec.type = ssl->out_msgtype;
4520
Hanno Beckera5a2b082019-05-15 14:03:01 +01004521#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004522 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004523 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004524#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004525
Hanno Becker611a83b2018-01-03 14:27:32 +00004526 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004527 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004528 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004531 return( ret );
4532 }
4533
Hanno Becker3307b532017-12-27 21:37:21 +00004534 if( rec.data_offset != 0 )
4535 {
4536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4538 }
4539
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004540 /* Update the record content type and CID. */
4541 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004543 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004544#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004545 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004546 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004547 }
4548
Hanno Becker43395762019-05-03 14:46:38 +01004549 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004550
4551#if defined(MBEDTLS_SSL_PROTO_DTLS)
4552 /* In case of DTLS, double-check that we don't exceed
4553 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004554 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004555 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004556 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004557 if( ret < 0 )
4558 return( ret );
4559
4560 if( protected_record_size > (size_t) ret )
4561 {
4562 /* Should never happen */
4563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4564 }
4565 }
4566#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004567
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004568 /* Now write the potentially updated record content type. */
4569 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004572 "version = [%d:%d], msglen = %d",
4573 ssl->out_hdr[0], ssl->out_hdr[1],
4574 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004577 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004578
4579 ssl->out_left += protected_record_size;
4580 ssl->out_hdr += protected_record_size;
4581 ssl_update_out_pointers( ssl, ssl->transform_out );
4582
Hanno Becker04484622018-08-06 09:49:38 +01004583 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4584 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4585 break;
4586
4587 /* The loop goes to its end iff the counter is wrapping */
4588 if( i == ssl_ep_len( ssl ) )
4589 {
4590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4591 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 }
4594
Hanno Becker67bc7c32018-08-06 11:33:50 +01004595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004596 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004597 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004598 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004599 size_t remaining;
4600 ret = ssl_get_remaining_payload_in_datagram( ssl );
4601 if( ret < 0 )
4602 {
4603 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4604 ret );
4605 return( ret );
4606 }
4607
4608 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004609 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004610 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004611 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004612 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004613 else
4614 {
Hanno Becker513815a2018-08-20 11:56:09 +01004615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004616 }
4617 }
4618#endif /* MBEDTLS_SSL_PROTO_DTLS */
4619
4620 if( ( flush == SSL_FORCE_FLUSH ) &&
4621 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 return( ret );
4625 }
4626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004628
4629 return( 0 );
4630}
4631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004633
4634static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4635{
4636 if( ssl->in_msglen < ssl->in_hslen ||
4637 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4638 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4639 {
4640 return( 1 );
4641 }
4642 return( 0 );
4643}
Hanno Becker44650b72018-08-16 12:51:11 +01004644
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004645static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004646{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004647 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004648}
4649
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004650static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004651{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004652 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004653}
4654
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004655static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004656{
4657 uint32_t msg_len, frag_off, frag_len;
4658
4659 msg_len = ssl_get_hs_total_len( ssl );
4660 frag_off = ssl_get_hs_frag_off( ssl );
4661 frag_len = ssl_get_hs_frag_len( ssl );
4662
4663 if( frag_off > msg_len )
4664 return( -1 );
4665
4666 if( frag_len > msg_len - frag_off )
4667 return( -1 );
4668
4669 if( frag_len + 12 > ssl->in_msglen )
4670 return( -1 );
4671
4672 return( 0 );
4673}
4674
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004675/*
4676 * Mark bits in bitmask (used for DTLS HS reassembly)
4677 */
4678static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4679{
4680 unsigned int start_bits, end_bits;
4681
4682 start_bits = 8 - ( offset % 8 );
4683 if( start_bits != 8 )
4684 {
4685 size_t first_byte_idx = offset / 8;
4686
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004687 /* Special case */
4688 if( len <= start_bits )
4689 {
4690 for( ; len != 0; len-- )
4691 mask[first_byte_idx] |= 1 << ( start_bits - len );
4692
4693 /* Avoid potential issues with offset or len becoming invalid */
4694 return;
4695 }
4696
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004697 offset += start_bits; /* Now offset % 8 == 0 */
4698 len -= start_bits;
4699
4700 for( ; start_bits != 0; start_bits-- )
4701 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4702 }
4703
4704 end_bits = len % 8;
4705 if( end_bits != 0 )
4706 {
4707 size_t last_byte_idx = ( offset + len ) / 8;
4708
4709 len -= end_bits; /* Now len % 8 == 0 */
4710
4711 for( ; end_bits != 0; end_bits-- )
4712 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4713 }
4714
4715 memset( mask + offset / 8, 0xFF, len / 8 );
4716}
4717
4718/*
4719 * Check that bitmask is full
4720 */
4721static int ssl_bitmask_check( unsigned char *mask, size_t len )
4722{
4723 size_t i;
4724
4725 for( i = 0; i < len / 8; i++ )
4726 if( mask[i] != 0xFF )
4727 return( -1 );
4728
4729 for( i = 0; i < len % 8; i++ )
4730 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4731 return( -1 );
4732
4733 return( 0 );
4734}
4735
Hanno Becker56e205e2018-08-16 09:06:12 +01004736/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004737static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004738 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004739{
Hanno Becker56e205e2018-08-16 09:06:12 +01004740 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004741
Hanno Becker56e205e2018-08-16 09:06:12 +01004742 alloc_len = 12; /* Handshake header */
4743 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004744
Hanno Beckerd07df862018-08-16 09:14:58 +01004745 if( add_bitmap )
4746 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004747
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004748 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004749}
Hanno Becker56e205e2018-08-16 09:06:12 +01004750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004752
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004753static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004754{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004755 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004756}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004757
Simon Butcher99000142016-10-13 17:21:01 +01004758int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004763 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004764 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004765 }
4766
Hanno Becker12555c62018-08-16 12:47:53 +01004767 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004770 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004771 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004774 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004775 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004776 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004777 unsigned int recv_msg_seq = (unsigned int)
4778 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004779
Hanno Becker44650b72018-08-16 12:51:11 +01004780 if( ssl_check_hs_header( ssl ) != 0 )
4781 {
4782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4783 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4784 }
4785
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004786 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004787 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4788 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4789 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4790 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004791 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004792 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4793 {
4794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4795 recv_msg_seq,
4796 ssl->handshake->in_msg_seq ) );
4797 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4798 }
4799
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004800 /* Retransmit only on last message from previous flight, to avoid
4801 * too many retransmissions.
4802 * Besides, No sane server ever retransmits HelloVerifyRequest */
4803 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004807 "message_seq = %d, start_of_flight = %d",
4808 recv_msg_seq,
4809 ssl->handshake->in_flight_start_seq ) );
4810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004814 return( ret );
4815 }
4816 }
4817 else
4818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004820 "message_seq = %d, expected = %d",
4821 recv_msg_seq,
4822 ssl->handshake->in_msg_seq ) );
4823 }
4824
Hanno Becker90333da2017-10-10 11:27:13 +01004825 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004826 }
4827 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004828
Hanno Becker6d97ef52018-08-16 13:09:04 +01004829 /* Message reassembly is handled alongside buffering of future
4830 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004831 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004832 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004833 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004836 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004837 }
4838 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004839 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004841#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004842 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004843 /* With TLS we don't handle fragmentation (for now) */
4844 if( ssl->in_msglen < ssl->in_hslen )
4845 {
4846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4847 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4848 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004849 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004850#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004851
Simon Butcher99000142016-10-13 17:21:01 +01004852 return( 0 );
4853}
4854
4855void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4856{
Hanno Becker0271f962018-08-16 13:23:47 +01004857 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004858
Hanno Becker0271f962018-08-16 13:23:47 +01004859 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004860 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004861
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004862 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004864 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004865 ssl->handshake != NULL )
4866 {
Hanno Becker0271f962018-08-16 13:23:47 +01004867 unsigned offset;
4868 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004869
Hanno Becker0271f962018-08-16 13:23:47 +01004870 /* Increment handshake sequence number */
4871 hs->in_msg_seq++;
4872
4873 /*
4874 * Clear up handshake buffering and reassembly structure.
4875 */
4876
4877 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004878 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004879
4880 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004881 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4882 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004883 offset++, hs_buf++ )
4884 {
4885 *hs_buf = *(hs_buf + 1);
4886 }
4887
4888 /* Create a fresh last entry */
4889 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004890 }
4891#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004892}
4893
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004894/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004895 * DTLS anti-replay: RFC 6347 4.1.2.6
4896 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004897 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4898 * Bit n is set iff record number in_window_top - n has been seen.
4899 *
4900 * Usually, in_window_top is the last record number seen and the lsb of
4901 * in_window is set. The only exception is the initial state (record number 0
4902 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4905static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004906{
4907 ssl->in_window_top = 0;
4908 ssl->in_window = 0;
4909}
4910
4911static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4912{
4913 return( ( (uint64_t) buf[0] << 40 ) |
4914 ( (uint64_t) buf[1] << 32 ) |
4915 ( (uint64_t) buf[2] << 24 ) |
4916 ( (uint64_t) buf[3] << 16 ) |
4917 ( (uint64_t) buf[4] << 8 ) |
4918 ( (uint64_t) buf[5] ) );
4919}
4920
4921/*
4922 * Return 0 if sequence number is acceptable, -1 otherwise
4923 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004924int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004925{
4926 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4927 uint64_t bit;
4928
Hanno Becker7f376f42019-06-12 16:20:48 +01004929 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4930 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4931 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004932 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004933 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004934
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004935 if( rec_seqnum > ssl->in_window_top )
4936 return( 0 );
4937
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004938 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004939
4940 if( bit >= 64 )
4941 return( -1 );
4942
4943 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4944 return( -1 );
4945
4946 return( 0 );
4947}
4948
4949/*
4950 * Update replay window on new validated record
4951 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004953{
4954 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4955
Hanno Becker7f376f42019-06-12 16:20:48 +01004956 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4957 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4958 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004959 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004960 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004961
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004962 if( rec_seqnum > ssl->in_window_top )
4963 {
4964 /* Update window_top and the contents of the window */
4965 uint64_t shift = rec_seqnum - ssl->in_window_top;
4966
4967 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004968 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004969 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004970 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004971 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004972 ssl->in_window |= 1;
4973 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004974
4975 ssl->in_window_top = rec_seqnum;
4976 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004977 else
4978 {
4979 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004980 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004981
4982 if( bit < 64 ) /* Always true, but be extra sure */
4983 ssl->in_window |= (uint64_t) 1 << bit;
4984 }
4985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004987
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004988#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004989/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004990static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4991
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004992/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004993 * Without any SSL context, check if a datagram looks like a ClientHello with
4994 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004995 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004996 *
4997 * - if cookie is valid, return 0
4998 * - if ClientHello looks superficially valid but cookie is not,
4999 * fill obuf and set olen, then
5000 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5001 * - otherwise return a specific error code
5002 */
5003static int ssl_check_dtls_clihlo_cookie(
5004 mbedtls_ssl_cookie_write_t *f_cookie_write,
5005 mbedtls_ssl_cookie_check_t *f_cookie_check,
5006 void *p_cookie,
5007 const unsigned char *cli_id, size_t cli_id_len,
5008 const unsigned char *in, size_t in_len,
5009 unsigned char *obuf, size_t buf_len, size_t *olen )
5010{
5011 size_t sid_len, cookie_len;
5012 unsigned char *p;
5013
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005014 /*
5015 * Structure of ClientHello with record and handshake headers,
5016 * and expected values. We don't need to check a lot, more checks will be
5017 * done when actually parsing the ClientHello - skipping those checks
5018 * avoids code duplication and does not make cookie forging any easier.
5019 *
5020 * 0-0 ContentType type; copied, must be handshake
5021 * 1-2 ProtocolVersion version; copied
5022 * 3-4 uint16 epoch; copied, must be 0
5023 * 5-10 uint48 sequence_number; copied
5024 * 11-12 uint16 length; (ignored)
5025 *
5026 * 13-13 HandshakeType msg_type; (ignored)
5027 * 14-16 uint24 length; (ignored)
5028 * 17-18 uint16 message_seq; copied
5029 * 19-21 uint24 fragment_offset; copied, must be 0
5030 * 22-24 uint24 fragment_length; (ignored)
5031 *
5032 * 25-26 ProtocolVersion client_version; (ignored)
5033 * 27-58 Random random; (ignored)
5034 * 59-xx SessionID session_id; 1 byte len + sid_len content
5035 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5036 * ...
5037 *
5038 * Minimum length is 61 bytes.
5039 */
5040 if( in_len < 61 ||
5041 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5042 in[3] != 0 || in[4] != 0 ||
5043 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5044 {
5045 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5046 }
5047
5048 sid_len = in[59];
5049 if( sid_len > in_len - 61 )
5050 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5051
5052 cookie_len = in[60 + sid_len];
5053 if( cookie_len > in_len - 60 )
5054 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5055
5056 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5057 cli_id, cli_id_len ) == 0 )
5058 {
5059 /* Valid cookie */
5060 return( 0 );
5061 }
5062
5063 /*
5064 * If we get here, we've got an invalid cookie, let's prepare HVR.
5065 *
5066 * 0-0 ContentType type; copied
5067 * 1-2 ProtocolVersion version; copied
5068 * 3-4 uint16 epoch; copied
5069 * 5-10 uint48 sequence_number; copied
5070 * 11-12 uint16 length; olen - 13
5071 *
5072 * 13-13 HandshakeType msg_type; hello_verify_request
5073 * 14-16 uint24 length; olen - 25
5074 * 17-18 uint16 message_seq; copied
5075 * 19-21 uint24 fragment_offset; copied
5076 * 22-24 uint24 fragment_length; olen - 25
5077 *
5078 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5079 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5080 *
5081 * Minimum length is 28.
5082 */
5083 if( buf_len < 28 )
5084 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5085
5086 /* Copy most fields and adapt others */
5087 memcpy( obuf, in, 25 );
5088 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5089 obuf[25] = 0xfe;
5090 obuf[26] = 0xff;
5091
5092 /* Generate and write actual cookie */
5093 p = obuf + 28;
5094 if( f_cookie_write( p_cookie,
5095 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5096 {
5097 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5098 }
5099
5100 *olen = p - obuf;
5101
5102 /* Go back and fill length fields */
5103 obuf[27] = (unsigned char)( *olen - 28 );
5104
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005105 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005106 obuf[22] = obuf[14];
5107 obuf[23] = obuf[15];
5108 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005109
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005110 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005111
5112 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5113}
5114
5115/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005116 * Handle possible client reconnect with the same UDP quadruplet
5117 * (RFC 6347 Section 4.2.8).
5118 *
5119 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5120 * that looks like a ClientHello.
5121 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005122 * - if the input looks like a ClientHello without cookies,
5123 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005124 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005125 * - if the input looks like a ClientHello with a valid cookie,
5126 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005127 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005128 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005129 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005130 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005131 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5132 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005133 */
5134static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5135{
5136 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005137 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005138
Hanno Becker87b56262019-07-10 14:37:41 +01005139 if( ssl->conf->f_cookie_write == NULL ||
5140 ssl->conf->f_cookie_check == NULL )
5141 {
5142 /* If we can't use cookies to verify reachability of the peer,
5143 * drop the record. */
5144 return( 0 );
5145 }
5146
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005147 ret = ssl_check_dtls_clihlo_cookie(
5148 ssl->conf->f_cookie_write,
5149 ssl->conf->f_cookie_check,
5150 ssl->conf->p_cookie,
5151 ssl->cli_id, ssl->cli_id_len,
5152 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005153 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005154
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005155 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5156
5157 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005158 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005159 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005160 * If the error is permanent we'll catch it later,
5161 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005162 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005163 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005164 }
5165
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005166 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005167 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005168 /* Got a valid cookie, partially reset context */
5169 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5170 {
5171 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5172 return( ret );
5173 }
5174
5175 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005176 }
5177
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005178 return( ret );
5179}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005180#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005181
Hanno Becker46483f12019-05-03 13:25:54 +01005182static int ssl_check_record_type( uint8_t record_type )
5183{
5184 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5185 record_type != MBEDTLS_SSL_MSG_ALERT &&
5186 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5187 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5188 {
5189 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5190 }
5191
5192 return( 0 );
5193}
5194
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005195/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005196 * ContentType type;
5197 * ProtocolVersion version;
5198 * uint16 epoch; // DTLS only
5199 * uint48 sequence_number; // DTLS only
5200 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005201 *
5202 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005203 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005204 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5205 *
5206 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005207 * 1. proceed with the record if this function returns 0
5208 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5209 * 3. return CLIENT_RECONNECT if this function return that value
5210 * 4. drop the whole datagram if this function returns anything else.
5211 * Point 2 is needed when the peer is resending, and we have already received
5212 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005213 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005214static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005215 unsigned char *buf,
5216 size_t len,
5217 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005218{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005219 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005220
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005221 size_t const rec_hdr_type_offset = 0;
5222 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005223
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005224 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5225 rec_hdr_type_len;
5226 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005227
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005228 size_t const rec_hdr_ctr_len = 8;
5229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005230 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005231 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5232 rec_hdr_version_len;
5233
Hanno Beckera5a2b082019-05-15 14:03:01 +01005234#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005235 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5236 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005237 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005238#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5239#endif /* MBEDTLS_SSL_PROTO_DTLS */
5240
5241 size_t rec_hdr_len_offset; /* To be determined */
5242 size_t const rec_hdr_len_len = 2;
5243
5244 /*
5245 * Check minimum lengths for record header.
5246 */
5247
5248#if defined(MBEDTLS_SSL_PROTO_DTLS)
5249 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5250 {
5251 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5252 }
5253 MBEDTLS_SSL_TRANSPORT_ELSE
5254#endif /* MBEDTLS_SSL_PROTO_DTLS */
5255#if defined(MBEDTLS_SSL_PROTO_TLS)
5256 {
5257 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5258 }
5259#endif /* MBEDTLS_SSL_PROTO_DTLS */
5260
5261 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5262 {
5263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5264 (unsigned) len,
5265 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5266 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5267 }
5268
5269 /*
5270 * Parse and validate record content type
5271 */
5272
5273 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005274
5275 /* Check record content type */
5276#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5277 rec->cid_len = 0;
5278
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005279 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005280 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5281 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005282 {
5283 /* Shift pointers to account for record header including CID
5284 * struct {
5285 * ContentType special_type = tls12_cid;
5286 * ProtocolVersion version;
5287 * uint16 epoch;
5288 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005289 * opaque cid[cid_length]; // Additional field compared to
5290 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005291 * uint16 length;
5292 * opaque enc_content[DTLSCiphertext.length];
5293 * } DTLSCiphertext;
5294 */
5295
5296 /* So far, we only support static CID lengths
5297 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005298 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5299 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005300
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005301 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005302 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5304 (unsigned) len,
5305 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005306 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005307 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005308
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005309 /* configured CID len is guaranteed at most 255, see
5310 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5311 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005312 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005313 }
5314 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005316 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005317 if( ssl_check_record_type( rec->type ) )
5318 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5320 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005321 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5322 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005323 }
5324
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005325 /*
5326 * Parse and validate record version
5327 */
5328
Hanno Becker8061c6e2019-07-26 08:07:03 +01005329 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5330 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005331 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5332 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005333 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005334
Hanno Becker2881d802019-05-22 14:44:53 +01005335 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5338 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005339 }
5340
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005341 if( mbedtls_ssl_ver_gt( minor_ver,
5342 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5345 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005346 }
5347
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005348 /*
5349 * Parse/Copy record sequence number.
5350 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005351
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005352#if defined(MBEDTLS_SSL_PROTO_DTLS)
5353 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5354 {
5355 /* Copy explicit record sequence number from input buffer. */
5356 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5357 rec_hdr_ctr_len );
5358 }
5359 MBEDTLS_SSL_TRANSPORT_ELSE
5360#endif /* MBEDTLS_SSL_PROTO_DTLS */
5361#if defined(MBEDTLS_SSL_PROTO_TLS)
5362 {
5363 /* Copy implicit record sequence number from SSL context structure. */
5364 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5365 }
5366#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005367
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005368 /*
5369 * Parse record length.
5370 */
5371
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005372 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005373 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005374 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5375
Hanno Becker8b09b732019-05-08 12:03:28 +01005376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005377 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005378 rec->type,
5379 major_ver, minor_ver, rec->data_len ) );
5380
5381 rec->buf = buf;
5382 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005383
Hanno Beckerec014082019-07-26 08:20:27 +01005384 if( rec->data_len == 0 )
5385 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5386
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005387 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005388 * DTLS-related tests.
5389 * Check epoch before checking length constraint because
5390 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5391 * message gets duplicated before the corresponding Finished message,
5392 * the second ChangeCipherSpec should be discarded because it belongs
5393 * to an old epoch, but not because its length is shorter than
5394 * the minimum record length for packets using the new record transform.
5395 * Note that these two kinds of failures are handled differently,
5396 * as an unexpected record is silently skipped but an invalid
5397 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005398 */
5399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005400 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005401 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005402 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005403
Hanno Beckere0452772019-07-10 17:12:07 +01005404 /* Check that the datagram is large enough to contain a record
5405 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005406 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005407 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5409 (unsigned) len,
5410 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005411 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5412 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005413
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005414 /* Records from other, non-matching epochs are silently discarded.
5415 * (The case of same-port Client reconnects must be considered in
5416 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005417 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005418 {
5419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5420 "expected %d, received %d",
5421 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005422
5423 /* Records from the next epoch are considered for buffering
5424 * (concretely: early Finished messages). */
5425 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5426 {
5427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5428 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5429 }
5430
Hanno Becker87b56262019-07-10 14:37:41 +01005431 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005432 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005433#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005434 /* For records from the correct epoch, check whether their
5435 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005436 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005437 {
5438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5439 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5440 }
5441#endif
5442 }
5443#endif /* MBEDTLS_SSL_PROTO_DTLS */
5444
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445 return( 0 );
5446}
Paul Bakker5121ce52009-01-03 21:22:43 +00005447
Hanno Becker87b56262019-07-10 14:37:41 +01005448
5449#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5450static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5451{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005452 unsigned int rec_epoch = (unsigned int)
5453 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005454
5455 /*
5456 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5457 * access the first byte of record content (handshake type), as we
5458 * have an active transform (possibly iv_len != 0), so use the
5459 * fact that the record header len is 13 instead.
5460 */
5461 if( rec_epoch == 0 &&
5462 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5463 MBEDTLS_SSL_IS_SERVER &&
5464 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5465 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5466 ssl->in_left > 13 &&
5467 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5468 {
5469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5470 "from the same port" ) );
5471 return( ssl_handle_possible_reconnect( ssl ) );
5472 }
5473
5474 return( 0 );
5475}
5476#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5477
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005478/*
5479 * If applicable, decrypt (and decompress) record content
5480 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005481static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5482 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005483{
5484 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005487 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5490 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 ret = mbedtls_ssl_hw_record_read( ssl );
5495 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5498 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005499 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005500
5501 if( ret == 0 )
5502 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005505 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005507 unsigned char const old_msg_type = rec->type;
5508
Hanno Becker611a83b2018-01-03 14:27:32 +00005509 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005510 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005513
Hanno Beckera5a2b082019-05-15 14:03:01 +01005514#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005515 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005516 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5517 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005518 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005520 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005521 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005522#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005523
Paul Bakker5121ce52009-01-03 21:22:43 +00005524 return( ret );
5525 }
5526
Hanno Becker106f3da2019-07-12 09:35:58 +01005527 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005528 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005529 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005530 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005532
Paul Bakker5121ce52009-01-03 21:22:43 +00005533 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005534 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005535
Hanno Beckera5a2b082019-05-15 14:03:01 +01005536#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005537 /* We have already checked the record content type
5538 * in ssl_parse_record_header(), failing or silently
5539 * dropping the record in the case of an unknown type.
5540 *
5541 * Since with the use of CIDs, the record content type
5542 * might change during decryption, re-check the record
5543 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005544 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5547 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5548 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005549#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005550
Hanno Becker106f3da2019-07-12 09:35:58 +01005551 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005552 {
5553#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005554 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005555 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005556 {
5557 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5559 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5560 }
5561#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5562
5563 ssl->nb_zero++;
5564
5565 /*
5566 * Three or more empty messages may be a DoS attack
5567 * (excessive CPU consumption).
5568 */
5569 if( ssl->nb_zero > 3 )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005572 "messages, possible DoS attack" ) );
5573 /* Treat the records as if they were not properly authenticated,
5574 * thereby failing the connection if we see more than allowed
5575 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005576 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5577 }
5578 }
5579 else
5580 ssl->nb_zero = 0;
5581
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005582 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5583#if defined(MBEDTLS_SSL_PROTO_TLS)
5584 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005585 {
5586 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005587 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005588 if( ++ssl->in_ctr[i - 1] != 0 )
5589 break;
5590
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005591 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005592 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005593 {
5594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5595 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5596 }
5597 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005598#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005599
Paul Bakker5121ce52009-01-03 21:22:43 +00005600 }
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005603 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005604 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005605 {
5606 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005609 return( ret );
5610 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005611 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005614#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005615 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005617 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005618 }
5619#endif
5620
Hanno Beckerf0242852019-07-09 17:30:02 +01005621 /* Check actual (decrypted) record content length against
5622 * configured maximum. */
5623 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5624 {
5625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5626 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5627 }
5628
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005629 return( 0 );
5630}
5631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005633
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005634/*
5635 * Read a record.
5636 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005637 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5638 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5639 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005640 */
Hanno Becker1097b342018-08-15 14:09:41 +01005641
5642/* Helper functions for mbedtls_ssl_read_record(). */
5643static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005644static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5645static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005646
Hanno Becker327c93b2018-08-15 13:56:18 +01005647int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005648 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005649{
5650 int ret;
5651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005653
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005654 if( ssl->keep_current_message == 0 )
5655 {
5656 do {
Simon Butcher99000142016-10-13 17:21:01 +01005657
Hanno Becker26994592018-08-15 14:14:59 +01005658 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005659 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005660 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005661
Hanno Beckere74d5562018-08-15 14:26:08 +01005662 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005663 {
Hanno Becker40f50842018-08-15 14:48:01 +01005664#if defined(MBEDTLS_SSL_PROTO_DTLS)
5665 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005666
Hanno Becker40f50842018-08-15 14:48:01 +01005667 /* We only check for buffered messages if the
5668 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005669 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005670 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005671 {
Hanno Becker40f50842018-08-15 14:48:01 +01005672 if( ssl_load_buffered_message( ssl ) == 0 )
5673 have_buffered = 1;
5674 }
5675
5676 if( have_buffered == 0 )
5677#endif /* MBEDTLS_SSL_PROTO_DTLS */
5678 {
5679 ret = ssl_get_next_record( ssl );
5680 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5681 continue;
5682
5683 if( ret != 0 )
5684 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005685 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005686 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005687 return( ret );
5688 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005689 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005690 }
5691
5692 ret = mbedtls_ssl_handle_message_type( ssl );
5693
Hanno Becker40f50842018-08-15 14:48:01 +01005694#if defined(MBEDTLS_SSL_PROTO_DTLS)
5695 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5696 {
5697 /* Buffer future message */
5698 ret = ssl_buffer_message( ssl );
5699 if( ret != 0 )
5700 return( ret );
5701
5702 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5703 }
5704#endif /* MBEDTLS_SSL_PROTO_DTLS */
5705
Hanno Becker90333da2017-10-10 11:27:13 +01005706 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5707 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005708
5709 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005710 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005711 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005712 return( ret );
5713 }
5714
Hanno Becker327c93b2018-08-15 13:56:18 +01005715 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005716 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005717 {
5718 mbedtls_ssl_update_handshake_status( ssl );
5719 }
Simon Butcher99000142016-10-13 17:21:01 +01005720 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005721 else
Simon Butcher99000142016-10-13 17:21:01 +01005722 {
Hanno Becker02f59072018-08-15 14:00:24 +01005723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005724 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005725 }
5726
5727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5728
5729 return( 0 );
5730}
5731
Hanno Becker40f50842018-08-15 14:48:01 +01005732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005733static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005734{
Hanno Becker40f50842018-08-15 14:48:01 +01005735 if( ssl->in_left > ssl->next_record_offset )
5736 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005737
Hanno Becker40f50842018-08-15 14:48:01 +01005738 return( 0 );
5739}
5740
5741static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5742{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005743 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005744 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005745 int ret = 0;
5746
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005747 if( hs == NULL )
5748 return( -1 );
5749
Hanno Beckere00ae372018-08-20 09:39:42 +01005750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5751
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005752 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5753 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5754 {
5755 /* Check if we have seen a ChangeCipherSpec before.
5756 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005757 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005758 {
5759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5760 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005761 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005762 }
5763
Hanno Becker39b8bc92018-08-28 17:17:13 +01005764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005765 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5766 ssl->in_msglen = 1;
5767 ssl->in_msg[0] = 1;
5768
5769 /* As long as they are equal, the exact value doesn't matter. */
5770 ssl->in_left = 0;
5771 ssl->next_record_offset = 0;
5772
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005773 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005774 goto exit;
5775 }
Hanno Becker37f95322018-08-16 13:55:32 +01005776
Hanno Beckerb8f50142018-08-28 10:01:34 +01005777#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005778 /* Debug only */
5779 {
5780 unsigned offset;
5781 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5782 {
5783 hs_buf = &hs->buffering.hs[offset];
5784 if( hs_buf->is_valid == 1 )
5785 {
5786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5787 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005788 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005789 }
5790 }
5791 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005792#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005793
5794 /* Check if we have buffered and/or fully reassembled the
5795 * next handshake message. */
5796 hs_buf = &hs->buffering.hs[0];
5797 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5798 {
5799 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005800 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005801
5802 /* Double-check that we haven't accidentally buffered
5803 * a message that doesn't fit into the input buffer. */
5804 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5805 {
5806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5808 }
5809
5810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5811 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5812 hs_buf->data, msg_len + 12 );
5813
5814 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5815 ssl->in_hslen = msg_len + 12;
5816 ssl->in_msglen = msg_len + 12;
5817 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5818
5819 ret = 0;
5820 goto exit;
5821 }
5822 else
5823 {
5824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5825 hs->in_msg_seq ) );
5826 }
5827
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005828 ret = -1;
5829
5830exit:
5831
5832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5833 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005834}
5835
Hanno Beckera02b0b42018-08-21 17:20:27 +01005836static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5837 size_t desired )
5838{
5839 int offset;
5840 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5842 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005843
Hanno Becker01315ea2018-08-21 17:22:17 +01005844 /* Get rid of future records epoch first, if such exist. */
5845 ssl_free_buffered_record( ssl );
5846
5847 /* Check if we have enough space available now. */
5848 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5849 hs->buffering.total_bytes_buffered ) )
5850 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005852 return( 0 );
5853 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005854
Hanno Becker4f432ad2018-08-28 10:02:32 +01005855 /* We don't have enough space to buffer the next expected handshake
5856 * message. Remove buffers used for future messages to gain space,
5857 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005858 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5859 offset >= 0; offset-- )
5860 {
5861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5862 offset ) );
5863
Hanno Beckerb309b922018-08-23 13:18:05 +01005864 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005865
5866 /* Check if we have enough space available now. */
5867 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5868 hs->buffering.total_bytes_buffered ) )
5869 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005871 return( 0 );
5872 }
5873 }
5874
5875 return( -1 );
5876}
5877
Hanno Becker40f50842018-08-15 14:48:01 +01005878static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5879{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005880 int ret = 0;
5881 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5882
5883 if( hs == NULL )
5884 return( 0 );
5885
5886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5887
5888 switch( ssl->in_msgtype )
5889 {
5890 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005892
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005893 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005894 break;
5895
5896 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005897 {
5898 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005899 unsigned recv_msg_seq = (unsigned)
5900 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005901
Hanno Becker37f95322018-08-16 13:55:32 +01005902 mbedtls_ssl_hs_buffer *hs_buf;
5903 size_t msg_len = ssl->in_hslen - 12;
5904
5905 /* We should never receive an old handshake
5906 * message - double-check nonetheless. */
5907 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5908 {
5909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5911 }
5912
5913 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5914 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5915 {
5916 /* Silently ignore -- message too far in the future */
5917 MBEDTLS_SSL_DEBUG_MSG( 2,
5918 ( "Ignore future HS message with sequence number %u, "
5919 "buffering window %u - %u",
5920 recv_msg_seq, ssl->handshake->in_msg_seq,
5921 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5922
5923 goto exit;
5924 }
5925
5926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5927 recv_msg_seq, recv_msg_seq_offset ) );
5928
5929 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5930
5931 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005932 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005933 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005934 size_t reassembly_buf_sz;
5935
Hanno Becker37f95322018-08-16 13:55:32 +01005936 hs_buf->is_fragmented =
5937 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5938
5939 /* We copy the message back into the input buffer
5940 * after reassembly, so check that it's not too large.
5941 * This is an implementation-specific limitation
5942 * and not one from the standard, hence it is not
5943 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005944 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005945 {
5946 /* Ignore message */
5947 goto exit;
5948 }
5949
Hanno Beckere0b150f2018-08-21 15:51:03 +01005950 /* Check if we have enough space to buffer the message. */
5951 if( hs->buffering.total_bytes_buffered >
5952 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5953 {
5954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5956 }
5957
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005958 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5959 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005960
5961 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5962 hs->buffering.total_bytes_buffered ) )
5963 {
5964 if( recv_msg_seq_offset > 0 )
5965 {
5966 /* If we can't buffer a future message because
5967 * of space limitations -- ignore. */
5968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5969 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5970 (unsigned) hs->buffering.total_bytes_buffered ) );
5971 goto exit;
5972 }
Hanno Beckere1801392018-08-21 16:51:05 +01005973 else
5974 {
5975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5976 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5977 (unsigned) hs->buffering.total_bytes_buffered ) );
5978 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005979
Hanno Beckera02b0b42018-08-21 17:20:27 +01005980 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005981 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5983 (unsigned) msg_len,
5984 (unsigned) reassembly_buf_sz,
5985 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005986 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005987 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5988 goto exit;
5989 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005990 }
5991
5992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5993 msg_len ) );
5994
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005995 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5996 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005997 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005998 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005999 goto exit;
6000 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006001 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006002
6003 /* Prepare final header: copy msg_type, length and message_seq,
6004 * then add standardised fragment_offset and fragment_length */
6005 memcpy( hs_buf->data, ssl->in_msg, 6 );
6006 memset( hs_buf->data + 6, 0, 3 );
6007 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
6008
6009 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006010
6011 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006012 }
6013 else
6014 {
6015 /* Make sure msg_type and length are consistent */
6016 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
6017 {
6018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6019 /* Ignore */
6020 goto exit;
6021 }
6022 }
6023
Hanno Becker4422bbb2018-08-20 09:40:19 +01006024 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006025 {
6026 size_t frag_len, frag_off;
6027 unsigned char * const msg = hs_buf->data + 12;
6028
6029 /*
6030 * Check and copy current fragment
6031 */
6032
6033 /* Validation of header fields already done in
6034 * mbedtls_ssl_prepare_handshake_record(). */
6035 frag_off = ssl_get_hs_frag_off( ssl );
6036 frag_len = ssl_get_hs_frag_len( ssl );
6037
6038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6039 frag_off, frag_len ) );
6040 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6041
6042 if( hs_buf->is_fragmented )
6043 {
6044 unsigned char * const bitmask = msg + msg_len;
6045 ssl_bitmask_set( bitmask, frag_off, frag_len );
6046 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6047 msg_len ) == 0 );
6048 }
6049 else
6050 {
6051 hs_buf->is_complete = 1;
6052 }
6053
6054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6055 hs_buf->is_complete ? "" : "not yet " ) );
6056 }
6057
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006058 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006059 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006060
6061 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006062 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006063 break;
6064 }
6065
6066exit:
6067
6068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6069 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006070}
6071#endif /* MBEDTLS_SSL_PROTO_DTLS */
6072
Hanno Becker1097b342018-08-15 14:09:41 +01006073static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006074{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006075 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006076 * Consume last content-layer message and potentially
6077 * update in_msglen which keeps track of the contents'
6078 * consumption state.
6079 *
6080 * (1) Handshake messages:
6081 * Remove last handshake message, move content
6082 * and adapt in_msglen.
6083 *
6084 * (2) Alert messages:
6085 * Consume whole record content, in_msglen = 0.
6086 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006087 * (3) Change cipher spec:
6088 * Consume whole record content, in_msglen = 0.
6089 *
6090 * (4) Application data:
6091 * Don't do anything - the record layer provides
6092 * the application data as a stream transport
6093 * and consumes through mbedtls_ssl_read only.
6094 *
6095 */
6096
6097 /* Case (1): Handshake messages */
6098 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006099 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006100 /* Hard assertion to be sure that no application data
6101 * is in flight, as corrupting ssl->in_msglen during
6102 * ssl->in_offt != NULL is fatal. */
6103 if( ssl->in_offt != NULL )
6104 {
6105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6106 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6107 }
6108
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006109 /*
6110 * Get next Handshake message in the current record
6111 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006112
Hanno Becker4a810fb2017-05-24 16:27:30 +01006113 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006114 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006115 * current handshake content: If DTLS handshake
6116 * fragmentation is used, that's the fragment
6117 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006118 * size here is faulty and should be changed at
6119 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006120 * (2) While it doesn't seem to cause problems, one
6121 * has to be very careful not to assume that in_hslen
6122 * is always <= in_msglen in a sensible communication.
6123 * Again, it's wrong for DTLS handshake fragmentation.
6124 * The following check is therefore mandatory, and
6125 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006126 * Additionally, ssl->in_hslen might be arbitrarily out of
6127 * bounds after handling a DTLS message with an unexpected
6128 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006129 */
6130 if( ssl->in_hslen < ssl->in_msglen )
6131 {
6132 ssl->in_msglen -= ssl->in_hslen;
6133 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6134 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006135
Hanno Becker4a810fb2017-05-24 16:27:30 +01006136 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6137 ssl->in_msg, ssl->in_msglen );
6138 }
6139 else
6140 {
6141 ssl->in_msglen = 0;
6142 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006143
Hanno Becker4a810fb2017-05-24 16:27:30 +01006144 ssl->in_hslen = 0;
6145 }
6146 /* Case (4): Application data */
6147 else if( ssl->in_offt != NULL )
6148 {
6149 return( 0 );
6150 }
6151 /* Everything else (CCS & Alerts) */
6152 else
6153 {
6154 ssl->in_msglen = 0;
6155 }
6156
Hanno Becker1097b342018-08-15 14:09:41 +01006157 return( 0 );
6158}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006159
Hanno Beckere74d5562018-08-15 14:26:08 +01006160static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6161{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006162 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006163 return( 1 );
6164
6165 return( 0 );
6166}
6167
Hanno Becker5f066e72018-08-16 14:56:31 +01006168#if defined(MBEDTLS_SSL_PROTO_DTLS)
6169
6170static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6171{
6172 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6173 if( hs == NULL )
6174 return;
6175
Hanno Becker01315ea2018-08-21 17:22:17 +01006176 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006177 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006178 hs->buffering.total_bytes_buffered -=
6179 hs->buffering.future_record.len;
6180
6181 mbedtls_free( hs->buffering.future_record.data );
6182 hs->buffering.future_record.data = NULL;
6183 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006184}
6185
6186static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6187{
6188 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6189 unsigned char * rec;
6190 size_t rec_len;
6191 unsigned rec_epoch;
6192
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006193 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006194 return( 0 );
6195
6196 if( hs == NULL )
6197 return( 0 );
6198
Hanno Becker5f066e72018-08-16 14:56:31 +01006199 rec = hs->buffering.future_record.data;
6200 rec_len = hs->buffering.future_record.len;
6201 rec_epoch = hs->buffering.future_record.epoch;
6202
6203 if( rec == NULL )
6204 return( 0 );
6205
Hanno Becker4cb782d2018-08-20 11:19:05 +01006206 /* Only consider loading future records if the
6207 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006208 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006209 return( 0 );
6210
Hanno Becker5f066e72018-08-16 14:56:31 +01006211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6212
6213 if( rec_epoch != ssl->in_epoch )
6214 {
6215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6216 goto exit;
6217 }
6218
6219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6220
6221 /* Double-check that the record is not too large */
6222 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6223 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6224 {
6225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6227 }
6228
6229 memcpy( ssl->in_hdr, rec, rec_len );
6230 ssl->in_left = rec_len;
6231 ssl->next_record_offset = 0;
6232
6233 ssl_free_buffered_record( ssl );
6234
6235exit:
6236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6237 return( 0 );
6238}
6239
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006240static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6241 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006242{
6243 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006244
6245 /* Don't buffer future records outside handshakes. */
6246 if( hs == NULL )
6247 return( 0 );
6248
6249 /* Only buffer handshake records (we are only interested
6250 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006251 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006252 return( 0 );
6253
6254 /* Don't buffer more than one future epoch record. */
6255 if( hs->buffering.future_record.data != NULL )
6256 return( 0 );
6257
Hanno Becker01315ea2018-08-21 17:22:17 +01006258 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006259 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006260 hs->buffering.total_bytes_buffered ) )
6261 {
6262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006263 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006264 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006265 return( 0 );
6266 }
6267
Hanno Becker5f066e72018-08-16 14:56:31 +01006268 /* Buffer record */
6269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6270 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006271 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006272
6273 /* ssl_parse_record_header() only considers records
6274 * of the next epoch as candidates for buffering. */
6275 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006276 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006277
6278 hs->buffering.future_record.data =
6279 mbedtls_calloc( 1, hs->buffering.future_record.len );
6280 if( hs->buffering.future_record.data == NULL )
6281 {
6282 /* If we run out of RAM trying to buffer a
6283 * record from the next epoch, just ignore. */
6284 return( 0 );
6285 }
6286
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006287 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006288
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006289 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006290 return( 0 );
6291}
6292
6293#endif /* MBEDTLS_SSL_PROTO_DTLS */
6294
Hanno Beckere74d5562018-08-15 14:26:08 +01006295static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006296{
6297 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006298 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006299
Hanno Becker5f066e72018-08-16 14:56:31 +01006300#if defined(MBEDTLS_SSL_PROTO_DTLS)
6301 /* We might have buffered a future record; if so,
6302 * and if the epoch matches now, load it.
6303 * On success, this call will set ssl->in_left to
6304 * the length of the buffered record, so that
6305 * the calls to ssl_fetch_input() below will
6306 * essentially be no-ops. */
6307 ret = ssl_load_buffered_record( ssl );
6308 if( ret != 0 )
6309 return( ret );
6310#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006311
Hanno Becker8b09b732019-05-08 12:03:28 +01006312 /* Ensure that we have enough space available for the default form
6313 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6314 * with no space for CIDs counted in). */
6315 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6316 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006319 return( ret );
6320 }
6321
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006322 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6323 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006326 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006327 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006328 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6329 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006330 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006331 if( ret != 0 )
6332 return( ret );
6333
6334 /* Fall through to handling of unexpected records */
6335 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6336 }
6337
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006338 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6339 {
Hanno Becker87b56262019-07-10 14:37:41 +01006340#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006341 /* Reset in pointers to default state for TLS/DTLS records,
6342 * assuming no CID and no offset between record content and
6343 * record plaintext. */
6344 ssl_update_in_pointers( ssl );
6345
Hanno Becker69412452019-07-12 08:33:49 +01006346 /* Setup internal message pointers from record structure. */
6347 ssl->in_msgtype = rec.type;
6348#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6349 ssl->in_len = ssl->in_cid + rec.cid_len;
6350#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006351 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006352 ssl->in_msglen = rec.data_len;
6353
Hanno Becker87b56262019-07-10 14:37:41 +01006354 ret = ssl_check_client_reconnect( ssl );
6355 if( ret != 0 )
6356 return( ret );
6357#endif
6358
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006359 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006360 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006361
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6363 "(header)" ) );
6364 }
6365 else
6366 {
6367 /* Skip invalid record and the rest of the datagram */
6368 ssl->next_record_offset = 0;
6369 ssl->in_left = 0;
6370
6371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6372 "(header)" ) );
6373 }
6374
6375 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006376 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006377 }
Hanno Becker87b56262019-07-10 14:37:41 +01006378 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006379#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006380 {
6381 return( ret );
6382 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006383 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006386 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006387 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006388 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006389 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006390 if( ssl->next_record_offset < ssl->in_left )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6393 }
6394 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006395 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006396#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006397#if defined(MBEDTLS_SSL_PROTO_TLS)
6398 {
Hanno Beckere0452772019-07-10 17:12:07 +01006399 /*
6400 * Fetch record contents from underlying transport.
6401 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006402 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006403 if( ret != 0 )
6404 {
6405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6406 return( ret );
6407 }
6408
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006409 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006410 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006411#endif /* MBEDTLS_SSL_PROTO_TLS */
6412
6413 /*
6414 * Decrypt record contents.
6415 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006416
Hanno Beckera89610a2019-07-11 13:07:45 +01006417 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006420 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006421 {
6422 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006423 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006424 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006425 /* Except when waiting for Finished as a bad mac here
6426 * probably means something went wrong in the handshake
6427 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6428 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6429 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6430 {
6431#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6432 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6433 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006434 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006435 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6436 }
6437#endif
6438 return( ret );
6439 }
6440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006441#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006442 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6443 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6446 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006447 }
6448#endif
6449
Hanno Becker4a810fb2017-05-24 16:27:30 +01006450 /* As above, invalid records cause
6451 * dismissal of the whole datagram. */
6452
6453 ssl->next_record_offset = 0;
6454 ssl->in_left = 0;
6455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006457 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006458 }
6459
6460 return( ret );
6461 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006462 MBEDTLS_SSL_TRANSPORT_ELSE
6463#endif /* MBEDTLS_SSL_PROTO_DTLS */
6464#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006465 {
6466 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6468 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006469 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006470 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006471 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006472 }
6473#endif
6474 return( ret );
6475 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006476#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006477 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006478
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006479
6480 /* Reset in pointers to default state for TLS/DTLS records,
6481 * assuming no CID and no offset between record content and
6482 * record plaintext. */
6483 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006484#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6485 ssl->in_len = ssl->in_cid + rec.cid_len;
6486#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006487 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006488
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006489 /* The record content type may change during decryption,
6490 * so re-read it. */
6491 ssl->in_msgtype = rec.type;
6492 /* Also update the input buffer, because unfortunately
6493 * the server-side ssl_parse_client_hello() reparses the
6494 * record header when receiving a ClientHello initiating
6495 * a renegotiation. */
6496 ssl->in_hdr[0] = rec.type;
6497 ssl->in_msg = rec.buf + rec.data_offset;
6498 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006499 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006500
Simon Butcher99000142016-10-13 17:21:01 +01006501 return( 0 );
6502}
6503
6504int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6505{
6506 int ret;
6507
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006508 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006509 * Handle particular types of records
6510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 {
Simon Butcher99000142016-10-13 17:21:01 +01006513 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6514 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006515 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006517 }
6518
Hanno Beckere678eaa2018-08-21 14:57:46 +01006519 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006520 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006521 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006522 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6524 ssl->in_msglen ) );
6525 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006526 }
6527
Hanno Beckere678eaa2018-08-21 14:57:46 +01006528 if( ssl->in_msg[0] != 1 )
6529 {
6530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6531 ssl->in_msg[0] ) );
6532 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6533 }
6534
6535#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006536 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006537 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6538 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6539 {
6540 if( ssl->handshake == NULL )
6541 {
6542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6543 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6544 }
6545
6546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6547 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6548 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006549#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006550 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006553 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006554 if( ssl->in_msglen != 2 )
6555 {
6556 /* Note: Standard allows for more than one 2 byte alert
6557 to be packed in a single message, but Mbed TLS doesn't
6558 currently support this. */
6559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6560 ssl->in_msglen ) );
6561 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6562 }
6563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006565 ssl->in_msg[0], ssl->in_msg[1] ) );
6566
6567 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006568 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006570 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006573 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006574 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 }
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6578 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6581 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006582 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006583
6584#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6585 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6586 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6587 {
Hanno Becker90333da2017-10-10 11:27:13 +01006588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006589 /* Will be handled when trying to parse ServerHello */
6590 return( 0 );
6591 }
6592#endif
6593
6594#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006595 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006596 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6597 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006598 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6599 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6600 {
6601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6602 /* Will be handled in mbedtls_ssl_parse_certificate() */
6603 return( 0 );
6604 }
6605#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6606
6607 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006608 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 }
6610
Hanno Beckerc76c6192017-06-06 10:03:17 +01006611#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006612 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006613 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006614 /* Drop unexpected ApplicationData records,
6615 * except at the beginning of renegotiations */
6616 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6617 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6618#if defined(MBEDTLS_SSL_RENEGOTIATION)
6619 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6620 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006621#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006622 )
6623 {
6624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6625 return( MBEDTLS_ERR_SSL_NON_FATAL );
6626 }
6627
6628 if( ssl->handshake != NULL &&
6629 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6630 {
6631 ssl_handshake_wrapup_free_hs_transform( ssl );
6632 }
6633 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006634#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006635
Paul Bakker5121ce52009-01-03 21:22:43 +00006636 return( 0 );
6637}
6638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006640{
6641 int ret;
6642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6644 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6645 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006646 {
6647 return( ret );
6648 }
6649
6650 return( 0 );
6651}
6652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006654 unsigned char level,
6655 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006656{
6657 int ret;
6658
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006659 if( ssl == NULL || ssl->conf == NULL )
6660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006663 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006666 ssl->out_msglen = 2;
6667 ssl->out_msg[0] = level;
6668 ssl->out_msg[1] = message;
6669
Hanno Becker67bc7c32018-08-06 11:33:50 +01006670 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006673 return( ret );
6674 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006676
6677 return( 0 );
6678}
6679
Hanno Becker17572472019-02-08 07:19:04 +00006680#if defined(MBEDTLS_X509_CRT_PARSE_C)
6681static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6682{
6683#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6684 if( session->peer_cert != NULL )
6685 {
6686 mbedtls_x509_crt_free( session->peer_cert );
6687 mbedtls_free( session->peer_cert );
6688 session->peer_cert = NULL;
6689 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006690#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006691 if( session->peer_cert_digest != NULL )
6692 {
6693 /* Zeroization is not necessary. */
6694 mbedtls_free( session->peer_cert_digest );
6695 session->peer_cert_digest = NULL;
6696 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6697 session->peer_cert_digest_len = 0;
6698 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006699#else
6700 ((void) session);
6701#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006702}
6703#endif /* MBEDTLS_X509_CRT_PARSE_C */
6704
Paul Bakker5121ce52009-01-03 21:22:43 +00006705/*
6706 * Handshake functions
6707 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006708#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006709/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006711{
Hanno Beckerdf645962019-06-26 13:02:22 +01006712 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6713 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006716
Hanno Becker5097cba2019-02-05 13:36:46 +00006717 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006720 ssl->state++;
6721 return( 0 );
6722 }
6723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006726}
6727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006729{
Hanno Beckerdf645962019-06-26 13:02:22 +01006730 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6731 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006734
Hanno Becker5097cba2019-02-05 13:36:46 +00006735 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738 ssl->state++;
6739 return( 0 );
6740 }
6741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006744}
Gilles Peskinef9828522017-05-03 12:28:43 +02006745
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006746#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006747/* Some certificate support -> implement write and parse */
6748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006749int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006752 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006754 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6755 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006758
Hanno Becker5097cba2019-02-05 13:36:46 +00006759 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006762 ssl->state++;
6763 return( 0 );
6764 }
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006767 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6768 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006769 {
6770 if( ssl->client_auth == 0 )
6771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006773 ssl->state++;
6774 return( 0 );
6775 }
6776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 /*
6779 * If using SSLv3 and got no cert, send an Alert message
6780 * (otherwise an empty Certificate message will be sent).
6781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006783 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 {
6785 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6787 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6788 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006791 goto write_msg;
6792 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#endif /* MBEDTLS_SSL_CLI_C */
6796#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006797 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6802 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 }
6804 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006805#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006808
6809 /*
6810 * 0 . 0 handshake type
6811 * 1 . 3 handshake length
6812 * 4 . 6 length of all certs
6813 * 7 . 9 length of cert. 1
6814 * 10 . n-1 peer certificate
6815 * n . n+2 length of cert. 2
6816 * n+3 . ... upper level cert, etc.
6817 */
6818 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Paul Bakker29087132010-03-21 21:03:34 +00006821 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 {
6823 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006824 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006827 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006829 }
6830
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006831 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
6833 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6834 i += n; crt = crt->next;
6835 }
6836
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006837 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006838
6839 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6841 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006843#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006844write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006846
6847 ssl->state++;
6848
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006849 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006850 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006852 return( ret );
6853 }
6854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Paul Bakkered27a042013-04-18 22:46:23 +02006857 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858}
6859
Hanno Becker285ff0c2019-01-31 07:44:03 +00006860#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006861
6862#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006863static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6864 unsigned char *crt_buf,
6865 size_t crt_buf_len )
6866{
6867 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6868
6869 if( peer_crt == NULL )
6870 return( -1 );
6871
6872 if( peer_crt->raw.len != crt_buf_len )
6873 return( -1 );
6874
Hanno Becker68b856d2019-02-08 14:00:04 +00006875 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006876}
Hanno Becker5882dd02019-06-06 16:25:57 +01006877#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006878static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6879 unsigned char *crt_buf,
6880 size_t crt_buf_len )
6881{
6882 int ret;
6883 unsigned char const * const peer_cert_digest =
6884 ssl->session->peer_cert_digest;
6885 mbedtls_md_type_t const peer_cert_digest_type =
6886 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006887 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00006888 mbedtls_md_info_from_type( peer_cert_digest_type );
6889 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6890 size_t digest_len;
6891
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006892 if( peer_cert_digest == NULL ||
6893 digest_info == MBEDTLS_MD_INVALID_HANDLE )
6894 {
Hanno Beckerdf759382019-02-05 17:02:46 +00006895 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01006896 }
Hanno Beckerdf759382019-02-05 17:02:46 +00006897
6898 digest_len = mbedtls_md_get_size( digest_info );
6899 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6900 return( -1 );
6901
6902 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6903 if( ret != 0 )
6904 return( -1 );
6905
6906 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6907}
Hanno Becker5882dd02019-06-06 16:25:57 +01006908#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006909#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006910
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006911/*
6912 * Once the certificate message is read, parse it into a cert chain and
6913 * perform basic checks, but leave actual verification to the caller
6914 */
Hanno Becker35e41772019-02-05 15:37:23 +00006915static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6916 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006917{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006918 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006919#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6920 int crt_cnt=0;
6921#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006922 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006923 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006928 mbedtls_ssl_pend_fatal_alert( ssl,
6929 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006931 }
6932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6934 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006937 mbedtls_ssl_pend_fatal_alert( ssl,
6938 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006940 }
6941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006943
Paul Bakker5121ce52009-01-03 21:22:43 +00006944 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006945 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006946 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006947 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006948
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006949 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006953 mbedtls_ssl_pend_fatal_alert( ssl,
6954 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 }
6957
Hanno Becker33c3dc82019-01-30 14:46:46 +00006958 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6959 i += 3;
6960
Hanno Becker33c3dc82019-01-30 14:46:46 +00006961 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 while( i < ssl->in_hslen )
6963 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006964 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006965 if ( i + 3 > ssl->in_hslen ) {
6966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006967 mbedtls_ssl_pend_fatal_alert( ssl,
6968 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006969 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6970 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006971 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6972 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006973 if( ssl->in_msg[i] != 0 )
6974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006976 mbedtls_ssl_pend_fatal_alert( ssl,
6977 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006979 }
6980
Hanno Becker33c3dc82019-01-30 14:46:46 +00006981 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006982 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00006983 i += 3;
6984
6985 if( n < 128 || i + n > ssl->in_hslen )
6986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006988 mbedtls_ssl_pend_fatal_alert( ssl,
6989 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006991 }
6992
Hanno Becker33c3dc82019-01-30 14:46:46 +00006993 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006994#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6995 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006996 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6997 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006998 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006999 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007000 /* During client-side renegotiation, check that the server's
7001 * end-CRTs hasn't changed compared to the initial handshake,
7002 * mitigating the triple handshake attack. On success, reuse
7003 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7005 if( ssl_check_peer_crt_unchanged( ssl,
7006 &ssl->in_msg[i],
7007 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007008 {
Hanno Becker35e41772019-02-05 15:37:23 +00007009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007010 mbedtls_ssl_pend_fatal_alert( ssl,
7011 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007012 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007013 }
Hanno Becker35e41772019-02-05 15:37:23 +00007014
7015 /* Now we can safely free the original chain. */
7016 ssl_clear_peer_cert( ssl->session );
7017 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007018#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7019
Hanno Becker33c3dc82019-01-30 14:46:46 +00007020 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007021#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007022 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007023#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007024 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007025 * it in-place from the input buffer instead of making a copy. */
7026 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7027#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007028 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007029 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007030 case 0: /*ok*/
7031 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7032 /* Ignore certificate with an unknown algorithm: maybe a
7033 prior certificate was already trusted. */
7034 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007035
Hanno Becker33c3dc82019-01-30 14:46:46 +00007036 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7037 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7038 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007039
Hanno Becker33c3dc82019-01-30 14:46:46 +00007040 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7041 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7042 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007043
Hanno Becker33c3dc82019-01-30 14:46:46 +00007044 default:
7045 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7046 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007047 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007048 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7049 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007050 }
7051
7052 i += n;
7053 }
7054
Hanno Becker35e41772019-02-05 15:37:23 +00007055 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007056 return( 0 );
7057}
7058
Hanno Beckerb8a08572019-02-05 12:49:06 +00007059#if defined(MBEDTLS_SSL_SRV_C)
7060static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7061{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007062 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007063 return( -1 );
7064
7065#if defined(MBEDTLS_SSL_PROTO_SSL3)
7066 /*
7067 * Check if the client sent an empty certificate
7068 */
Hanno Becker2881d802019-05-22 14:44:53 +01007069 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007070 {
7071 if( ssl->in_msglen == 2 &&
7072 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7073 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7074 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7075 {
7076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7077 return( 0 );
7078 }
7079
7080 return( -1 );
7081 }
7082#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7083
7084#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7085 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7086 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7087 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7088 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7089 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7090 {
7091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7092 return( 0 );
7093 }
7094
Hanno Beckerb8a08572019-02-05 12:49:06 +00007095#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7096 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007097
7098 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007099}
7100#endif /* MBEDTLS_SSL_SRV_C */
7101
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007102/* Check if a certificate message is expected.
7103 * Return either
7104 * - SSL_CERTIFICATE_EXPECTED, or
7105 * - SSL_CERTIFICATE_SKIP
7106 * indicating whether a Certificate message is expected or not.
7107 */
7108#define SSL_CERTIFICATE_EXPECTED 0
7109#define SSL_CERTIFICATE_SKIP 1
7110static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7111 int authmode )
7112{
Hanno Becker473f98f2019-06-26 10:27:32 +01007113 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007114 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007115
7116 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7117 return( SSL_CERTIFICATE_SKIP );
7118
7119#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007120 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007121 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007122 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7123 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7124 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007125 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007126 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007127
7128 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7129 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007130 ssl->session_negotiate->verify_result =
7131 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7132 return( SSL_CERTIFICATE_SKIP );
7133 }
7134 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007135#else
7136 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007137#endif /* MBEDTLS_SSL_SRV_C */
7138
7139 return( SSL_CERTIFICATE_EXPECTED );
7140}
7141
Hanno Becker3cf50612019-02-05 14:36:34 +00007142static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7143 int authmode,
7144 mbedtls_x509_crt *chain,
7145 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007146{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007147 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007148 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007149 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007150 mbedtls_x509_crt *ca_chain;
7151 mbedtls_x509_crl *ca_crl;
7152
7153 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7154 return( 0 );
7155
7156#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7157 if( ssl->handshake->sni_ca_chain != NULL )
7158 {
7159 ca_chain = ssl->handshake->sni_ca_chain;
7160 ca_crl = ssl->handshake->sni_ca_crl;
7161 }
7162 else
7163#endif
7164 {
7165 ca_chain = ssl->conf->ca_chain;
7166 ca_crl = ssl->conf->ca_crl;
7167 }
7168
7169 /*
7170 * Main check: verify certificate
7171 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007172 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007173 chain,
7174 ca_chain, ca_crl,
7175 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007176#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007177 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007178#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007179 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007180#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7181 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7182#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7183 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007184
Hanno Becker8c13ee62019-02-26 16:48:17 +00007185 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007186 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007187 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007188 }
7189
7190#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007191 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007192 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7193#endif
7194
7195 /*
7196 * Secondary checks: always done, but change 'ret' only if it was 0
7197 */
7198
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007199#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007200 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007201 int ret;
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007202#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007203 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007204#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007205 mbedtls_pk_context *pk;
7206 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7207 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007208 {
7209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007210 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007211 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007212
7213 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007214 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007215 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007216 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007217 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007218
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007219 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007220#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007221
7222 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007223 {
7224 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7225
7226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007227 if( verify_ret == 0 )
7228 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007229 }
7230 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007231#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007232
7233 if( mbedtls_ssl_check_cert_usage( chain,
7234 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007235 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7236 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007237 &ssl->session_negotiate->verify_result ) != 0 )
7238 {
7239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007240 if( verify_ret == 0 )
7241 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007242 }
7243
7244 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7245 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7246 * with details encoded in the verification flags. All other kinds
7247 * of error codes, including those from the user provided f_vrfy
7248 * functions, are treated as fatal and lead to a failure of
7249 * ssl_parse_certificate even if verification was optional. */
7250 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007251 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7252 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007253 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007254 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007255 }
7256
7257 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7258 {
7259 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007260 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007261 }
7262
Hanno Becker8c13ee62019-02-26 16:48:17 +00007263 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007264 {
7265 uint8_t alert;
7266
7267 /* The certificate may have been rejected for several reasons.
7268 Pick one and send the corresponding alert. Which alert to send
7269 may be a subject of debate in some cases. */
7270 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7271 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7272 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7273 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7274 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7275 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7276 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7277 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7278 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7279 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7280 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7281 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7282 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7283 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7284 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7285 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7286 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7287 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7288 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7289 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7290 else
7291 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007292 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007293 }
7294
7295#if defined(MBEDTLS_DEBUG_C)
7296 if( ssl->session_negotiate->verify_result != 0 )
7297 {
7298 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7299 ssl->session_negotiate->verify_result ) );
7300 }
7301 else
7302 {
7303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7304 }
7305#endif /* MBEDTLS_DEBUG_C */
7306
Hanno Becker8c13ee62019-02-26 16:48:17 +00007307 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007308}
7309
Hanno Becker34106f62019-02-08 14:59:05 +00007310#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007311
7312#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007313static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7314 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007315{
7316 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007317 /* Remember digest of the peer's end-CRT. */
7318 ssl->session_negotiate->peer_cert_digest =
7319 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7320 if( ssl->session_negotiate->peer_cert_digest == NULL )
7321 {
7322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7323 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007324 mbedtls_ssl_pend_fatal_alert( ssl,
7325 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007326
7327 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7328 }
7329
7330 ret = mbedtls_md( mbedtls_md_info_from_type(
7331 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7332 start, len,
7333 ssl->session_negotiate->peer_cert_digest );
7334
7335 ssl->session_negotiate->peer_cert_digest_type =
7336 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7337 ssl->session_negotiate->peer_cert_digest_len =
7338 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7339
7340 return( ret );
7341}
Hanno Becker5882dd02019-06-06 16:25:57 +01007342#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007343
7344static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7345 unsigned char *start, size_t len )
7346{
7347 unsigned char *end = start + len;
7348 int ret;
7349
7350 /* Make a copy of the peer's raw public key. */
7351 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7352 ret = mbedtls_pk_parse_subpubkey( &start, end,
7353 &ssl->handshake->peer_pubkey );
7354 if( ret != 0 )
7355 {
7356 /* We should have parsed the public key before. */
7357 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7358 }
7359
7360 return( 0 );
7361}
7362#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7363
Hanno Becker3cf50612019-02-05 14:36:34 +00007364int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7365{
7366 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007367 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007368#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7369 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7370 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007371 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007372#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007373 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007374#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007375 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007376 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007377
7378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7379
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007380 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7381 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007382 {
7383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007384 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007385 }
7386
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007387#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7388 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007389 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007390 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007391 chain = ssl->handshake->ecrs_peer_cert;
7392 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007393 goto crt_verify;
7394 }
7395#endif
7396
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007397 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007398 {
7399 /* mbedtls_ssl_read_record may have sent an alert already. We
7400 let it decide whether to alert. */
7401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007402 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007403 }
7404
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007405#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007406 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7407 {
7408 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007409
Hanno Beckerb8a08572019-02-05 12:49:06 +00007410 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007411 ret = 0;
7412 else
7413 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007414
Hanno Becker613d4902019-02-05 13:11:17 +00007415 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007416 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007417#endif /* MBEDTLS_SSL_SRV_C */
7418
Hanno Becker35e41772019-02-05 15:37:23 +00007419 /* Clear existing peer CRT structure in case we tried to
7420 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007421 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007422
7423 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7424 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007425 {
7426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7427 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007428 mbedtls_ssl_pend_fatal_alert( ssl,
7429 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007430
Hanno Beckere4aeb762019-02-05 17:19:52 +00007431 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7432 goto exit;
7433 }
7434 mbedtls_x509_crt_init( chain );
7435
7436 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007437 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007438 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007439
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007440#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7441 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007442 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007443
7444crt_verify:
7445 if( ssl->handshake->ecrs_enabled)
7446 rs_ctx = &ssl->handshake->ecrs_ctx;
7447#endif
7448
Hanno Becker3cf50612019-02-05 14:36:34 +00007449 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007450 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007451 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007452 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007453
Hanno Becker3008d282019-02-05 17:02:28 +00007454#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007455 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007456 size_t pk_len;
7457 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007458
Hanno Becker34106f62019-02-08 14:59:05 +00007459 /* We parse the CRT chain without copying, so
7460 * these pointers point into the input buffer,
7461 * and are hence still valid after freeing the
7462 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007463
Hanno Becker5882dd02019-06-06 16:25:57 +01007464#if defined(MBEDTLS_SSL_RENEGOTIATION)
7465 unsigned char *crt_start;
7466 size_t crt_len;
7467
Hanno Becker34106f62019-02-08 14:59:05 +00007468 crt_start = chain->raw.p;
7469 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007470#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007471
Hanno Becker8c13ee62019-02-26 16:48:17 +00007472 pk_start = chain->cache->pk_raw.p;
7473 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007474
7475 /* Free the CRT structures before computing
7476 * digest and copying the peer's public key. */
7477 mbedtls_x509_crt_free( chain );
7478 mbedtls_free( chain );
7479 chain = NULL;
7480
Hanno Becker5882dd02019-06-06 16:25:57 +01007481#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007482 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007483 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007484 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007485#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007486
Hanno Becker34106f62019-02-08 14:59:05 +00007487 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007488 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007489 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007490 }
Hanno Becker34106f62019-02-08 14:59:05 +00007491#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7492 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007493 ssl->session_negotiate->peer_cert = chain;
7494 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007495#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007498
Hanno Becker613d4902019-02-05 13:11:17 +00007499exit:
7500
Hanno Beckere4aeb762019-02-05 17:19:52 +00007501 if( ret == 0 )
7502 ssl->state++;
7503
7504#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7505 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7506 {
7507 ssl->handshake->ecrs_peer_cert = chain;
7508 chain = NULL;
7509 }
7510#endif
7511
7512 if( chain != NULL )
7513 {
7514 mbedtls_x509_crt_free( chain );
7515 mbedtls_free( chain );
7516 }
7517
Paul Bakker5121ce52009-01-03 21:22:43 +00007518 return( ret );
7519}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007520#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007523{
7524 int ret;
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007529 ssl->out_msglen = 1;
7530 ssl->out_msg[0] = 1;
7531
Paul Bakker5121ce52009-01-03 21:22:43 +00007532 ssl->state++;
7533
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007534 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007535 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007536 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007537 return( ret );
7538 }
7539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007541
7542 return( 0 );
7543}
7544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007546{
7547 int ret;
7548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007550
Hanno Becker327c93b2018-08-15 13:56:18 +01007551 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007554 return( ret );
7555 }
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007560 mbedtls_ssl_pend_fatal_alert( ssl,
7561 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007563 }
7564
Hanno Beckere678eaa2018-08-21 14:57:46 +01007565 /* CCS records are only accepted if they have length 1 and content '1',
7566 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007567
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007568 /*
7569 * Switch to our negotiated transform and session parameters for inbound
7570 * data.
7571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007573 ssl->transform_in = ssl->transform_negotiate;
7574 ssl->session_in = ssl->session_negotiate;
7575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007577 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007580 ssl_dtls_replay_reset( ssl );
7581#endif
7582
7583 /* Increment epoch */
7584 if( ++ssl->in_epoch == 0 )
7585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007587 /* This is highly unlikely to happen for legitimate reasons, so
7588 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007590 }
7591 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007592 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007594#if defined(MBEDTLS_SSL_PROTO_TLS)
7595 {
7596 memset( ssl->in_ctr, 0, 8 );
7597 }
7598#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007599
Hanno Beckerf5970a02019-05-08 09:38:41 +01007600 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7603 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007608 mbedtls_ssl_pend_fatal_alert( ssl,
7609 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007611 }
7612 }
7613#endif
7614
Paul Bakker5121ce52009-01-03 21:22:43 +00007615 ssl->state++;
7616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007618
7619 return( 0 );
7620}
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7625 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007626 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007627 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007628#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7630#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007631 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007632#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007634 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007635#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007637}
7638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007642
7643 /*
7644 * Free our handshake params
7645 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007646 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007648 ssl->handshake = NULL;
7649
7650 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007651 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007652 */
7653 if( ssl->transform )
7654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 mbedtls_ssl_transform_free( ssl->transform );
7656 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007657 }
7658 ssl->transform = ssl->transform_negotiate;
7659 ssl->transform_negotiate = NULL;
7660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007662}
7663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#if defined(MBEDTLS_SSL_RENEGOTIATION)
7669 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007672 ssl->renego_records_seen = 0;
7673 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007674#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007675
7676 /*
7677 * Free the previous session and switch in the current one
7678 */
Paul Bakker0a597072012-09-25 21:55:46 +00007679 if( ssl->session )
7680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007682 /* RFC 7366 3.1: keep the EtM state */
7683 ssl->session_negotiate->encrypt_then_mac =
7684 ssl->session->encrypt_then_mac;
7685#endif
7686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 mbedtls_ssl_session_free( ssl->session );
7688 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007689 }
7690 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007691 ssl->session_negotiate = NULL;
7692
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007693#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007694 /*
7695 * Add cache entry
7696 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007697 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007698 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007699 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007700 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007701 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007703 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007704#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007707 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007708 ssl->handshake->flight != NULL )
7709 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007710 /* Cancel handshake timer */
7711 ssl_set_timer( ssl, 0 );
7712
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007713 /* Keep last flight around in case we need to resend it:
7714 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007716 }
7717 else
7718#endif
7719 ssl_handshake_wrapup_free_hs_transform( ssl );
7720
Paul Bakker48916f92012-09-16 19:57:18 +00007721 ssl->state++;
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007724}
7725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007727{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007728 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007731
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007732 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007733
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007734 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7735 mbedtls_ssl_suite_get_mac(
7736 mbedtls_ssl_ciphersuite_from_id(
7737 mbedtls_ssl_session_get_ciphersuite(
7738 ssl->session_negotiate ) ) ),
7739 ssl, ssl->out_msg + 4,
7740 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007741
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007742 /*
7743 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7744 * may define some other value. Currently (early 2016), no defined
7745 * ciphersuite does this (and this is unlikely to change as activity has
7746 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7747 */
Hanno Becker2881d802019-05-22 14:44:53 +01007748 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007751 ssl->verify_data_len = hash_len;
7752 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007753#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007754
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7757 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007758
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007759#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007760 /*
7761 * In case of session resuming, invert the client and server
7762 * ChangeCipherSpec messages order.
7763 */
Paul Bakker0a597072012-09-25 21:55:46 +00007764 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007767 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7768 MBEDTLS_SSL_IS_CLIENT )
7769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007771 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007774 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7775 MBEDTLS_SSL_IS_SERVER )
7776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007778 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007779#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007780 }
7781 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007782#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007783 ssl->state++;
7784
Paul Bakker48916f92012-09-16 19:57:18 +00007785 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007786 * Switch to our negotiated transform and session parameters for outbound
7787 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007792 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007793 {
7794 unsigned char i;
7795
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007796 /* Remember current epoch settings for resending */
7797 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007798 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007799
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007800 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007801 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007802
7803 /* Increment epoch */
7804 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007805 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007806 break;
7807
7808 /* The loop goes to its end iff the counter is wrapping */
7809 if( i == 0 )
7810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7812 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007813 }
7814 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007815 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007817#if defined(MBEDTLS_SSL_PROTO_TLS)
7818 {
7819 memset( ssl->cur_out_ctr, 0, 8 );
7820 }
7821#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007822
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007823 ssl->transform_out = ssl->transform_negotiate;
7824 ssl->session_out = ssl->session_negotiate;
7825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7827 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7832 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007833 }
7834 }
7835#endif
7836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007838 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007840#endif
7841
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007842 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 return( ret );
7846 }
7847
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007849 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007850 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7851 {
7852 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7853 return( ret );
7854 }
7855#endif
7856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007858
7859 return( 0 );
7860}
7861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007863#define SSL_MAX_HASH_LEN 36
7864#else
7865#define SSL_MAX_HASH_LEN 12
7866#endif
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007869{
Paul Bakker23986e52011-04-24 08:57:21 +00007870 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007871 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007872 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007875
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007876 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7877 mbedtls_ssl_suite_get_mac(
7878 mbedtls_ssl_ciphersuite_from_id(
7879 mbedtls_ssl_session_get_ciphersuite(
7880 ssl->session_negotiate ) ) ),
7881 ssl, buf,
7882 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007883
Hanno Becker327c93b2018-08-15 13:56:18 +01007884 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007887 return( ret );
7888 }
7889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007893 mbedtls_ssl_pend_fatal_alert( ssl,
7894 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007896 }
7897
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007898 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007900 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007901 hash_len = 36;
7902 else
7903#endif
7904 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7907 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007910 mbedtls_ssl_pend_fatal_alert( ssl,
7911 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007913 }
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007916 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007919 mbedtls_ssl_pend_fatal_alert( ssl,
7920 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007922 }
7923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007925 ssl->verify_data_len = hash_len;
7926 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007927#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007928
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007929#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007930 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007933 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007937 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007939#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007940 }
7941 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007942#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007943 ssl->state++;
7944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007946 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007948#endif
7949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007951
7952 return( 0 );
7953}
7954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007956{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7960 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7961 mbedtls_md5_init( &handshake->fin_md5 );
7962 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007963 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7964 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7967#if defined(MBEDTLS_SHA256_C)
7968 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007969 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007970#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971#if defined(MBEDTLS_SHA512_C)
7972 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007973 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007976
Hanno Becker7e5437a2017-04-28 17:15:26 +01007977#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7978 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7979 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7980#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_DHM_C)
7983 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985#if defined(MBEDTLS_ECDH_C)
7986 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007987#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007988#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007989 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007990#if defined(MBEDTLS_SSL_CLI_C)
7991 handshake->ecjpake_cache = NULL;
7992 handshake->ecjpake_cache_len = 0;
7993#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007994#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007995
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007996#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007997 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007998#endif
7999
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008000#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8001 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8002#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008003
8004#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8005 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8006 mbedtls_pk_init( &handshake->peer_pubkey );
8007#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008008}
8009
Hanno Becker611a83b2018-01-03 14:27:32 +00008010void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008011{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8015 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008016
Hanno Becker92231322018-01-03 15:32:51 +00008017#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 mbedtls_md_init( &transform->md_ctx_enc );
8019 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008020#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008021}
8022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008026}
8027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008029{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008030 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008031 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008033 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008035 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008036 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008037
8038 /*
8039 * Either the pointers are now NULL or cleared properly and can be freed.
8040 * Now allocate missing structures.
8041 */
8042 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008043 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008044 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008045 }
Paul Bakker48916f92012-09-16 19:57:18 +00008046
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008047 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008048 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008049 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008050 }
Paul Bakker48916f92012-09-16 19:57:18 +00008051
Paul Bakker82788fb2014-10-20 13:59:19 +02008052 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008053 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008054 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008055 }
Paul Bakker48916f92012-09-16 19:57:18 +00008056
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008057 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008058 if( ssl->handshake == NULL ||
8059 ssl->transform_negotiate == NULL ||
8060 ssl->session_negotiate == NULL )
8061 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064 mbedtls_free( ssl->handshake );
8065 mbedtls_free( ssl->transform_negotiate );
8066 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008067
8068 ssl->handshake = NULL;
8069 ssl->transform_negotiate = NULL;
8070 ssl->session_negotiate = NULL;
8071
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008072 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008073 }
8074
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008075 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008077 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008078 ssl_handshake_params_init( ssl->handshake );
8079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008080#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008081 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008082 {
8083 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008084
Hanno Becker2d9623f2019-06-13 12:07:05 +01008085 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008086 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8087 else
8088 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8089 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008090#endif
8091
Paul Bakker48916f92012-09-16 19:57:18 +00008092 return( 0 );
8093}
8094
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008095#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008096/* Dummy cookie callbacks for defaults */
8097static int ssl_cookie_write_dummy( void *ctx,
8098 unsigned char **p, unsigned char *end,
8099 const unsigned char *cli_id, size_t cli_id_len )
8100{
8101 ((void) ctx);
8102 ((void) p);
8103 ((void) end);
8104 ((void) cli_id);
8105 ((void) cli_id_len);
8106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008108}
8109
8110static int ssl_cookie_check_dummy( void *ctx,
8111 const unsigned char *cookie, size_t cookie_len,
8112 const unsigned char *cli_id, size_t cli_id_len )
8113{
8114 ((void) ctx);
8115 ((void) cookie);
8116 ((void) cookie_len);
8117 ((void) cli_id);
8118 ((void) cli_id_len);
8119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008121}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008122#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008123
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008124/* Once ssl->out_hdr as the address of the beginning of the
8125 * next outgoing record is set, deduce the other pointers.
8126 *
8127 * Note: For TLS, we save the implicit record sequence number
8128 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8129 * and the caller has to make sure there's space for this.
8130 */
8131
8132static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8133 mbedtls_ssl_transform *transform )
8134{
8135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008136 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008137 {
8138 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008139#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008140 ssl->out_cid = ssl->out_ctr + 8;
8141 ssl->out_len = ssl->out_cid;
8142 if( transform != NULL )
8143 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008144#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008145 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008146#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008147 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008148 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008149 MBEDTLS_SSL_TRANSPORT_ELSE
8150#endif /* MBEDTLS_SSL_PROTO_DTLS */
8151#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008152 {
8153 ssl->out_ctr = ssl->out_hdr - 8;
8154 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008155#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008156 ssl->out_cid = ssl->out_len;
8157#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008158 ssl->out_iv = ssl->out_hdr + 5;
8159 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008160#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008161
8162 /* Adjust out_msg to make space for explicit IV, if used. */
8163 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008164 mbedtls_ssl_ver_geq(
8165 mbedtls_ssl_get_minor_ver( ssl ),
8166 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008167 {
8168 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8169 }
8170 else
8171 ssl->out_msg = ssl->out_iv;
8172}
8173
8174/* Once ssl->in_hdr as the address of the beginning of the
8175 * next incoming record is set, deduce the other pointers.
8176 *
8177 * Note: For TLS, we save the implicit record sequence number
8178 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8179 * and the caller has to make sure there's space for this.
8180 */
8181
Hanno Beckerf5970a02019-05-08 09:38:41 +01008182static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008183{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008184 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008185 * of unprotected TLS/DTLS records, with ssl->in_msg
8186 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008187 *
8188 * When decrypting a protected record, ssl->in_msg
8189 * will be shifted to point to the beginning of the
8190 * record plaintext.
8191 */
8192
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008193#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008194 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008195 {
Hanno Becker70e79282019-05-03 14:34:53 +01008196 /* This sets the header pointers to match records
8197 * without CID. When we receive a record containing
8198 * a CID, the fields are shifted accordingly in
8199 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008200 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008201#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008202 ssl->in_cid = ssl->in_ctr + 8;
8203 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008204#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008205 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008206#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008207 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008208 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008209 MBEDTLS_SSL_TRANSPORT_ELSE
8210#endif /* MBEDTLS_SSL_PROTO_DTLS */
8211#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008212 {
8213 ssl->in_ctr = ssl->in_hdr - 8;
8214 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008215#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008216 ssl->in_cid = ssl->in_len;
8217#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008218 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008219 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008220#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008221}
8222
Paul Bakker5121ce52009-01-03 21:22:43 +00008223/*
8224 * Initialize an SSL context
8225 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008226void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8227{
8228 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8229}
8230
8231/*
8232 * Setup an SSL context
8233 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008234
8235static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8236{
8237 /* Set the incoming and outgoing record pointers. */
8238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008239 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008240 {
8241 ssl->out_hdr = ssl->out_buf;
8242 ssl->in_hdr = ssl->in_buf;
8243 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008244 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008245#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008246#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008247 {
8248 ssl->out_hdr = ssl->out_buf + 8;
8249 ssl->in_hdr = ssl->in_buf + 8;
8250 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008251#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008252
8253 /* Derive other internal pointers. */
8254 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008255 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008256}
8257
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008258int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008259 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008260{
Paul Bakker48916f92012-09-16 19:57:18 +00008261 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008262
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008263 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008264
Hanno Beckeref982d52019-07-23 15:56:18 +01008265#if defined(MBEDTLS_USE_TINYCRYPT)
8266 uECC_set_rng( &uecc_rng_wrapper );
8267#endif
8268
Paul Bakker62f2dee2012-09-28 07:31:51 +00008269 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008270 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008271 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008272
8273 /* Set to NULL in case of an error condition */
8274 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008275
Angus Grattond8213d02016-05-25 20:56:48 +10008276 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8277 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008278 {
Angus Grattond8213d02016-05-25 20:56:48 +10008279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008280 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008281 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008282 }
8283
8284 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8285 if( ssl->out_buf == NULL )
8286 {
8287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008288 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008289 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008290 }
8291
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008292 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008293
Paul Bakker48916f92012-09-16 19:57:18 +00008294 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008295 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008296
Hanno Beckerc8f52992019-07-25 11:15:08 +01008297 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008298
Paul Bakker5121ce52009-01-03 21:22:43 +00008299 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008300
8301error:
8302 mbedtls_free( ssl->in_buf );
8303 mbedtls_free( ssl->out_buf );
8304
8305 ssl->conf = NULL;
8306
8307 ssl->in_buf = NULL;
8308 ssl->out_buf = NULL;
8309
8310 ssl->in_hdr = NULL;
8311 ssl->in_ctr = NULL;
8312 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008313 ssl->in_msg = NULL;
8314
8315 ssl->out_hdr = NULL;
8316 ssl->out_ctr = NULL;
8317 ssl->out_len = NULL;
8318 ssl->out_iv = NULL;
8319 ssl->out_msg = NULL;
8320
k-stachowiak9f7798e2018-07-31 16:52:32 +02008321 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008322}
8323
8324/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008325 * Reset an initialized and used SSL context for re-use while retaining
8326 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008327 *
8328 * If partial is non-zero, keep data in the input buffer and client ID.
8329 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008330 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008331static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008332{
Paul Bakker48916f92012-09-16 19:57:18 +00008333 int ret;
8334
Hanno Becker7e772132018-08-10 12:38:21 +01008335#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8336 !defined(MBEDTLS_SSL_SRV_C)
8337 ((void) partial);
8338#endif
8339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008341
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008342 /* Cancel any possibly running timer */
8343 ssl_set_timer( ssl, 0 );
8344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345#if defined(MBEDTLS_SSL_RENEGOTIATION)
8346 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008347 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008348
8349 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8351 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008352#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008354
Paul Bakker7eb013f2011-10-06 12:37:39 +00008355 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008356 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008357
8358 ssl->in_msgtype = 0;
8359 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008361 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008362 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008365 ssl_dtls_replay_reset( ssl );
8366#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008367
8368 ssl->in_hslen = 0;
8369 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008370
8371 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008372
8373 ssl->out_msgtype = 0;
8374 ssl->out_msglen = 0;
8375 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8377 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008378 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008379#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008380
Hanno Becker19859472018-08-06 09:40:20 +01008381 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8382
Paul Bakker48916f92012-09-16 19:57:18 +00008383 ssl->transform_in = NULL;
8384 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008385
Hanno Becker78640902018-08-13 16:35:15 +01008386 ssl->session_in = NULL;
8387 ssl->session_out = NULL;
8388
Angus Grattond8213d02016-05-25 20:56:48 +10008389 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008390
8391#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008392 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008393#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8394 {
8395 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008396 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008397 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8400 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8403 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8406 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008407 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008408 }
8409#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008410
Paul Bakker48916f92012-09-16 19:57:18 +00008411 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413 mbedtls_ssl_transform_free( ssl->transform );
8414 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008415 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008416 }
Paul Bakker48916f92012-09-16 19:57:18 +00008417
Paul Bakkerc0463502013-02-14 11:19:38 +01008418 if( ssl->session )
8419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420 mbedtls_ssl_session_free( ssl->session );
8421 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008422 ssl->session = NULL;
8423 }
8424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008426 ssl->alpn_chosen = NULL;
8427#endif
8428
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008429#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008430#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008431 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008432#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008433 {
8434 mbedtls_free( ssl->cli_id );
8435 ssl->cli_id = NULL;
8436 ssl->cli_id_len = 0;
8437 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008438#endif
8439
Paul Bakker48916f92012-09-16 19:57:18 +00008440 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8441 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008442
8443 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008444}
8445
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008446/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008447 * Reset an initialized and used SSL context for re-use while retaining
8448 * all application-set variables, function pointers and data.
8449 */
8450int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8451{
8452 return( ssl_session_reset_int( ssl, 0 ) );
8453}
8454
8455/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008456 * SSL set accessors
8457 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008458#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008459void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008460{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008461 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008462}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008463#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008464
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008465void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008466{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008467 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008468}
8469
Hanno Becker7f376f42019-06-12 16:20:48 +01008470#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8471 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008472void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008473{
Hanno Becker7f376f42019-06-12 16:20:48 +01008474 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008475}
Hanno Becker7f376f42019-06-12 16:20:48 +01008476#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008477
Hanno Beckerde671542019-06-12 16:30:46 +01008478#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8479 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8480void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8481 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008482{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008483 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008484}
Hanno Beckerde671542019-06-12 16:30:46 +01008485#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008488
Hanno Becker1841b0a2018-08-24 11:13:57 +01008489void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8490 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008491{
8492 ssl->disable_datagram_packing = !allow_packing;
8493}
8494
Hanno Becker1f835fa2019-06-13 10:14:59 +01008495#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8496 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008497void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8498 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008499{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008500 conf->hs_timeout_min = min;
8501 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008502}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008503#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8504 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8505void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8506 uint32_t min, uint32_t max )
8507{
8508 ((void) conf);
8509 ((void) min);
8510 ((void) max);
8511}
8512#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8513 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8514
8515#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008516
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008517void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008518{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008519#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8520 conf->authmode = authmode;
8521#else
8522 ((void) conf);
8523 ((void) authmode);
8524#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008525}
8526
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008527#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8528 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008529void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008530 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008531 void *p_vrfy )
8532{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008533 conf->f_vrfy = f_vrfy;
8534 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008535}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008536#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008537
Hanno Beckerece325c2019-06-13 15:39:27 +01008538#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008539void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008540 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008541 void *p_rng )
8542{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008543 conf->f_rng = f_rng;
8544 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008545}
Hanno Beckerece325c2019-06-13 15:39:27 +01008546#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008547
Hanno Becker14a4a442019-07-02 17:00:34 +01008548#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008549void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008550 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008551 void *p_dbg )
8552{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008553 conf->f_dbg = f_dbg;
8554 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008555}
Hanno Becker14a4a442019-07-02 17:00:34 +01008556#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008557
Hanno Beckera58a8962019-06-13 16:11:15 +01008558#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8559 !defined(MBEDTLS_SSL_CONF_SEND) && \
8560 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008562 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008563 mbedtls_ssl_send_t *f_send,
8564 mbedtls_ssl_recv_t *f_recv,
8565 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008566{
Hanno Beckera58a8962019-06-13 16:11:15 +01008567 ssl->p_bio = p_bio;
8568 ssl->f_send = f_send;
8569 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008570 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008571}
Hanno Beckera58a8962019-06-13 16:11:15 +01008572#else
8573void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8574 void *p_bio )
8575{
8576 ssl->p_bio = p_bio;
8577}
8578#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008579
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008580#if defined(MBEDTLS_SSL_PROTO_DTLS)
8581void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8582{
8583 ssl->mtu = mtu;
8584}
8585#endif
8586
Hanno Becker1f835fa2019-06-13 10:14:59 +01008587#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008588void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008589{
8590 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008591}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008592#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008593
Hanno Becker0ae6b242019-06-13 16:45:36 +01008594#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8595 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008596void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8597 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008598 mbedtls_ssl_set_timer_t *f_set_timer,
8599 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008600{
8601 ssl->p_timer = p_timer;
8602 ssl->f_set_timer = f_set_timer;
8603 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008604 /* Make sure we start with no timer running */
8605 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008606}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008607#else
8608void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8609 void *p_timer )
8610{
8611 ssl->p_timer = p_timer;
8612 /* Make sure we start with no timer running */
8613 ssl_set_timer( ssl, 0 );
8614}
8615#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008616
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008617#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008618void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008619 void *p_cache,
8620 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8621 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008622{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008623 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008624 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008625 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008626}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008627#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008628
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008629#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008631{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008632 int ret;
8633
8634 if( ssl == NULL ||
8635 session == NULL ||
8636 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008637 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008640 }
8641
Hanno Becker58fccf22019-02-06 14:30:46 +00008642 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8643 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008644 return( ret );
8645
Paul Bakker0a597072012-09-25 21:55:46 +00008646 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008647
8648 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008649}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008650#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008651
Hanno Becker73f4cb12019-06-27 13:51:07 +01008652#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008653void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008654 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008655{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008656 conf->ciphersuite_list[0] = ciphersuites;
8657 conf->ciphersuite_list[1] = ciphersuites;
8658 conf->ciphersuite_list[2] = ciphersuites;
8659 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008660}
8661
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008662void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008663 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008664 int major, int minor )
8665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008667 return;
8668
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008669 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
8670 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
8671 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008672 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008673 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008674
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008675 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
8676 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008677}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008678#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008681void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008682 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008683{
8684 conf->cert_profile = profile;
8685}
8686
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008687/* Append a new keycert entry to a (possibly empty) list */
8688static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8689 mbedtls_x509_crt *cert,
8690 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008691{
niisato8ee24222018-06-25 19:05:48 +09008692 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008693
niisato8ee24222018-06-25 19:05:48 +09008694 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8695 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008696 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008697
niisato8ee24222018-06-25 19:05:48 +09008698 new_cert->cert = cert;
8699 new_cert->key = key;
8700 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008701
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008702 /* Update head is the list was null, else add to the end */
8703 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008704 {
niisato8ee24222018-06-25 19:05:48 +09008705 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008706 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008707 else
8708 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008709 mbedtls_ssl_key_cert *cur = *head;
8710 while( cur->next != NULL )
8711 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008712 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008713 }
8714
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008715 return( 0 );
8716}
8717
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008718int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008719 mbedtls_x509_crt *own_cert,
8720 mbedtls_pk_context *pk_key )
8721{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008722 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008723}
8724
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008725void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008726 mbedtls_x509_crt *ca_chain,
8727 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008728{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008729 conf->ca_chain = ca_chain;
8730 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008731}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008732#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008733
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008734#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8735int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8736 mbedtls_x509_crt *own_cert,
8737 mbedtls_pk_context *pk_key )
8738{
8739 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8740 own_cert, pk_key ) );
8741}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008742
8743void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8744 mbedtls_x509_crt *ca_chain,
8745 mbedtls_x509_crl *ca_crl )
8746{
8747 ssl->handshake->sni_ca_chain = ca_chain;
8748 ssl->handshake->sni_ca_crl = ca_crl;
8749}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008750
8751void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8752 int authmode )
8753{
8754 ssl->handshake->sni_authmode = authmode;
8755}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008756#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8757
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008758#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008759/*
8760 * Set EC J-PAKE password for current handshake
8761 */
8762int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8763 const unsigned char *pw,
8764 size_t pw_len )
8765{
8766 mbedtls_ecjpake_role role;
8767
Janos Follath8eb64132016-06-03 15:40:57 +01008768 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8770
Hanno Becker2d9623f2019-06-13 12:07:05 +01008771 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008772 role = MBEDTLS_ECJPAKE_SERVER;
8773 else
8774 role = MBEDTLS_ECJPAKE_CLIENT;
8775
8776 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8777 role,
8778 MBEDTLS_MD_SHA256,
8779 MBEDTLS_ECP_DP_SECP256R1,
8780 pw, pw_len ) );
8781}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008782#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008785int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008786 const unsigned char *psk, size_t psk_len,
8787 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008788{
Paul Bakker6db455e2013-09-18 17:29:31 +02008789 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008794
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008795 /* Identity len will be encoded on two bytes */
8796 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008797 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008798 {
8799 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8800 }
8801
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008802 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008803 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008804 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008805
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008806 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008807 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008808 conf->psk_len = 0;
8809 }
8810 if( conf->psk_identity != NULL )
8811 {
8812 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008813 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008814 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008815 }
8816
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008817 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8818 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008819 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008820 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008821 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008822 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008823 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008824 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008825 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008826
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008827 conf->psk_len = psk_len;
8828 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008829
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008830 memcpy( conf->psk, psk, conf->psk_len );
8831 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008832
8833 return( 0 );
8834}
8835
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008836int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8837 const unsigned char *psk, size_t psk_len )
8838{
8839 if( psk == NULL || ssl->handshake == NULL )
8840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8841
8842 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8844
8845 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008846 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008847 mbedtls_platform_zeroize( ssl->handshake->psk,
8848 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008849 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008850 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008851 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008852
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008853 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008854 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008855
8856 ssl->handshake->psk_len = psk_len;
8857 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8858
8859 return( 0 );
8860}
8861
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008862void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008864 size_t),
8865 void *p_psk )
8866{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008867 conf->f_psk = f_psk;
8868 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008871
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008872#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008873
8874#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008875int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008876{
8877 int ret;
8878
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008879 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8880 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8881 {
8882 mbedtls_mpi_free( &conf->dhm_P );
8883 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008884 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008885 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008886
8887 return( 0 );
8888}
Hanno Becker470a8c42017-10-04 15:28:46 +01008889#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008890
Hanno Beckera90658f2017-10-04 15:29:08 +01008891int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8892 const unsigned char *dhm_P, size_t P_len,
8893 const unsigned char *dhm_G, size_t G_len )
8894{
8895 int ret;
8896
8897 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8898 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8899 {
8900 mbedtls_mpi_free( &conf->dhm_P );
8901 mbedtls_mpi_free( &conf->dhm_G );
8902 return( ret );
8903 }
8904
8905 return( 0 );
8906}
Paul Bakker5121ce52009-01-03 21:22:43 +00008907
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008908int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008909{
8910 int ret;
8911
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008912 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8913 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8914 {
8915 mbedtls_mpi_free( &conf->dhm_P );
8916 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008917 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008918 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008919
8920 return( 0 );
8921}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008922#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008923
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008924#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8925/*
8926 * Set the minimum length for Diffie-Hellman parameters
8927 */
8928void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8929 unsigned int bitlen )
8930{
8931 conf->dhm_min_bitlen = bitlen;
8932}
8933#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8934
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008935#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008936/*
8937 * Set allowed/preferred hashes for handshake signatures
8938 */
8939void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8940 const int *hashes )
8941{
Hanno Becker56595f42019-06-19 16:31:38 +01008942#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008943 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008944#else
8945 ((void) conf);
8946 ((void) hashes);
8947#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008948}
Hanno Becker947194e2017-04-07 13:25:49 +01008949#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008950
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008951#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008952#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008953/*
8954 * Set the allowed elliptic curves
8955 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008956void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008957 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008958{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008959 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008960}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008961#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008962#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008963
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008964#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008965int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008966{
Hanno Becker947194e2017-04-07 13:25:49 +01008967 /* Initialize to suppress unnecessary compiler warning */
8968 size_t hostname_len = 0;
8969
8970 /* Check if new hostname is valid before
8971 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008972 if( hostname != NULL )
8973 {
8974 hostname_len = strlen( hostname );
8975
8976 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8978 }
8979
8980 /* Now it's clear that we will overwrite the old hostname,
8981 * so we can free it safely */
8982
8983 if( ssl->hostname != NULL )
8984 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008985 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008986 mbedtls_free( ssl->hostname );
8987 }
8988
8989 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008990
Paul Bakker5121ce52009-01-03 21:22:43 +00008991 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008992 {
8993 ssl->hostname = NULL;
8994 }
8995 else
8996 {
8997 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008998 if( ssl->hostname == NULL )
8999 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009000
Hanno Becker947194e2017-04-07 13:25:49 +01009001 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009002
Hanno Becker947194e2017-04-07 13:25:49 +01009003 ssl->hostname[hostname_len] = '\0';
9004 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009005
9006 return( 0 );
9007}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009008#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009009
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009010#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009011void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009013 const unsigned char *, size_t),
9014 void *p_sni )
9015{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009016 conf->f_sni = f_sni;
9017 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009018}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009021#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009022int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009023{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009024 size_t cur_len, tot_len;
9025 const char **p;
9026
9027 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009028 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9029 * MUST NOT be truncated."
9030 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009031 */
9032 tot_len = 0;
9033 for( p = protos; *p != NULL; p++ )
9034 {
9035 cur_len = strlen( *p );
9036 tot_len += cur_len;
9037
9038 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009040 }
9041
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009042 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009043
9044 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009045}
9046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009047const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009048{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009049 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009050}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009052
Hanno Becker33b9b252019-07-05 11:23:25 +01009053#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9054 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9055void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9056 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009058 conf->max_major_ver = major;
9059 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009060}
Hanno Becker33b9b252019-07-05 11:23:25 +01009061#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9062 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009063
Hanno Becker33b9b252019-07-05 11:23:25 +01009064#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9065 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9066void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9067 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009068{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009069 conf->min_major_ver = major;
9070 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009071}
Hanno Becker33b9b252019-07-05 11:23:25 +01009072#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9073 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009076void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009077{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009078 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009079}
9080#endif
9081
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009082#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009083void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9084 char cert_req_ca_list )
9085{
9086 conf->cert_req_ca_list = cert_req_ca_list;
9087}
9088#endif
9089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009091void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009092{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009093 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009094}
9095#endif
9096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009098#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009099void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009100{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009101 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009102}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009103#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9104#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009105void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009106 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009107{
9108 conf->enforce_extended_master_secret = ems_enf;
9109}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009110#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009111#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009112
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009113#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009114void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009116 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009117}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009118#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009124 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009127 }
9128
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009129 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009130
9131 return( 0 );
9132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009135#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009136void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009138 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009143void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009144{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009145 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009146}
9147#endif
9148
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009149#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009150void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009151{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009152 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009153}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009154#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009156#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009157void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009158{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009159 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009160}
9161
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009162void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009163{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009164 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009165}
9166
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009167void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009168 const unsigned char period[8] )
9169{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009170 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009174#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009175#if defined(MBEDTLS_SSL_CLI_C)
9176void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009177{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009178 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009179}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009180#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009181
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009182#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009183void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9184 mbedtls_ssl_ticket_write_t *f_ticket_write,
9185 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9186 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009187{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009188 conf->f_ticket_write = f_ticket_write;
9189 conf->f_ticket_parse = f_ticket_parse;
9190 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009191}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009194
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009195#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9196void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9197 mbedtls_ssl_export_keys_t *f_export_keys,
9198 void *p_export_keys )
9199{
9200 conf->f_export_keys = f_export_keys;
9201 conf->p_export_keys = p_export_keys;
9202}
9203#endif
9204
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009205#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009206void mbedtls_ssl_conf_async_private_cb(
9207 mbedtls_ssl_config *conf,
9208 mbedtls_ssl_async_sign_t *f_async_sign,
9209 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9210 mbedtls_ssl_async_resume_t *f_async_resume,
9211 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009212 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009213{
9214 conf->f_async_sign_start = f_async_sign;
9215 conf->f_async_decrypt_start = f_async_decrypt;
9216 conf->f_async_resume = f_async_resume;
9217 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009218 conf->p_async_config_data = async_config_data;
9219}
9220
Gilles Peskine8f97af72018-04-26 11:46:10 +02009221void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9222{
9223 return( conf->p_async_config_data );
9224}
9225
Gilles Peskine1febfef2018-04-30 11:54:39 +02009226void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009227{
9228 if( ssl->handshake == NULL )
9229 return( NULL );
9230 else
9231 return( ssl->handshake->user_async_ctx );
9232}
9233
Gilles Peskine1febfef2018-04-30 11:54:39 +02009234void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009235 void *ctx )
9236{
9237 if( ssl->handshake != NULL )
9238 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009239}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009240#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009241
Paul Bakker5121ce52009-01-03 21:22:43 +00009242/*
9243 * SSL get accessors
9244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009245size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009246{
9247 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9248}
9249
Hanno Becker8b170a02017-10-10 11:51:19 +01009250int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9251{
9252 /*
9253 * Case A: We're currently holding back
9254 * a message for further processing.
9255 */
9256
9257 if( ssl->keep_current_message == 1 )
9258 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009260 return( 1 );
9261 }
9262
9263 /*
9264 * Case B: Further records are pending in the current datagram.
9265 */
9266
9267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009268 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009269 ssl->in_left > ssl->next_record_offset )
9270 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009272 return( 1 );
9273 }
9274#endif /* MBEDTLS_SSL_PROTO_DTLS */
9275
9276 /*
9277 * Case C: A handshake message is being processed.
9278 */
9279
Hanno Becker8b170a02017-10-10 11:51:19 +01009280 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9281 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009283 return( 1 );
9284 }
9285
9286 /*
9287 * Case D: An application data message is being processed
9288 */
9289 if( ssl->in_offt != NULL )
9290 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009292 return( 1 );
9293 }
9294
9295 /*
9296 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009297 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009298 * we implement support for multiple alerts in single records.
9299 */
9300
9301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9302 return( 0 );
9303}
9304
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009305uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009306{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009307 if( ssl->session != NULL )
9308 return( ssl->session->verify_result );
9309
9310 if( ssl->session_negotiate != NULL )
9311 return( ssl->session_negotiate->verify_result );
9312
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009313 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009314}
9315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009317{
Hanno Beckere02758c2019-06-26 15:31:31 +01009318 int suite;
9319
Paul Bakker926c8e42013-03-06 10:23:34 +01009320 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009321 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009322
Hanno Beckere02758c2019-06-26 15:31:31 +01009323 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9324 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009325}
9326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009328{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009330 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009331 {
Hanno Becker2881d802019-05-22 14:44:53 +01009332 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009335 return( "DTLSv1.0" );
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009338 return( "DTLSv1.2" );
9339
9340 default:
9341 return( "unknown (DTLS)" );
9342 }
9343 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009344 MBEDTLS_SSL_TRANSPORT_ELSE
9345#endif /* MBEDTLS_SSL_PROTO_DTLS */
9346#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009347 {
Hanno Becker2881d802019-05-22 14:44:53 +01009348 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009349 {
9350 case MBEDTLS_SSL_MINOR_VERSION_0:
9351 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009352
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009353 case MBEDTLS_SSL_MINOR_VERSION_1:
9354 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009355
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009356 case MBEDTLS_SSL_MINOR_VERSION_2:
9357 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009358
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009359 case MBEDTLS_SSL_MINOR_VERSION_3:
9360 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009361
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009362 default:
9363 return( "unknown" );
9364 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009365 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009366#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009367}
9368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009370{
Hanno Becker3136ede2018-08-17 15:28:19 +01009371 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009373
Hanno Becker43395762019-05-03 14:46:38 +01009374 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9375
Hanno Becker78640902018-08-13 16:35:15 +01009376 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009377 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379#if defined(MBEDTLS_ZLIB_SUPPORT)
9380 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9381 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009382#endif
9383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009385 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009386#if defined(MBEDTLS_GCM_C) || \
9387 defined(MBEDTLS_CCM_C) || \
9388 defined(MBEDTLS_CHACHAPOLY_C)
9389#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009391#endif
9392#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009394#endif
9395#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009396 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009397#endif
9398 transform_expansion =
9399 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009400 break;
9401
Hanno Beckera9d5c452019-07-25 16:47:12 +01009402#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9403 MBEDTLS_CHACHAPOLY_C */
9404
9405#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9406 case MBEDTLS_MODE_STREAM:
9407 transform_expansion = transform->maclen;
9408 break;
9409#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9410
9411#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009413 {
9414 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009415
9416 block_size = mbedtls_cipher_get_block_size(
9417 &transform->cipher_ctx_enc );
9418
Hanno Becker3136ede2018-08-17 15:28:19 +01009419 /* Expansion due to the addition of the MAC. */
9420 transform_expansion += transform->maclen;
9421
9422 /* Expansion due to the addition of CBC padding;
9423 * Theoretically up to 256 bytes, but we never use
9424 * more than the block size of the underlying cipher. */
9425 transform_expansion += block_size;
9426
9427 /* For TLS 1.1 or higher, an explicit IV is added
9428 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009429#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009430 if( mbedtls_ssl_ver_geq(
9431 mbedtls_ssl_get_minor_ver( ssl ),
9432 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9433 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009434 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009435 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009436#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009437
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009438 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009439 }
9440#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009441
9442 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009445 }
9446
Hanno Beckera5a2b082019-05-15 14:03:01 +01009447#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009448 if( transform->out_cid_len != 0 )
9449 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009450#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009451
Hanno Becker43395762019-05-03 14:46:38 +01009452 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009453}
9454
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009455#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9456size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9457{
9458 size_t max_len;
9459
9460 /*
9461 * Assume mfl_code is correct since it was checked when set
9462 */
Angus Grattond8213d02016-05-25 20:56:48 +10009463 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009464
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009465 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009466 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009467 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009468 {
Angus Grattond8213d02016-05-25 20:56:48 +10009469 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009470 }
9471
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009472 /* During a handshake, use the value being negotiated */
9473 if( ssl->session_negotiate != NULL &&
9474 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9475 {
9476 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9477 }
9478
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009479 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009480}
9481#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9482
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009483#if defined(MBEDTLS_SSL_PROTO_DTLS)
9484static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9485{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009486 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009487 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009488 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9489 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009490 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009491
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009492 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9493 return( ssl->mtu );
9494
9495 if( ssl->mtu == 0 )
9496 return( ssl->handshake->mtu );
9497
9498 return( ssl->mtu < ssl->handshake->mtu ?
9499 ssl->mtu : ssl->handshake->mtu );
9500}
9501#endif /* MBEDTLS_SSL_PROTO_DTLS */
9502
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009503int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9504{
9505 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9506
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009507#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9508 !defined(MBEDTLS_SSL_PROTO_DTLS)
9509 (void) ssl;
9510#endif
9511
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009512#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9513 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9514
9515 if( max_len > mfl )
9516 max_len = mfl;
9517#endif
9518
9519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009520 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009521 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009522 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009523 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9524 const size_t overhead = (size_t) ret;
9525
9526 if( ret < 0 )
9527 return( ret );
9528
9529 if( mtu <= overhead )
9530 {
9531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9532 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9533 }
9534
9535 if( max_len > mtu - overhead )
9536 max_len = mtu - overhead;
9537 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009538#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009539
Hanno Becker0defedb2018-08-10 12:35:02 +01009540#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9541 !defined(MBEDTLS_SSL_PROTO_DTLS)
9542 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009543#endif
9544
9545 return( (int) max_len );
9546}
9547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548#if defined(MBEDTLS_X509_CRT_PARSE_C)
9549const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009550{
9551 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009552 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009553
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009554#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009555 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009556#else
9557 return( NULL );
9558#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009563int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9564 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009565{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009566 if( ssl == NULL ||
9567 dst == NULL ||
9568 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009569 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009572 }
9573
Hanno Becker58fccf22019-02-06 14:30:46 +00009574 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009577
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009578const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9579{
9580 if( ssl == NULL )
9581 return( NULL );
9582
9583 return( ssl->session );
9584}
9585
Paul Bakker5121ce52009-01-03 21:22:43 +00009586/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009587 * Define ticket header determining Mbed TLS version
9588 * and structure of the ticket.
9589 */
9590
Hanno Becker41527622019-05-16 12:50:45 +01009591/*
Hanno Becker26829e92019-05-28 14:30:45 +01009592 * Define bitflag determining compile-time settings influencing
9593 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009594 */
9595
Hanno Becker26829e92019-05-28 14:30:45 +01009596#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009597#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009598#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009599#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009600#endif /* MBEDTLS_HAVE_TIME */
9601
9602#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009603#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009604#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009605#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009606#endif /* MBEDTLS_X509_CRT_PARSE_C */
9607
9608#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009609#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009610#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009611#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009612#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9613
9614#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009615#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009616#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009617#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009618#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9619
9620#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009621#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009622#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009623#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009624#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9625
9626#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009627#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009628#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009629#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009630#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9631
Hanno Becker41527622019-05-16 12:50:45 +01009632#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9633#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9634#else
9635#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9636#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9637
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009638#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9639#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9640#else
9641#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9642#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9643
Hanno Becker88440552019-07-03 14:16:13 +01009644#if defined(MBEDTLS_ZLIB_SUPPORT)
9645#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9646#else
9647#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9648#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9649
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009650#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9651#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9652#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9653#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9654#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9655#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9656#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009657#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009658#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009659
Hanno Becker26829e92019-05-28 14:30:45 +01009660#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009661 ( (uint16_t) ( \
9662 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9663 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9664 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9665 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9666 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9667 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009668 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009669 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009670 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009671
Hanno Becker557fe9f2019-05-16 12:41:07 +01009672static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009673 MBEDTLS_VERSION_MAJOR,
9674 MBEDTLS_VERSION_MINOR,
9675 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009676 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9677 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009678};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009679
9680/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009681 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009682 * (in the presentation language of TLS, RFC 8446 section 3)
9683 *
Hanno Becker26829e92019-05-28 14:30:45 +01009684 * opaque mbedtls_version[3]; // major, minor, patch
9685 * opaque session_format[2]; // version-specific 16-bit field determining
9686 * // the format of the remaining
9687 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009688 *
9689 * Note: When updating the format, remember to keep
9690 * these version+format bytes.
9691 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009692 * // In this version, `session_format` determines
9693 * // the setting of those compile-time
9694 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009695 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009696 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009697 * uint8 ciphersuite[2]; // defined by the standard
9698 * uint8 compression; // 0 or 1
9699 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009700 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009701 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009702 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009703 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9704 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9705 * case disabled: uint8_t peer_cert_digest_type;
9706 * opaque peer_cert_digest<0..2^8-1>;
9707 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009708 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009709 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009710 * uint8 mfl_code; // up to 255 according to standard
9711 * uint8 trunc_hmac; // 0 or 1
9712 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009713 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009714 * The order is the same as in the definition of the structure, except
9715 * verify_result is put before peer_cert so that all mandatory fields come
9716 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009717 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009718static int ssl_session_save( const mbedtls_ssl_session *session,
9719 unsigned char omit_header,
9720 unsigned char *buf,
9721 size_t buf_len,
9722 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009723{
9724 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009725 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009726#if defined(MBEDTLS_HAVE_TIME)
9727 uint64_t start;
9728#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009729#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009730#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009731 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009732#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009733#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009734
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009735 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009736 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009737 /*
9738 * Add version identifier
9739 */
9740
9741 used += sizeof( ssl_serialized_session_header );
9742
9743 if( used <= buf_len )
9744 {
9745 memcpy( p, ssl_serialized_session_header,
9746 sizeof( ssl_serialized_session_header ) );
9747 p += sizeof( ssl_serialized_session_header );
9748 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009749 }
9750
9751 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009752 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009753 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009754#if defined(MBEDTLS_HAVE_TIME)
9755 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009756
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009757 if( used <= buf_len )
9758 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009759 start = (uint64_t) session->start;
9760
9761 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9762 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9763 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9764 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9765 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9766 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9767 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9768 *p++ = (unsigned char)( ( start ) & 0xFF );
9769 }
9770#endif /* MBEDTLS_HAVE_TIME */
9771
9772 /*
9773 * Basic mandatory fields
9774 */
Hanno Becker88440552019-07-03 14:16:13 +01009775 {
9776 size_t const ciphersuite_len = 2;
9777#if defined(MBEDTLS_ZLIB_SUPPORT)
9778 size_t const compression_len = 1;
9779#else
9780 size_t const compression_len = 0;
9781#endif
9782 size_t const id_len_len = 1;
9783 size_t const id_len = 32;
9784 size_t const master_len = 48;
9785 size_t const verif_result_len = 4;
9786
9787 size_t const basic_len =
9788 ciphersuite_len +
9789 compression_len +
9790 id_len_len +
9791 id_len +
9792 master_len +
9793 verif_result_len;
9794
9795 used += basic_len;
9796 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009797
9798 if( used <= buf_len )
9799 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009800 const int ciphersuite =
9801 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009802 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009803
Hanno Becker88440552019-07-03 14:16:13 +01009804#if defined(MBEDTLS_ZLIB_SUPPORT)
9805 *p++ = (unsigned char)(
9806 mbedtls_ssl_session_get_compression( session ) );
9807#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009808
9809 *p++ = (unsigned char)( session->id_len & 0xFF );
9810 memcpy( p, session->id, 32 );
9811 p += 32;
9812
9813 memcpy( p, session->master, 48 );
9814 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009815 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009816 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009817
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009818 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009819 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009820 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009821#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009822#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009823 if( session->peer_cert == NULL )
9824 cert_len = 0;
9825 else
9826 cert_len = session->peer_cert->raw.len;
9827
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009828 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009829
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009830 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009831 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009832 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009833
9834 if( session->peer_cert != NULL )
9835 {
9836 memcpy( p, session->peer_cert->raw.p, cert_len );
9837 p += cert_len;
9838 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009839 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009840
Hanno Becker5882dd02019-06-06 16:25:57 +01009841#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009842 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009843 if( session->peer_cert_digest != NULL )
9844 {
9845 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9846 if( used <= buf_len )
9847 {
9848 *p++ = (unsigned char) session->peer_cert_digest_type;
9849 *p++ = (unsigned char) session->peer_cert_digest_len;
9850 memcpy( p, session->peer_cert_digest,
9851 session->peer_cert_digest_len );
9852 p += session->peer_cert_digest_len;
9853 }
9854 }
9855 else
9856 {
9857 used += 2;
9858 if( used <= buf_len )
9859 {
9860 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9861 *p++ = 0;
9862 }
9863 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009864#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009865#endif /* MBEDTLS_X509_CRT_PARSE_C */
9866
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009867 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009868 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009869 */
9870#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009871 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009872
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009873 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009874 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009875 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009876
9877 if( session->ticket != NULL )
9878 {
9879 memcpy( p, session->ticket, session->ticket_len );
9880 p += session->ticket_len;
9881 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009882
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03009883 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009884 }
9885#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9886
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009887 /*
9888 * Misc extension-related info
9889 */
9890#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9891 used += 1;
9892
9893 if( used <= buf_len )
9894 *p++ = session->mfl_code;
9895#endif
9896
9897#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9898 used += 1;
9899
9900 if( used <= buf_len )
9901 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9902#endif
9903
9904#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9905 used += 1;
9906
9907 if( used <= buf_len )
9908 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9909#endif
9910
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009911 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009912 *olen = used;
9913
9914 if( used > buf_len )
9915 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009916
9917 return( 0 );
9918}
9919
9920/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009921 * Public wrapper for ssl_session_save()
9922 */
9923int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9924 unsigned char *buf,
9925 size_t buf_len,
9926 size_t *olen )
9927{
9928 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9929}
9930
9931/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009932 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009933 *
9934 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009935 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009936 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009937static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009938 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009939 const unsigned char *buf,
9940 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009941{
9942 const unsigned char *p = buf;
9943 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009944 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009945#if defined(MBEDTLS_HAVE_TIME)
9946 uint64_t start;
9947#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009948#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009949#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009950 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009951#endif
9952#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009953
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009954 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009955 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009956 /*
9957 * Check version identifier
9958 */
9959
9960 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9962
9963 if( memcmp( p, ssl_serialized_session_header,
9964 sizeof( ssl_serialized_session_header ) ) != 0 )
9965 {
9966 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9967 }
9968 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009969 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009970
9971 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009972 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009973 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009974#if defined(MBEDTLS_HAVE_TIME)
9975 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9977
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009978 start = ( (uint64_t) p[0] << 56 ) |
9979 ( (uint64_t) p[1] << 48 ) |
9980 ( (uint64_t) p[2] << 40 ) |
9981 ( (uint64_t) p[3] << 32 ) |
9982 ( (uint64_t) p[4] << 24 ) |
9983 ( (uint64_t) p[5] << 16 ) |
9984 ( (uint64_t) p[6] << 8 ) |
9985 ( (uint64_t) p[7] );
9986 p += 8;
9987
9988 session->start = (time_t) start;
9989#endif /* MBEDTLS_HAVE_TIME */
9990
9991 /*
9992 * Basic mandatory fields
9993 */
Hanno Becker88440552019-07-03 14:16:13 +01009994 {
9995 size_t const ciphersuite_len = 2;
9996#if defined(MBEDTLS_ZLIB_SUPPORT)
9997 size_t const compression_len = 1;
9998#else
9999 size_t const compression_len = 0;
10000#endif
10001 size_t const id_len_len = 1;
10002 size_t const id_len = 32;
10003 size_t const master_len = 48;
10004 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010005
Hanno Becker88440552019-07-03 14:16:13 +010010006 size_t const basic_len =
10007 ciphersuite_len +
10008 compression_len +
10009 id_len_len +
10010 id_len +
10011 master_len +
10012 verif_result_len;
10013
10014 if( basic_len > (size_t)( end - p ) )
10015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10016 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010017
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010018 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010019 p += 2;
10020
Hanno Becker73f4cb12019-06-27 13:51:07 +010010021#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010022 session->ciphersuite = ciphersuite;
10023#else
10024 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010025 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010026 {
10027 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10028 }
10029#endif
10030
Hanno Becker88440552019-07-03 14:16:13 +010010031#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010032 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010033#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010034
10035 session->id_len = *p++;
10036 memcpy( session->id, p, 32 );
10037 p += 32;
10038
10039 memcpy( session->master, p, 48 );
10040 p += 48;
10041
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010042 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010043 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010044
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010045 /* Immediately clear invalid pointer values that have been read, in case
10046 * we exit early before we replaced them with valid ones. */
10047#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010048#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010049 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010050#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010051 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010052#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010053#endif
10054#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10055 session->ticket = NULL;
10056#endif
10057
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010058 /*
10059 * Peer certificate
10060 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010061#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010062#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010063 if( 3 > (size_t)( end - p ) )
10064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10065
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010066 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10067
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010068 p += 3;
10069
10070 if( cert_len == 0 )
10071 {
10072 session->peer_cert = NULL;
10073 }
10074 else
10075 {
10076 int ret;
10077
10078 if( cert_len > (size_t)( end - p ) )
10079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10080
10081 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10082
10083 if( session->peer_cert == NULL )
10084 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10085
10086 mbedtls_x509_crt_init( session->peer_cert );
10087
10088 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10089 p, cert_len ) ) != 0 )
10090 {
10091 mbedtls_x509_crt_free( session->peer_cert );
10092 mbedtls_free( session->peer_cert );
10093 session->peer_cert = NULL;
10094 return( ret );
10095 }
10096
10097 p += cert_len;
10098 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010099#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010100 /* Deserialize CRT digest from the end of the ticket. */
10101 if( 2 > (size_t)( end - p ) )
10102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10103
10104 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10105 session->peer_cert_digest_len = (size_t) *p++;
10106
10107 if( session->peer_cert_digest_len != 0 )
10108 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010109 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010110 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010111 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10113 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10115
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010116 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10118
10119 session->peer_cert_digest =
10120 mbedtls_calloc( 1, session->peer_cert_digest_len );
10121 if( session->peer_cert_digest == NULL )
10122 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10123
10124 memcpy( session->peer_cert_digest, p,
10125 session->peer_cert_digest_len );
10126 p += session->peer_cert_digest_len;
10127 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010128#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010129#endif /* MBEDTLS_X509_CRT_PARSE_C */
10130
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010131 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010132 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010133 */
10134#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10135 if( 3 > (size_t)( end - p ) )
10136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10137
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010138 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010139 p += 3;
10140
10141 if( session->ticket_len != 0 )
10142 {
10143 if( session->ticket_len > (size_t)( end - p ) )
10144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10145
10146 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10147 if( session->ticket == NULL )
10148 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10149
10150 memcpy( session->ticket, p, session->ticket_len );
10151 p += session->ticket_len;
10152 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010153
10154 if( 4 > (size_t)( end - p ) )
10155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10156
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010157 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010158 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010159#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10160
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010161 /*
10162 * Misc extension-related info
10163 */
10164#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10165 if( 1 > (size_t)( end - p ) )
10166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10167
10168 session->mfl_code = *p++;
10169#endif
10170
10171#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10172 if( 1 > (size_t)( end - p ) )
10173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10174
10175 session->trunc_hmac = *p++;
10176#endif
10177
10178#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10179 if( 1 > (size_t)( end - p ) )
10180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10181
10182 session->encrypt_then_mac = *p++;
10183#endif
10184
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010185 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010186 if( p != end )
10187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10188
10189 return( 0 );
10190}
10191
10192/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010193 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010194 */
10195int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10196 const unsigned char *buf,
10197 size_t len )
10198{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010199 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010200
10201 if( ret != 0 )
10202 mbedtls_ssl_session_free( session );
10203
10204 return( ret );
10205}
10206
10207/*
Paul Bakker1961b702013-01-25 14:49:24 +010010208 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010213
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010214 if( ssl == NULL || ssl->conf == NULL )
10215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010218 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010222 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010224#endif
10225
Hanno Beckerb82350b2019-07-26 07:24:05 +010010226 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010227 return( ret );
10228}
10229
10230/*
10231 * Perform the SSL handshake
10232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010234{
10235 int ret = 0;
10236
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010237 if( ssl == NULL || ssl->conf == NULL )
10238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010245
10246 if( ret != 0 )
10247 break;
10248 }
10249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010251
10252 return( ret );
10253}
10254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255#if defined(MBEDTLS_SSL_RENEGOTIATION)
10256#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010257/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010258 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010260static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010261{
10262 int ret;
10263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010265
10266 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10268 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010269
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010270 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010271 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010273 return( ret );
10274 }
10275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010277
10278 return( 0 );
10279}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010281
10282/*
10283 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284 * - any side: calling mbedtls_ssl_renegotiate(),
10285 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10286 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010287 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010288 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010292{
10293 int ret;
10294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010296
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010297 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10298 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010299
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010300 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10301 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010303 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010305 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010306 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10307 MBEDTLS_SSL_IS_SERVER )
10308 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010309 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010310 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010311 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010312 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010313 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010314 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010315 }
10316#endif
10317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10319 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010324 return( ret );
10325 }
10326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010328
10329 return( 0 );
10330}
10331
10332/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010333 * Renegotiate current connection on client,
10334 * or request renegotiation on server
10335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010337{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010339
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010340 if( ssl == NULL || ssl->conf == NULL )
10341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010344 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010345 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010350 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010351
10352 /* Did we already try/start sending HelloRequest? */
10353 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010355
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010356 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010357 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010361 /*
10362 * On client, either start the renegotiation process or,
10363 * if already in progress, continue the handshake
10364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010369
10370 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010373 return( ret );
10374 }
10375 }
10376 else
10377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010381 return( ret );
10382 }
10383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010384#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010385
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010386 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010387}
10388
10389/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010390 * Check record counters and renegotiate if they're above the limit.
10391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010393{
Andres AG2196c7f2016-12-15 17:01:16 +000010394 size_t ep_len = ssl_ep_len( ssl );
10395 int in_ctr_cmp;
10396 int out_ctr_cmp;
10397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10399 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010400 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010401 {
10402 return( 0 );
10403 }
10404
Andres AG2196c7f2016-12-15 17:01:16 +000010405 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10406 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010407 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010408 ssl->conf->renego_period + ep_len, 8 - ep_len );
10409
10410 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010411 {
10412 return( 0 );
10413 }
10414
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010417}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010418#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010419
10420/*
10421 * Receive application data decrypted from the SSL layer
10422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010424{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010425 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010426 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010427
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010428 if( ssl == NULL || ssl->conf == NULL )
10429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010434 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010437 return( ret );
10438
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010439 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010441 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010442 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010443 return( ret );
10444 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010445 }
10446#endif
10447
Hanno Becker4a810fb2017-05-24 16:27:30 +010010448 /*
10449 * Check if renegotiation is necessary and/or handshake is
10450 * in process. If yes, perform/continue, and fall through
10451 * if an unexpected packet is received while the client
10452 * is waiting for the ServerHello.
10453 *
10454 * (There is no equivalent to the last condition on
10455 * the server-side as it is not treated as within
10456 * a handshake while waiting for the ClientHello
10457 * after a renegotiation request.)
10458 */
10459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010461 ret = ssl_check_ctr_renegotiate( ssl );
10462 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10463 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010465 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010466 return( ret );
10467 }
10468#endif
10469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010470 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010473 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10474 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010477 return( ret );
10478 }
10479 }
10480
Hanno Beckere41158b2017-10-23 13:30:32 +010010481 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010482 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010483 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010484 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010485 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10486 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010487 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010488 ssl_set_timer( ssl,
10489 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010490 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010491
Hanno Becker327c93b2018-08-15 13:56:18 +010010492 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010493 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010494 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10495 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010496
Hanno Becker4a810fb2017-05-24 16:27:30 +010010497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10498 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010499 }
10500
10501 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010503 {
10504 /*
10505 * OpenSSL sends empty messages to randomize the IV
10506 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010507 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010510 return( 0 );
10511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010513 return( ret );
10514 }
10515 }
10516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010517 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010520
Hanno Becker4a810fb2017-05-24 16:27:30 +010010521 /*
10522 * - For client-side, expect SERVER_HELLO_REQUEST.
10523 * - For server-side, expect CLIENT_HELLO.
10524 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10525 */
10526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010527#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010528 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10529 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010530 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010531 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010534
10535 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010537 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010538 {
10539 continue;
10540 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010541 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010542#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010543#if defined(MBEDTLS_SSL_PROTO_TLS)
10544 {
10545 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10546 }
10547#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010548 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010549#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010550
Hanno Becker4a810fb2017-05-24 16:27:30 +010010551#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010552 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10553 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010554 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010557
10558 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010559#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010560 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010561 {
10562 continue;
10563 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010564 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010565#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010566#if defined(MBEDTLS_SSL_PROTO_TLS)
10567 {
10568 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10569 }
10570#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010571 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010572#endif /* MBEDTLS_SSL_SRV_C */
10573
Hanno Becker21df7f92017-10-17 11:03:26 +010010574#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010575 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010576 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10577 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010578 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010579 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10580 {
10581 /*
10582 * Accept renegotiation request
10583 */
Paul Bakker48916f92012-09-16 19:57:18 +000010584
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010585 /* DTLS clients need to know renego is server-initiated */
10586#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010587 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010588 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10589 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010590 {
10591 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10592 }
10593#endif
10594 ret = ssl_start_renegotiation( ssl );
10595 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10596 ret != 0 )
10597 {
10598 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10599 return( ret );
10600 }
10601 }
10602 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010603#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010604 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010605 /*
10606 * Refuse renegotiation
10607 */
10608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010612 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010613 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010614 /* SSLv3 does not have a "no_renegotiation" warning, so
10615 we send a fatal alert and abort the connection. */
10616 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10617 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10618 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010619 }
10620 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10622#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10623 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010010624 if( mbedtls_ssl_ver_geq(
10625 mbedtls_ssl_get_minor_ver( ssl ),
10626 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010627 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010628 ret = mbedtls_ssl_send_alert_message( ssl,
10629 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10630 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10631 if( ret != 0 )
10632 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010633 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010634 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10636 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010640 }
Paul Bakker48916f92012-09-16 19:57:18 +000010641 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010642
Hanno Becker90333da2017-10-10 11:27:13 +010010643 /* At this point, we don't know whether the renegotiation has been
10644 * completed or not. The cases to consider are the following:
10645 * 1) The renegotiation is complete. In this case, no new record
10646 * has been read yet.
10647 * 2) The renegotiation is incomplete because the client received
10648 * an application data record while awaiting the ServerHello.
10649 * 3) The renegotiation is incomplete because the client received
10650 * a non-handshake, non-application data message while awaiting
10651 * the ServerHello.
10652 * In each of these case, looping will be the proper action:
10653 * - For 1), the next iteration will read a new record and check
10654 * if it's application data.
10655 * - For 2), the loop condition isn't satisfied as application data
10656 * is present, hence continue is the same as break
10657 * - For 3), the loop condition is satisfied and read_record
10658 * will re-deliver the message that was held back by the client
10659 * when expecting the ServerHello.
10660 */
10661 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010662 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010663#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010665 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010666 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010667 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010668 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010671 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010672 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010673 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010674 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010675 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010676#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10679 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010682 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010683 }
10684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010685 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10688 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010689 }
10690
10691 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010692
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010693 /* We're going to return something now, cancel timer,
10694 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010696 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010697
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010698#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010699 /* If we requested renego but received AppData, resend HelloRequest.
10700 * Do it now, after setting in_offt, to avoid taking this branch
10701 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010703 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10704 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010706 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010707 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010710 return( ret );
10711 }
10712 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010714#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010715 }
10716
10717 n = ( len < ssl->in_msglen )
10718 ? len : ssl->in_msglen;
10719
10720 memcpy( buf, ssl->in_offt, n );
10721 ssl->in_msglen -= n;
10722
10723 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010724 {
10725 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010726 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010727 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010728 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010729 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010730 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010731 /* more data available */
10732 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010733 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010736
Paul Bakker23986e52011-04-24 08:57:21 +000010737 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010738}
10739
10740/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010741 * Send application data to be encrypted by the SSL layer, taking care of max
10742 * fragment length and buffer size.
10743 *
10744 * According to RFC 5246 Section 6.2.1:
10745 *
10746 * Zero-length fragments of Application data MAY be sent as they are
10747 * potentially useful as a traffic analysis countermeasure.
10748 *
10749 * Therefore, it is possible that the input message length is 0 and the
10750 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010751 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010752static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010753 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010754{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010755 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10756 const size_t max_len = (size_t) ret;
10757
10758 if( ret < 0 )
10759 {
10760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10761 return( ret );
10762 }
10763
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010764 if( len > max_len )
10765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010767 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010770 "maximum fragment length: %d > %d",
10771 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010773 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010774 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010775#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010776#if defined(MBEDTLS_SSL_PROTO_TLS)
10777 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010778 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010779 }
10780#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010781 }
Paul Bakker887bd502011-06-08 13:10:54 +000010782
Paul Bakker5121ce52009-01-03 21:22:43 +000010783 if( ssl->out_left != 0 )
10784 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010785 /*
10786 * The user has previously tried to send the data and
10787 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10788 * written. In this case, we expect the high-level write function
10789 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010794 return( ret );
10795 }
10796 }
Paul Bakker887bd502011-06-08 13:10:54 +000010797 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010798 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010799 /*
10800 * The user is trying to send a message the first time, so we need to
10801 * copy the data into the internal buffers and setup the data structure
10802 * to keep track of partial writes
10803 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010804 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010805 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010806 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010807
Hanno Becker67bc7c32018-08-06 11:33:50 +010010808 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010811 return( ret );
10812 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010813 }
10814
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010815 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010816}
10817
10818/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010819 * Write application data, doing 1/n-1 splitting if necessary.
10820 *
10821 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010822 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010823 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010825#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010826static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010827 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010828{
10829 int ret;
10830
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010831 if( ssl->conf->cbc_record_splitting ==
10832 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010833 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010010834 mbedtls_ssl_ver_gt(
10835 mbedtls_ssl_get_minor_ver( ssl ),
10836 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010837 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10838 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010839 {
10840 return( ssl_write_real( ssl, buf, len ) );
10841 }
10842
10843 if( ssl->split_done == 0 )
10844 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010845 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010846 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010847 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010848 }
10849
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010850 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10851 return( ret );
10852 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010853
10854 return( ret + 1 );
10855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010857
10858/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010859 * Write application data (public-facing wrapper)
10860 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010861int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010862{
10863 int ret;
10864
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010866
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010867 if( ssl == NULL || ssl->conf == NULL )
10868 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10869
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010870#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010871 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10872 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010873 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010874 return( ret );
10875 }
10876#endif
10877
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010878 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010879 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010880 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010881 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010883 return( ret );
10884 }
10885 }
10886
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010887#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010888 ret = ssl_write_split( ssl, buf, len );
10889#else
10890 ret = ssl_write_real( ssl, buf, len );
10891#endif
10892
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010894
10895 return( ret );
10896}
10897
10898/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010899 * Notify the peer that the connection is being closed
10900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010902{
10903 int ret;
10904
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010905 if( ssl == NULL || ssl->conf == NULL )
10906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010909
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010910 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10916 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10917 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010920 return( ret );
10921 }
10922 }
10923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010925
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010926 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010927}
10928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010930{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010931 if( transform == NULL )
10932 return;
10933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010934#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010935 deflateEnd( &transform->ctx_deflate );
10936 inflateEnd( &transform->ctx_inflate );
10937#endif
10938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10940 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010941
Hanno Becker92231322018-01-03 15:32:51 +000010942#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010943 mbedtls_md_free( &transform->md_ctx_enc );
10944 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010945#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010946
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010947 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010948}
10949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950#if defined(MBEDTLS_X509_CRT_PARSE_C)
10951static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010952{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010954
10955 while( cur != NULL )
10956 {
10957 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010959 cur = next;
10960 }
10961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010962#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010963
Hanno Becker0271f962018-08-16 13:23:47 +010010964#if defined(MBEDTLS_SSL_PROTO_DTLS)
10965
10966static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10967{
10968 unsigned offset;
10969 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10970
10971 if( hs == NULL )
10972 return;
10973
Hanno Becker283f5ef2018-08-24 09:34:47 +010010974 ssl_free_buffered_record( ssl );
10975
Hanno Becker0271f962018-08-16 13:23:47 +010010976 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010977 ssl_buffering_free_slot( ssl, offset );
10978}
10979
10980static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10981 uint8_t slot )
10982{
10983 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10984 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010985
10986 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10987 return;
10988
Hanno Beckere605b192018-08-21 15:59:07 +010010989 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010990 {
Hanno Beckere605b192018-08-21 15:59:07 +010010991 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010992 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010993 mbedtls_free( hs_buf->data );
10994 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010995 }
10996}
10997
10998#endif /* MBEDTLS_SSL_PROTO_DTLS */
10999
Gilles Peskine9b562d52018-04-25 20:32:43 +020011000void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011001{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011002 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11003
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011004 if( handshake == NULL )
11005 return;
11006
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011007#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11008 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11009 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011010 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011011 handshake->async_in_progress = 0;
11012 }
11013#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11014
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011015#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11016 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11017 mbedtls_md5_free( &handshake->fin_md5 );
11018 mbedtls_sha1_free( &handshake->fin_sha1 );
11019#endif
11020#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11021#if defined(MBEDTLS_SHA256_C)
11022 mbedtls_sha256_free( &handshake->fin_sha256 );
11023#endif
11024#if defined(MBEDTLS_SHA512_C)
11025 mbedtls_sha512_free( &handshake->fin_sha512 );
11026#endif
11027#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029#if defined(MBEDTLS_DHM_C)
11030 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032#if defined(MBEDTLS_ECDH_C)
11033 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011034#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011035#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011036 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011037#if defined(MBEDTLS_SSL_CLI_C)
11038 mbedtls_free( handshake->ecjpake_cache );
11039 handshake->ecjpake_cache = NULL;
11040 handshake->ecjpake_cache_len = 0;
11041#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011042#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011043
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011044#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11045 if( handshake->psk != NULL )
11046 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011047 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011048 mbedtls_free( handshake->psk );
11049 }
11050#endif
11051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011052#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11053 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011054 /*
11055 * Free only the linked list wrapper, not the keys themselves
11056 * since the belong to the SNI callback
11057 */
11058 if( handshake->sni_key_cert != NULL )
11059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011060 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011061
11062 while( cur != NULL )
11063 {
11064 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011065 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011066 cur = next;
11067 }
11068 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011069#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011070
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011071#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011072 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011073 if( handshake->ecrs_peer_cert != NULL )
11074 {
11075 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11076 mbedtls_free( handshake->ecrs_peer_cert );
11077 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011078#endif
11079
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011080#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11081 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11082 mbedtls_pk_free( &handshake->peer_pubkey );
11083#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011085#if defined(MBEDTLS_SSL_PROTO_DTLS)
11086 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011087 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011088 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011089#endif
11090
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011091 mbedtls_platform_zeroize( handshake,
11092 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011093}
11094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011095void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011096{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011097 if( session == NULL )
11098 return;
11099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011100#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011101 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011102#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011103
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011104#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011105 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011106#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011107
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011108 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011109}
11110
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011111#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011112
11113#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11114#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11115#else
11116#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11117#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11118
11119#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11120#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11121#else
11122#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11123#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11124
11125#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11126#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11127#else
11128#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11129#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11130
11131#if defined(MBEDTLS_SSL_ALPN)
11132#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11133#else
11134#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11135#endif /* MBEDTLS_SSL_ALPN */
11136
11137#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11138#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11139#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11140#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11141
11142#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11143 ( (uint32_t) ( \
11144 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11145 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11146 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11147 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11148 0u ) )
11149
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011150static unsigned char ssl_serialized_context_header[] = {
11151 MBEDTLS_VERSION_MAJOR,
11152 MBEDTLS_VERSION_MINOR,
11153 MBEDTLS_VERSION_PATCH,
11154 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11155 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011156 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11157 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11158 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011159};
11160
Paul Bakker5121ce52009-01-03 21:22:43 +000011161/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011162 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011163 *
11164 * The format of the serialized data is:
11165 * (in the presentation language of TLS, RFC 8446 section 3)
11166 *
11167 * // header
11168 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011169 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011170 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011171 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011172 * Note: When updating the format, remember to keep these
11173 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011174 *
11175 * // session sub-structure
11176 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11177 * // transform sub-structure
11178 * uint8 random[64]; // ServerHello.random+ClientHello.random
11179 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11180 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11181 * // fields from ssl_context
11182 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11183 * uint64 in_window_top; // DTLS: last validated record seq_num
11184 * uint64 in_window; // DTLS: bitmask for replay protection
11185 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11186 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11187 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11188 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11189 *
11190 * Note that many fields of the ssl_context or sub-structures are not
11191 * serialized, as they fall in one of the following categories:
11192 *
11193 * 1. forced value (eg in_left must be 0)
11194 * 2. pointer to dynamically-allocated memory (eg session, transform)
11195 * 3. value can be re-derived from other data (eg session keys from MS)
11196 * 4. value was temporary (eg content of input buffer)
11197 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011198 */
11199int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11200 unsigned char *buf,
11201 size_t buf_len,
11202 size_t *olen )
11203{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011204 unsigned char *p = buf;
11205 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011206 size_t session_len;
11207 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011208
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011209 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011210 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11211 * this function's documentation.
11212 *
11213 * These are due to assumptions/limitations in the implementation. Some of
11214 * them are likely to stay (no handshake in progress) some might go away
11215 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011216 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011217 /* The initial handshake must be over */
11218 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011220 if( ssl->handshake != NULL )
11221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11222 /* Double-check that sub-structures are indeed ready */
11223 if( ssl->transform == NULL || ssl->session == NULL )
11224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11225 /* There must be no pending incoming or outgoing data */
11226 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11228 if( ssl->out_left != 0 )
11229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11230 /* Protocol must be DLTS, not TLS */
11231 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11233 /* Version must be 1.2 */
11234 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11236 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11237 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11238 /* We must be using an AEAD ciphersuite */
11239 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11241 /* Renegotiation must not be enabled */
11242 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011244
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011245 /*
11246 * Version and format identifier
11247 */
11248 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011249
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011250 if( used <= buf_len )
11251 {
11252 memcpy( p, ssl_serialized_context_header,
11253 sizeof( ssl_serialized_context_header ) );
11254 p += sizeof( ssl_serialized_context_header );
11255 }
11256
11257 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011258 * Session (length + data)
11259 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011260 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011261 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11262 return( ret );
11263
11264 used += 4 + session_len;
11265 if( used <= buf_len )
11266 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011267 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011268
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011269 ret = ssl_session_save( ssl->session, 1,
11270 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011271 if( ret != 0 )
11272 return( ret );
11273
11274 p += session_len;
11275 }
11276
11277 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011278 * Transform
11279 */
11280 used += sizeof( ssl->transform->randbytes );
11281 if( used <= buf_len )
11282 {
11283 memcpy( p, ssl->transform->randbytes,
11284 sizeof( ssl->transform->randbytes ) );
11285 p += sizeof( ssl->transform->randbytes );
11286 }
11287
11288#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11289 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11290 if( used <= buf_len )
11291 {
11292 *p++ = ssl->transform->in_cid_len;
11293 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11294 p += ssl->transform->in_cid_len;
11295
11296 *p++ = ssl->transform->out_cid_len;
11297 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11298 p += ssl->transform->out_cid_len;
11299 }
11300#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11301
11302 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011303 * Saved fields from top-level ssl_context structure
11304 */
11305#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11306 used += 4;
11307 if( used <= buf_len )
11308 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011309 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011310 }
11311#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11312
11313#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11314 used += 16;
11315 if( used <= buf_len )
11316 {
11317 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11318 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11319 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11320 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11321 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11322 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11323 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11324 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11325
11326 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11327 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11328 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11329 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11330 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11331 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11332 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11333 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11334 }
11335#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11336
11337#if defined(MBEDTLS_SSL_PROTO_DTLS)
11338 used += 1;
11339 if( used <= buf_len )
11340 {
11341 *p++ = ssl->disable_datagram_packing;
11342 }
11343#endif /* MBEDTLS_SSL_PROTO_DTLS */
11344
11345 used += 8;
11346 if( used <= buf_len )
11347 {
11348 memcpy( p, ssl->cur_out_ctr, 8 );
11349 p += 8;
11350 }
11351
11352#if defined(MBEDTLS_SSL_PROTO_DTLS)
11353 used += 2;
11354 if( used <= buf_len )
11355 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011356 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011357 }
11358#endif /* MBEDTLS_SSL_PROTO_DTLS */
11359
11360#if defined(MBEDTLS_SSL_ALPN)
11361 {
11362 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011363 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011364 : 0;
11365
11366 used += 1 + alpn_len;
11367 if( used <= buf_len )
11368 {
11369 *p++ = alpn_len;
11370
11371 if( ssl->alpn_chosen != NULL )
11372 {
11373 memcpy( p, ssl->alpn_chosen, alpn_len );
11374 p += alpn_len;
11375 }
11376 }
11377 }
11378#endif /* MBEDTLS_SSL_ALPN */
11379
11380 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011381 * Done
11382 */
11383 *olen = used;
11384
11385 if( used > buf_len )
11386 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011387
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011388 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11389
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011390 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011391}
11392
11393/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011394 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011395 *
11396 * This internal version is wrapped by a public function that cleans up in
11397 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011398 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011399static int ssl_context_load( mbedtls_ssl_context *ssl,
11400 const unsigned char *buf,
11401 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011402{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011403 const unsigned char *p = buf;
11404 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011405 size_t session_len;
11406 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011407
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011408 /*
11409 * The context should have been freshly setup or reset.
11410 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011411 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011412 * renegotiating, or if the user mistakenly loaded a session first.)
11413 */
11414 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11415 ssl->session != NULL )
11416 {
11417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11418 }
11419
11420 /*
11421 * We can't check that the config matches the initial one, but we can at
11422 * least check it matches the requirements for serializing.
11423 */
11424 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011425 mbedtls_ssl_ver_lt(
11426 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11427 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11428 mbedtls_ssl_ver_gt(
11429 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11430 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11431 mbedtls_ssl_ver_lt(
11432 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11433 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11434 mbedtls_ssl_ver_gt(
11435 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11436 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011437 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011438 {
11439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11440 }
11441
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011442 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11443
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011444 /*
11445 * Check version identifier
11446 */
11447 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11449
11450 if( memcmp( p, ssl_serialized_context_header,
11451 sizeof( ssl_serialized_context_header ) ) != 0 )
11452 {
11453 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11454 }
11455 p += sizeof( ssl_serialized_context_header );
11456
11457 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011458 * Session
11459 */
11460 if( (size_t)( end - p ) < 4 )
11461 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11462
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011463 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011464 p += 4;
11465
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011466 /* This has been allocated by ssl_handshake_init(), called by
11467 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11468 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011469 ssl->session_in = ssl->session;
11470 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011471 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011472
11473 if( (size_t)( end - p ) < session_len )
11474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11475
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011476 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011477 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011478 {
11479 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011480 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011481 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011482
11483 p += session_len;
11484
11485 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011486 * Transform
11487 */
11488
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011489 /* This has been allocated by ssl_handshake_init(), called by
11490 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11491 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011492 ssl->transform_in = ssl->transform;
11493 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011494 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011495
11496 /* Read random bytes and populate structure */
11497 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11499
11500 ret = ssl_populate_transform( ssl->transform,
11501 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11502 ssl->session->master,
11503#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11504#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11505 ssl->session->encrypt_then_mac,
11506#endif
11507#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11508 ssl->session->trunc_hmac,
11509#endif
11510#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11511#if defined(MBEDTLS_ZLIB_SUPPORT)
11512 ssl->session->compression,
11513#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011514 p, /* currently pointing to randbytes */
11515 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11516 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11517 ssl );
11518 if( ret != 0 )
11519 return( ret );
11520
11521 p += sizeof( ssl->transform->randbytes );
11522
11523#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11524 /* Read connection IDs and store them */
11525 if( (size_t)( end - p ) < 1 )
11526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11527
11528 ssl->transform->in_cid_len = *p++;
11529
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011530 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11532
11533 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11534 p += ssl->transform->in_cid_len;
11535
11536 ssl->transform->out_cid_len = *p++;
11537
11538 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11540
11541 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11542 p += ssl->transform->out_cid_len;
11543#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11544
11545 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011546 * Saved fields from top-level ssl_context structure
11547 */
11548#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11549 if( (size_t)( end - p ) < 4 )
11550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11551
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011552 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011553 p += 4;
11554#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11555
11556#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11557 if( (size_t)( end - p ) < 16 )
11558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11559
11560 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11561 ( (uint64_t) p[1] << 48 ) |
11562 ( (uint64_t) p[2] << 40 ) |
11563 ( (uint64_t) p[3] << 32 ) |
11564 ( (uint64_t) p[4] << 24 ) |
11565 ( (uint64_t) p[5] << 16 ) |
11566 ( (uint64_t) p[6] << 8 ) |
11567 ( (uint64_t) p[7] );
11568 p += 8;
11569
11570 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11571 ( (uint64_t) p[1] << 48 ) |
11572 ( (uint64_t) p[2] << 40 ) |
11573 ( (uint64_t) p[3] << 32 ) |
11574 ( (uint64_t) p[4] << 24 ) |
11575 ( (uint64_t) p[5] << 16 ) |
11576 ( (uint64_t) p[6] << 8 ) |
11577 ( (uint64_t) p[7] );
11578 p += 8;
11579#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11580
11581#if defined(MBEDTLS_SSL_PROTO_DTLS)
11582 if( (size_t)( end - p ) < 1 )
11583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11584
11585 ssl->disable_datagram_packing = *p++;
11586#endif /* MBEDTLS_SSL_PROTO_DTLS */
11587
11588 if( (size_t)( end - p ) < 8 )
11589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11590
11591 memcpy( ssl->cur_out_ctr, p, 8 );
11592 p += 8;
11593
11594#if defined(MBEDTLS_SSL_PROTO_DTLS)
11595 if( (size_t)( end - p ) < 2 )
11596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011597 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011598 p += 2;
11599#endif /* MBEDTLS_SSL_PROTO_DTLS */
11600
11601#if defined(MBEDTLS_SSL_ALPN)
11602 {
11603 uint8_t alpn_len;
11604 const char **cur;
11605
11606 if( (size_t)( end - p ) < 1 )
11607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11608
11609 alpn_len = *p++;
11610
11611 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11612 {
11613 /* alpn_chosen should point to an item in the configured list */
11614 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11615 {
11616 if( strlen( *cur ) == alpn_len &&
11617 memcmp( p, cur, alpn_len ) == 0 )
11618 {
11619 ssl->alpn_chosen = *cur;
11620 break;
11621 }
11622 }
11623 }
11624
11625 /* can only happen on conf mismatch */
11626 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11628
11629 p += alpn_len;
11630 }
11631#endif /* MBEDTLS_SSL_ALPN */
11632
11633 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011634 * Forced fields from top-level ssl_context structure
11635 *
11636 * Most of them already set to the correct value by mbedtls_ssl_init() and
11637 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11638 */
11639 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11640
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011641#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011642 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011643#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11644#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011645 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011646#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011647
Hanno Becker83985822019-08-30 10:42:49 +010011648 /* Adjust pointers for header fields of outgoing records to
11649 * the given transform, accounting for explicit IV and CID. */
11650 ssl_update_out_pointers( ssl, ssl->transform );
11651
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011652#if defined(MBEDTLS_SSL_PROTO_DTLS)
11653 ssl->in_epoch = 1;
11654#endif
11655
11656 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11657 * which we don't want - otherwise we'd end up freeing the wrong transform
11658 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11659 if( ssl->handshake != NULL )
11660 {
11661 mbedtls_ssl_handshake_free( ssl );
11662 mbedtls_free( ssl->handshake );
11663 ssl->handshake = NULL;
11664 }
11665
11666 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011667 * Done - should have consumed entire buffer
11668 */
11669 if( p != end )
11670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011671
11672 return( 0 );
11673}
11674
11675/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011676 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011677 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011678int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011679 const unsigned char *buf,
11680 size_t len )
11681{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011682 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011683
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011684 if( ret != 0 )
11685 mbedtls_ssl_free( context );
11686
11687 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011688}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011689#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011690
11691/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011692 * Free an SSL context
11693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011694void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011695{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011696 if( ssl == NULL )
11697 return;
11698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011700
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011701 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011702 {
Angus Grattond8213d02016-05-25 20:56:48 +100011703 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011704 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011705 }
11706
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011707 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011708 {
Angus Grattond8213d02016-05-25 20:56:48 +100011709 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011710 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011711 }
11712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011713#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011714 if( ssl->compress_buf != NULL )
11715 {
Angus Grattond8213d02016-05-25 20:56:48 +100011716 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011717 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011718 }
11719#endif
11720
Paul Bakker48916f92012-09-16 19:57:18 +000011721 if( ssl->transform )
11722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011723 mbedtls_ssl_transform_free( ssl->transform );
11724 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011725 }
11726
11727 if( ssl->handshake )
11728 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011729 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011730 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11731 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011733 mbedtls_free( ssl->handshake );
11734 mbedtls_free( ssl->transform_negotiate );
11735 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011736 }
11737
Paul Bakkerc0463502013-02-14 11:19:38 +010011738 if( ssl->session )
11739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011740 mbedtls_ssl_session_free( ssl->session );
11741 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011742 }
11743
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011744#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011745 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011746 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011747 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011748 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011749 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011750#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011752#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11753 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11756 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011757 }
11758#endif
11759
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011760#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011761 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011762#endif
11763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011765
Paul Bakker86f04f42013-02-14 11:20:09 +010011766 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011767 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011768}
11769
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011770/*
11771 * Initialze mbedtls_ssl_config
11772 */
11773void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11774{
11775 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011776
11777#if !defined(MBEDTLS_SSL_PROTO_TLS)
11778 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11779#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011780}
11781
Simon Butcherc97b6972015-12-27 23:48:17 +000011782#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011783#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011784static int ssl_preset_default_hashes[] = {
11785#if defined(MBEDTLS_SHA512_C)
11786 MBEDTLS_MD_SHA512,
11787 MBEDTLS_MD_SHA384,
11788#endif
11789#if defined(MBEDTLS_SHA256_C)
11790 MBEDTLS_MD_SHA256,
11791 MBEDTLS_MD_SHA224,
11792#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011793#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011794 MBEDTLS_MD_SHA1,
11795#endif
11796 MBEDTLS_MD_NONE
11797};
Simon Butcherc97b6972015-12-27 23:48:17 +000011798#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011799#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011800
Hanno Becker73f4cb12019-06-27 13:51:07 +010011801#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011802static int ssl_preset_suiteb_ciphersuites[] = {
11803 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11804 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11805 0
11806};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011807#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011808
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011809#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011810#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011811static int ssl_preset_suiteb_hashes[] = {
11812 MBEDTLS_MD_SHA256,
11813 MBEDTLS_MD_SHA384,
11814 MBEDTLS_MD_NONE
11815};
11816#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011817#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011818
Hanno Beckerc1096e72019-06-19 12:30:41 +010011819#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011820static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011821#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011822 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011823#endif
11824#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011825 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011826#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011827 MBEDTLS_ECP_DP_NONE
11828};
11829#endif
11830
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011831/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011832 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011833 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011834int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011835 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011836{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011837#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011838 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011839#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011840
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011841 /* Use the functions here so that they are covered in tests,
11842 * but otherwise access member directly for efficiency */
11843 mbedtls_ssl_conf_endpoint( conf, endpoint );
11844 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011845
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011846 /*
11847 * Things that are common to all presets
11848 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011849#if defined(MBEDTLS_SSL_CLI_C)
11850 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11851 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011852#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011853 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011854#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011855#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11856 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11857#endif
11858 }
11859#endif
11860
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011861#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011862 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011863#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011864
11865#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11866 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11867#endif
11868
11869#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011870#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011871 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011872#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11873#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011874 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011875 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011876#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011877#endif
11878
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011879#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11880 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11881#endif
11882
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011883#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011884 conf->f_cookie_write = ssl_cookie_write_dummy;
11885 conf->f_cookie_check = ssl_cookie_check_dummy;
11886#endif
11887
Hanno Becker7f376f42019-06-12 16:20:48 +010011888#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11889 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011890 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11891#endif
11892
Janos Follath088ce432017-04-10 12:42:31 +010011893#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011894#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011895 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011896#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11897#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011898
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011899#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011900#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011901 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011902#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11903#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011904 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011905#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11906#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011907
11908#if defined(MBEDTLS_SSL_RENEGOTIATION)
11909 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011910 memset( conf->renego_period, 0x00, 2 );
11911 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011912#endif
11913
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011914#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11915 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11916 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011917 const unsigned char dhm_p[] =
11918 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11919 const unsigned char dhm_g[] =
11920 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11921
Hanno Beckera90658f2017-10-04 15:29:08 +010011922 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11923 dhm_p, sizeof( dhm_p ),
11924 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011925 {
11926 return( ret );
11927 }
11928 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011929#endif
11930
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011931 /*
11932 * Preset-specific defaults
11933 */
11934 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011935 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011936 /*
11937 * NSA Suite B
11938 */
11939 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011940#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011941 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011942#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11943#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011944 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011945#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11946#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011947 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011948#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11949#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011950 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011951#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011952
Hanno Becker73f4cb12019-06-27 13:51:07 +010011953#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011954 conf->ciphersuite_list[0] =
11955 conf->ciphersuite_list[1] =
11956 conf->ciphersuite_list[2] =
11957 conf->ciphersuite_list[3] =
11958 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011959#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011960
11961#if defined(MBEDTLS_X509_CRT_PARSE_C)
11962 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011963#endif
11964
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011965#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011966#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011967 conf->sig_hashes = ssl_preset_suiteb_hashes;
11968#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011969#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011970
11971#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011972#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011973 conf->curve_list = ssl_preset_suiteb_curves;
11974#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011975#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011976 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011977
11978 /*
11979 * Default
11980 */
11981 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011982#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011983 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11984 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11985 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11986 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011987#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11988#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011989 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11990 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11991 MBEDTLS_SSL_MIN_MINOR_VERSION :
11992 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011994 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011995 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11996#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011997#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11998#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11999 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12000#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12001#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12002 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12003#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012004
Hanno Becker73f4cb12019-06-27 13:51:07 +010012005#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012006 conf->ciphersuite_list[0] =
12007 conf->ciphersuite_list[1] =
12008 conf->ciphersuite_list[2] =
12009 conf->ciphersuite_list[3] =
12010 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012011#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012012
12013#if defined(MBEDTLS_X509_CRT_PARSE_C)
12014 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12015#endif
12016
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012017#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012018#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012019 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012020#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012021#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012022
12023#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012024#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012025 conf->curve_list = mbedtls_ecp_grp_id_list();
12026#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012027#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012028
12029#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12030 conf->dhm_min_bitlen = 1024;
12031#endif
12032 }
12033
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012034 return( 0 );
12035}
12036
12037/*
12038 * Free mbedtls_ssl_config
12039 */
12040void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12041{
12042#if defined(MBEDTLS_DHM_C)
12043 mbedtls_mpi_free( &conf->dhm_P );
12044 mbedtls_mpi_free( &conf->dhm_G );
12045#endif
12046
12047#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12048 if( conf->psk != NULL )
12049 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012050 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012051 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012052 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012053 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012054 }
12055
12056 if( conf->psk_identity != NULL )
12057 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012058 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012059 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012060 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012061 conf->psk_identity_len = 0;
12062 }
12063#endif
12064
12065#if defined(MBEDTLS_X509_CRT_PARSE_C)
12066 ssl_key_cert_free( conf->key_cert );
12067#endif
12068
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012069 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012070}
12071
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012072#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012073 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12074 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012075/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012076 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012078unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012080#if defined(MBEDTLS_RSA_C)
12081 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12082 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012083#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012084#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012085 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12086 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012088 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012089}
12090
Hanno Becker7e5437a2017-04-28 17:15:26 +010012091unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12092{
12093 switch( type ) {
12094 case MBEDTLS_PK_RSA:
12095 return( MBEDTLS_SSL_SIG_RSA );
12096 case MBEDTLS_PK_ECDSA:
12097 case MBEDTLS_PK_ECKEY:
12098 return( MBEDTLS_SSL_SIG_ECDSA );
12099 default:
12100 return( MBEDTLS_SSL_SIG_ANON );
12101 }
12102}
12103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012104mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012105{
12106 switch( sig )
12107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012108#if defined(MBEDTLS_RSA_C)
12109 case MBEDTLS_SSL_SIG_RSA:
12110 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012111#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012112#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012113 case MBEDTLS_SSL_SIG_ECDSA:
12114 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012115#endif
12116 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012117 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012118 }
12119}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012120#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012121
Hanno Becker7e5437a2017-04-28 17:15:26 +010012122#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12123 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12124
12125/* Find an entry in a signature-hash set matching a given hash algorithm. */
12126mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12127 mbedtls_pk_type_t sig_alg )
12128{
12129 switch( sig_alg )
12130 {
12131 case MBEDTLS_PK_RSA:
12132 return( set->rsa );
12133 case MBEDTLS_PK_ECDSA:
12134 return( set->ecdsa );
12135 default:
12136 return( MBEDTLS_MD_NONE );
12137 }
12138}
12139
12140/* Add a signature-hash-pair to a signature-hash set */
12141void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12142 mbedtls_pk_type_t sig_alg,
12143 mbedtls_md_type_t md_alg )
12144{
12145 switch( sig_alg )
12146 {
12147 case MBEDTLS_PK_RSA:
12148 if( set->rsa == MBEDTLS_MD_NONE )
12149 set->rsa = md_alg;
12150 break;
12151
12152 case MBEDTLS_PK_ECDSA:
12153 if( set->ecdsa == MBEDTLS_MD_NONE )
12154 set->ecdsa = md_alg;
12155 break;
12156
12157 default:
12158 break;
12159 }
12160}
12161
12162/* Allow exactly one hash algorithm for each signature. */
12163void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12164 mbedtls_md_type_t md_alg )
12165{
12166 set->rsa = md_alg;
12167 set->ecdsa = md_alg;
12168}
12169
12170#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12171 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12172
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012173/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012174 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012176mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012177{
12178 switch( hash )
12179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012180#if defined(MBEDTLS_MD5_C)
12181 case MBEDTLS_SSL_HASH_MD5:
12182 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012183#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012184#if defined(MBEDTLS_SHA1_C)
12185 case MBEDTLS_SSL_HASH_SHA1:
12186 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012188#if defined(MBEDTLS_SHA256_C)
12189 case MBEDTLS_SSL_HASH_SHA224:
12190 return( MBEDTLS_MD_SHA224 );
12191 case MBEDTLS_SSL_HASH_SHA256:
12192 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012194#if defined(MBEDTLS_SHA512_C)
12195 case MBEDTLS_SSL_HASH_SHA384:
12196 return( MBEDTLS_MD_SHA384 );
12197 case MBEDTLS_SSL_HASH_SHA512:
12198 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012199#endif
12200 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012201 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012202 }
12203}
12204
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012205/*
12206 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12207 */
12208unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12209{
12210 switch( md )
12211 {
12212#if defined(MBEDTLS_MD5_C)
12213 case MBEDTLS_MD_MD5:
12214 return( MBEDTLS_SSL_HASH_MD5 );
12215#endif
12216#if defined(MBEDTLS_SHA1_C)
12217 case MBEDTLS_MD_SHA1:
12218 return( MBEDTLS_SSL_HASH_SHA1 );
12219#endif
12220#if defined(MBEDTLS_SHA256_C)
12221 case MBEDTLS_MD_SHA224:
12222 return( MBEDTLS_SSL_HASH_SHA224 );
12223 case MBEDTLS_MD_SHA256:
12224 return( MBEDTLS_SSL_HASH_SHA256 );
12225#endif
12226#if defined(MBEDTLS_SHA512_C)
12227 case MBEDTLS_MD_SHA384:
12228 return( MBEDTLS_SSL_HASH_SHA384 );
12229 case MBEDTLS_MD_SHA512:
12230 return( MBEDTLS_SSL_HASH_SHA512 );
12231#endif
12232 default:
12233 return( MBEDTLS_SSL_HASH_NONE );
12234 }
12235}
12236
Hanno Beckeree902df2019-08-23 13:47:47 +010012237#if defined(MBEDTLS_USE_TINYCRYPT)
12238/*
12239 * Check if a curve proposed by the peer is in our list.
12240 * Return 0 if we're willing to use it, -1 otherwise.
12241 */
12242int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12243 mbedtls_uecc_group_id grp_id )
12244{
12245 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12246 if( own_ec_id == grp_id )
12247 return( 0 );
12248 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12249
12250 return( -1 );
12251}
12252#endif /* MBEDTLS_USE_TINYCRYPT */
12253
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012254#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012255/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012256 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012257 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012258 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012259int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12260 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012261{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012262 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12263 if( own_ec_id == grp_id )
12264 return( 0 );
12265 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012266
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012267 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012268}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012269#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012270
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012271#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012272/*
12273 * Check if a hash proposed by the peer is in our list.
12274 * Return 0 if we're willing to use it, -1 otherwise.
12275 */
12276int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12277 mbedtls_md_type_t md )
12278{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012279 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12280 if( md_alg == md )
12281 return( 0 );
12282 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012283
12284 return( -1 );
12285}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012286#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012288#if defined(MBEDTLS_X509_CRT_PARSE_C)
12289int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012290 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012291 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012292 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012293{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012294 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012295#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012296 int usage = 0;
12297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012298#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012299 const char *ext_oid;
12300 size_t ext_len;
12301#endif
12302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012303#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12304 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012305 ((void) cert);
12306 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012307 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012308#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012310#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12311 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012312 {
12313 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012314 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012316 case MBEDTLS_KEY_EXCHANGE_RSA:
12317 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012318 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012319 break;
12320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012321 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12322 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12323 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12324 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012325 break;
12326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012327 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12328 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012329 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012330 break;
12331
12332 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012333 case MBEDTLS_KEY_EXCHANGE_NONE:
12334 case MBEDTLS_KEY_EXCHANGE_PSK:
12335 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12336 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012337 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012338 usage = 0;
12339 }
12340 }
12341 else
12342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012343 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12344 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012345 }
12346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012347 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012348 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012349 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012350 ret = -1;
12351 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012352#else
12353 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012354#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012356#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12357 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012359 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12360 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012361 }
12362 else
12363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012364 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12365 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012366 }
12367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012368 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012369 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012370 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012371 ret = -1;
12372 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012373#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012374
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012375 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012377#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012378
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012379#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12380 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12381int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12382 unsigned char *output,
12383 unsigned char *data, size_t data_len )
12384{
12385 int ret = 0;
12386 mbedtls_md5_context mbedtls_md5;
12387 mbedtls_sha1_context mbedtls_sha1;
12388
12389 mbedtls_md5_init( &mbedtls_md5 );
12390 mbedtls_sha1_init( &mbedtls_sha1 );
12391
12392 /*
12393 * digitally-signed struct {
12394 * opaque md5_hash[16];
12395 * opaque sha_hash[20];
12396 * };
12397 *
12398 * md5_hash
12399 * MD5(ClientHello.random + ServerHello.random
12400 * + ServerParams);
12401 * sha_hash
12402 * SHA(ClientHello.random + ServerHello.random
12403 * + ServerParams);
12404 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012405 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012406 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012408 goto exit;
12409 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012410 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012411 ssl->handshake->randbytes, 64 ) ) != 0 )
12412 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012414 goto exit;
12415 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012416 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012417 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012419 goto exit;
12420 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012421 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012422 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012424 goto exit;
12425 }
12426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012427 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012428 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012430 goto exit;
12431 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012432 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012433 ssl->handshake->randbytes, 64 ) ) != 0 )
12434 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012436 goto exit;
12437 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012438 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012439 data_len ) ) != 0 )
12440 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012442 goto exit;
12443 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012444 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012445 output + 16 ) ) != 0 )
12446 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012448 goto exit;
12449 }
12450
12451exit:
12452 mbedtls_md5_free( &mbedtls_md5 );
12453 mbedtls_sha1_free( &mbedtls_sha1 );
12454
12455 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012456 mbedtls_ssl_pend_fatal_alert( ssl,
12457 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012458
12459 return( ret );
12460
12461}
12462#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12463 MBEDTLS_SSL_PROTO_TLS1_1 */
12464
12465#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12466 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12467int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012468 unsigned char *hash, size_t *hashlen,
12469 unsigned char *data, size_t data_len,
12470 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012471{
12472 int ret = 0;
12473 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012474 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012475 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012476
12477 mbedtls_md_init( &ctx );
12478
12479 /*
12480 * digitally-signed struct {
12481 * opaque client_random[32];
12482 * opaque server_random[32];
12483 * ServerDHParams params;
12484 * };
12485 */
12486 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12487 {
12488 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12489 goto exit;
12490 }
12491 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12492 {
12493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12494 goto exit;
12495 }
12496 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12497 {
12498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12499 goto exit;
12500 }
12501 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12502 {
12503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12504 goto exit;
12505 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012506 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012507 {
12508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12509 goto exit;
12510 }
12511
12512exit:
12513 mbedtls_md_free( &ctx );
12514
12515 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012516 mbedtls_ssl_pend_fatal_alert( ssl,
12517 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012518
12519 return( ret );
12520}
12521#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12522 MBEDTLS_SSL_PROTO_TLS1_2 */
12523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012524#endif /* MBEDTLS_SSL_TLS_C */