blob: cfb8717835ec2fef351b052c95a4d9f3404ac884 [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"
Jarno Lamsaaf60cd72019-12-19 16:45:23 +020051#include "mbedtls/platform.h"
52
Rich Evans00ab4702015-02-06 13:43:58 +000053#include <string.h>
54
Janos Follath23bdca02016-10-07 14:47:14 +010055#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020057#endif
58
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -040059#define PROPER_HS_FRAGMENT 0x75555555
60
Hanno Beckeref982d52019-07-23 15:56:18 +010061#if defined(MBEDTLS_USE_TINYCRYPT)
62static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
63{
Hanno Beckerd089fad2019-07-24 09:05:05 +010064 int ret;
65 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
66 if( ret == 0 )
67 return( (int) size );
68
69 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010070}
Hanno Becker75f12d12019-07-23 16:16:15 +010071
72int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
73 unsigned char **p, unsigned char *end )
74{
75 size_t const secp256r1_uncompressed_point_length =
76 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
77
78 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
79 {
80 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010082 }
83
84 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
85 (*p)[1] != 0x04 )
86 {
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
88 2 * NUM_ECC_BYTES + 1,
89 0x04,
90 (unsigned) (*p)[0],
91 (unsigned) (*p)[1] ) );
Hanno Beckerc64d5af2019-08-23 13:14:36 +010092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker75f12d12019-07-23 16:16:15 +010093 }
94
Teppo Järvelin91d79382019-10-02 09:09:31 +030095 mbedtls_platform_memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
Hanno Becker75f12d12019-07-23 16:16:15 +010096
97 *p += secp256r1_uncompressed_point_length;
98 return( 0 );
99}
Hanno Beckeref982d52019-07-23 15:56:18 +0100100#endif /* MBEDTLS_USE_TINYCRYPT */
101
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100102static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100103static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100104
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100105/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100109 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100110#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200111
112#if defined(MBEDTLS_SSL_PROTO_DTLS)
113 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
114 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200115 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200116#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200117#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100118 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200119#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100120}
121
Hanno Beckerb82350b2019-07-26 07:24:05 +0100122static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
123{
124 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
125 return;
126
127 mbedtls_ssl_send_alert_message( ssl,
128 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
129 ssl->pending_fatal_alert_msg );
130 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
131}
132
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133/*
134 * Start a timer.
135 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200138{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100139 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200140 return;
141
142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100143 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
144 millisecs / 4,
145 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200146}
147
148/*
149 * Return -1 is timer is expired, 0 if it isn't.
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200152{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200154 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200155
Hanno Becker0ae6b242019-06-13 16:45:36 +0100156 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 {
158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200159 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200160 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
162 return( 0 );
163}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200164
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100165static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
166 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100167static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100168
Hanno Becker02f26092019-07-03 16:13:00 +0100169#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100170static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
171 unsigned char *buf,
172 size_t len,
173 mbedtls_record *rec );
174
Hanno Becker02f26092019-07-03 16:13:00 +0100175int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
176 unsigned char *buf,
177 size_t buflen )
178{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400179 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400180 volatile unsigned char *buf_dup = buf;
181 volatile size_t buflen_dup = buflen;
Hanno Becker03e2db62019-07-12 14:40:00 +0100182 mbedtls_record rec;
183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
184 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
185
186 /* We don't support record checking in TLS because
187 * (a) there doesn't seem to be a usecase for it, and
188 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
189 * and we'd need to backup the transform here.
190 */
191#if defined(MBEDTLS_SSL_PROTO_TLS)
192 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
193 {
194 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
195 goto exit;
196 }
197 MBEDTLS_SSL_TRANSPORT_ELSE
198#endif /* MBEDTLS_SSL_PROTO_TLS */
199#if defined(MBEDTLS_SSL_PROTO_DTLS)
200 {
201 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
202 if( ret != 0 )
203 {
204 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
205 goto exit;
206 }
207
208 if( ssl->transform_in != NULL )
209 {
210 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
211 if( ret != 0 )
212 {
213 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
214 goto exit;
215 }
216 }
217 }
218#endif /* MBEDTLS_SSL_PROTO_DTLS */
219
220exit:
221 /* On success, we have decrypted the buffer in-place, so make
222 * sure we don't leak any plaintext data. */
223 mbedtls_platform_zeroize( buf, buflen );
224
225 /* For the purpose of this API, treat messages with unexpected CID
226 * as well as such from future epochs as unexpected. */
227 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
228 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
229 {
230 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
231 }
232
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400233 if( buf_dup != buf || buflen_dup != buflen )
234 {
Andrzej Kurek84bde412020-07-06 15:27:34 -0400235 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400236 }
Hanno Becker03e2db62019-07-12 14:40:00 +0100237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
238 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100239}
240#endif /* MBEDTLS_SSL_RECORD_CHECKING */
241
Hanno Becker67bc7c32018-08-06 11:33:50 +0100242#define SSL_DONT_FORCE_FLUSH 0
243#define SSL_FORCE_FLUSH 1
244
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200245#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100246
Hanno Beckera5a2b082019-05-15 14:03:01 +0100247#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100248/* Top-level Connection ID API */
249
Hanno Beckere0200da2019-06-13 09:23:43 +0100250#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
251 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
252 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100253int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
254 size_t len,
255 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100256{
257 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
259
Hanno Becker791ec6b2019-05-14 11:45:26 +0100260 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
261 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
262 {
263 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
264 }
265
266 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100267 conf->cid_len = len;
268 return( 0 );
269}
Hanno Beckere0200da2019-06-13 09:23:43 +0100270#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
271 !MBEDTLS_SSL_CONF_CID_LEN &&
272 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
273
274#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
275#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
276#endif
277#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
278 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
279#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
280#endif
281
282#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
283 !MBEDTLS_SSL_CONF_CID_LEN &&
284 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100285
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100286int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
287 int enable,
288 unsigned char const *own_cid,
289 size_t own_cid_len )
290{
Andrzej Kurek84bde412020-07-06 15:27:34 -0400291 volatile unsigned char const *own_cid_dup = own_cid;
292 volatile size_t own_cid_len_dup = own_cid_len;
293
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200294 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
296
Hanno Becker07489862019-04-25 16:01:49 +0100297 ssl->negotiate_cid = enable;
298 if( enable == MBEDTLS_SSL_CID_DISABLED )
299 {
300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
301 return( 0 );
302 }
303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100304 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100305
Hanno Beckere0200da2019-06-13 09:23:43 +0100306 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100307 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
309 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100310 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
312 }
313
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300314 /* Not using more secure mbedtls_platform_memcpy as cid is public */
315 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100316 /* Truncation is not an issue here because
317 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
318 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100319
Andrzej Kurek84bde412020-07-06 15:27:34 -0400320 /* Secure against buffer substitution */
321 if( own_cid_dup == own_cid && own_cid_len_dup == own_cid_len )
322 {
323 return( 0 );
324 }
325 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100326}
327
328int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
329 int *enabled,
330 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
331 size_t *peer_cid_len )
332{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100333 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100334
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200335 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100336 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
337 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100338 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100339 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100340
Hanno Beckercb063f52019-05-03 12:54:52 +0100341 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
342 * were used, but client and server requested the empty CID.
343 * This is indistinguishable from not using the CID extension
344 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100345 if( ssl->transform_in->in_cid_len == 0 &&
346 ssl->transform_in->out_cid_len == 0 )
347 {
348 return( 0 );
349 }
350
Hanno Becker633d6042019-05-22 16:50:35 +0100351 if( peer_cid_len != NULL )
352 {
353 *peer_cid_len = ssl->transform_in->out_cid_len;
354 if( peer_cid != NULL )
355 {
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300356 /* Not using more secure mbedtls_platform_memcpy as cid is public */
357 memcpy( peer_cid, ssl->transform_in->out_cid,
Hanno Becker633d6042019-05-22 16:50:35 +0100358 ssl->transform_in->out_cid_len );
359 }
360 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100361
362 *enabled = MBEDTLS_SSL_CID_ENABLED;
363
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100364 return( 0 );
365}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100366#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100367
Hanno Beckerd5847772018-08-28 10:09:23 +0100368/* Forward declarations for functions related to message buffering. */
369static void ssl_buffering_free( mbedtls_ssl_context *ssl );
370static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
371 uint8_t slot );
372static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
373static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
374static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
375static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100376static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
377 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100378static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100379
Hanno Beckera67dee22018-08-22 10:05:20 +0100380static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100381static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100382{
Hanno Becker11682cc2018-08-22 14:41:02 +0100383 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100384
385 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100386 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100387
388 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
389}
390
Hanno Becker67bc7c32018-08-06 11:33:50 +0100391static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
392{
Hanno Becker11682cc2018-08-22 14:41:02 +0100393 size_t const bytes_written = ssl->out_left;
394 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100395
396 /* Double-check that the write-index hasn't gone
397 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100398 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100399 {
400 /* Should never happen... */
401 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
402 }
403
404 return( (int) ( mtu - bytes_written ) );
405}
406
407static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
408{
409 int ret;
410 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400411 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100412
413#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
414 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
415
416 if( max_len > mfl )
417 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100418
419 /* By the standard (RFC 6066 Sect. 4), the MFL extension
420 * only limits the maximum record payload size, so in theory
421 * we would be allowed to pack multiple records of payload size
422 * MFL into a single datagram. However, this would mean that there's
423 * no way to explicitly communicate MTU restrictions to the peer.
424 *
425 * The following reduction of max_len makes sure that we never
426 * write datagrams larger than MFL + Record Expansion Overhead.
427 */
428 if( max_len <= ssl->out_left )
429 return( 0 );
430
431 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100432#endif
433
434 ret = ssl_get_remaining_space_in_datagram( ssl );
435 if( ret < 0 )
436 return( ret );
437 remaining = (size_t) ret;
438
439 ret = mbedtls_ssl_get_record_expansion( ssl );
440 if( ret < 0 )
441 return( ret );
442 expansion = (size_t) ret;
443
444 if( remaining <= expansion )
445 return( 0 );
446
447 remaining -= expansion;
448 if( remaining >= max_len )
449 remaining = max_len;
450
451 return( (int) remaining );
452}
453
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200454/*
455 * Double the retransmit timeout value, within the allowed range,
456 * returning -1 if the maximum value has already been reached.
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200459{
460 uint32_t new_timeout;
461
Hanno Becker1f835fa2019-06-13 10:14:59 +0100462 if( ssl->handshake->retransmit_timeout >=
463 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
464 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200465 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200468 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
469 * in the following way: after the initial transmission and a first
470 * retransmission, back off to a temporary estimated MTU of 508 bytes.
471 * This value is guaranteed to be deliverable (if not guaranteed to be
472 * delivered) of any compliant IPv4 (and IPv6) network, and should work
473 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100474 if( ssl->handshake->retransmit_timeout !=
475 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400476 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200477 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
479 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200480
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200481 new_timeout = 2 * ssl->handshake->retransmit_timeout;
482
483 /* Avoid arithmetic overflow and range overflow */
484 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100485 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200486 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100487 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200488 }
489
490 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200492 ssl->handshake->retransmit_timeout ) );
493
494 return( 0 );
495}
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200498{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100499 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200501 ssl->handshake->retransmit_timeout ) );
502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200506/*
507 * Convert max_fragment_length codes to length.
508 * RFC 6066 says:
509 * enum{
510 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
511 * } MaxFragmentLength;
512 * and we add 0 -> extension unused
513 */
Angus Grattond8213d02016-05-25 20:56:48 +1000514static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200515{
Angus Grattond8213d02016-05-25 20:56:48 +1000516 switch( mfl )
517 {
518 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300519 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000520 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
521 return 512;
522 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
523 return 1024;
524 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
525 return 2048;
526 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
527 return 4096;
528 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300529 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000530 }
531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200533
Hanno Becker58fccf22019-02-06 14:30:46 +0000534int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
535 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_ssl_session_free( dst );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300538 mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000541
542#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200543 if( src->peer_cert != NULL )
544 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200545 int ret;
546
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200547 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200548 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200549 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200554 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200557 dst->peer_cert = NULL;
558 return( ret );
559 }
560 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100561#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000562 if( src->peer_cert_digest != NULL )
563 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000564 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000565 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000566 if( dst->peer_cert_digest == NULL )
567 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
568
Teppo Järvelin91d79382019-10-02 09:09:31 +0300569 mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest,
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000570 src->peer_cert_digest_len );
571 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000572 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000573 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100574#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200577
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200578#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200579 if( src->ticket != NULL )
580 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200581 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200582 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200583 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200584
Teppo Järvelin91d79382019-10-02 09:09:31 +0300585 mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200586 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200587#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200588
589 return( 0 );
590}
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
593int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200594 const unsigned char *key_enc, const unsigned char *key_dec,
595 size_t keylen,
596 const unsigned char *iv_enc, const unsigned char *iv_dec,
597 size_t ivlen,
598 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200599 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
601int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
602int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
603int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
604int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
605#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000606
Paul Bakker5121ce52009-01-03 21:22:43 +0000607/*
608 * Key material generation
609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100611MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200612 const char *label,
613 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000614 unsigned char *dstbuf, size_t dlen )
615{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100616 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000617 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200618 mbedtls_md5_context md5;
619 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000620 unsigned char padding[16];
621 unsigned char sha1sum[20];
Andrzej Kurek84bde412020-07-06 15:27:34 -0400622 volatile const unsigned char *secret_dup = secret;
623 volatile size_t slen_dup = slen;
624 volatile const char *label_dup = label;
625 volatile const unsigned char *random_dup = random;
626 volatile size_t rlen_dup = rlen;
627 volatile unsigned char *dstbuf_dup = dstbuf;
628 volatile size_t dlen_dup = dlen;
Paul Bakker5f70b252012-09-13 14:23:06 +0000629
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200630 mbedtls_md5_init( &md5 );
631 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200632
Paul Bakker5f70b252012-09-13 14:23:06 +0000633 /*
634 * SSLv3:
635 * block =
636 * MD5( secret + SHA1( 'A' + secret + random ) ) +
637 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
638 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
639 * ...
640 */
641 for( i = 0; i < dlen / 16; i++ )
642 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200643 mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100645 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100646 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100647 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100649 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100650 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100651 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100652 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100653 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100654 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000655
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100656 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100657 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100658 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100659 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100660 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100661 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100662 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100663 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000664 }
665
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100666exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200667 mbedtls_md5_free( &md5 );
668 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000669
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500670 mbedtls_platform_zeroize( padding, sizeof( padding ) );
671 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000672
Andrzej Kurek84bde412020-07-06 15:27:34 -0400673 /* Secure against buffer substitution */
674 if( secret_dup == secret && slen_dup == slen && label_dup == label &&
675 random_dup == random && rlen_dup == rlen && dstbuf_dup == dstbuf &&
676 dlen_dup == dlen )
677 {
678 return( ret );
679 }
680 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker5f70b252012-09-13 14:23:06 +0000681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100685MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200686 const char *label,
687 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000688 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000689{
Paul Bakker23986e52011-04-24 08:57:21 +0000690 size_t nb, hs;
691 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200692 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 unsigned char tmp[128];
694 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100695 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100697 int ret;
Andrzej Kurek84bde412020-07-06 15:27:34 -0400698 volatile const unsigned char *secret_dup = secret;
699 volatile size_t slen_dup = slen;
700 volatile const char *label_dup = label;
701 volatile const unsigned char *random_dup = random;
702 volatile size_t rlen_dup = rlen;
703 volatile unsigned char *dstbuf_dup = dstbuf;
704 volatile size_t dlen_dup = dlen;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
708 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 hs = ( slen + 1 ) / 2;
712 S1 = secret;
713 S2 = secret + slen - hs;
714
715 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300716 mbedtls_platform_memcpy( tmp + 20, label, nb );
717 mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000718 nb += rlen;
719
720 /*
721 * First compute P_md5(secret,label+random)[0..dlen]
722 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100723 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
724 MBEDTLS_MD_INVALID_HANDLE )
725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100727 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730 return( ret );
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
733 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
734 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
736 for( i = 0; i < dlen; i += 16 )
737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_md_hmac_reset ( &md_ctx );
739 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
740 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_md_hmac_reset ( &md_ctx );
743 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
744 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
746 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
747
748 for( j = 0; j < k; j++ )
749 dstbuf[i + j] = h_i[j];
750 }
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100753
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 /*
755 * XOR out with P_sha1(secret,label+random)[0..dlen]
756 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100757 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
758 MBEDTLS_MD_INVALID_HANDLE )
759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100761 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100764 return( ret );
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
767 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
768 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770 for( i = 0; i < dlen; i += 20 )
771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_md_hmac_reset ( &md_ctx );
773 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
774 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_md_hmac_reset ( &md_ctx );
777 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
778 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
781
782 for( j = 0; j < k; j++ )
783 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
784 }
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100787
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500788 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
789 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Andrzej Kurek84bde412020-07-06 15:27:34 -0400791 /* Secure against buffer substitution */
792 if( secret_dup == secret && slen_dup == slen && label_dup == label &&
793 random_dup == random && rlen_dup == rlen && dstbuf_dup == dstbuf &&
794 dlen_dup == dlen )
795 {
796 return( 0 );
797 }
798 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000799}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100803#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
804MBEDTLS_ALWAYS_INLINE static inline
805#else
806static
807#endif
808int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100809 const unsigned char *secret, size_t slen,
810 const char *label,
811 const unsigned char *random, size_t rlen,
812 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000813{
814 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100815 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000816 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100818 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100820 int ret;
Andrzej Kurek84bde412020-07-06 15:27:34 -0400821 volatile const unsigned char *secret_dup = secret;
822 volatile size_t slen_dup = slen;
823 volatile const char *label_dup = label;
824 volatile const unsigned char *random_dup = random;
825 volatile size_t rlen_dup = rlen;
826 volatile unsigned char *dstbuf_dup = dstbuf;
827 volatile size_t dlen_dup = dlen;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100831 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
832 MBEDTLS_MD_INVALID_HANDLE )
833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100835 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100838
839 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000841
842 nb = strlen( label );
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200843 (void)mbedtls_platform_memcpy( tmp + md_len, label, nb );
844 (void)mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000845 nb += rlen;
846
847 /*
848 * Compute P_<hash>(secret, label + random)[0..dlen]
849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100851 return( ret );
852
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200853 if ( ( ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ) ) != 0 )
854 return( ret );
855 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ) ) != 0 )
856 return( ret );
857 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
858 return( ret );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100859
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100860 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000861 {
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200862 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
863 return( ret );
864 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ) ) != 0 )
865 return( ret );
866 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, h_i ) ) != 0 )
867 return( ret );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100868
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200869 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
870 return( ret );
871 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ) ) != 0 )
872 return( ret );
873 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
874 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000875
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100876 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000877
878 for( j = 0; j < k; j++ )
879 dstbuf[i + j] = h_i[j];
880 }
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100883
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200884 (void)mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
885 (void)mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000886
Andrzej Kurek84bde412020-07-06 15:27:34 -0400887 /* Secure against buffer substitution */
888 if( secret_dup == secret && slen_dup == slen && label_dup == label &&
889 random_dup == random && rlen_dup == rlen && dstbuf_dup == dstbuf &&
890 dlen_dup == dlen )
891 {
892 return( 0 );
893 }
894 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000895}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100898MBEDTLS_NO_INLINE static int tls_prf_sha256(
899 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100900 const char *label,
901 const unsigned char *random, size_t rlen,
902 unsigned char *dstbuf, size_t dlen )
903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100905 label, random, rlen, dstbuf, dlen ) );
906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100910MBEDTLS_NO_INLINE static int tls_prf_sha384(
911 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200912 const char *label,
913 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000914 unsigned char *dstbuf, size_t dlen )
915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100917 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#endif /* MBEDTLS_SHA512_C */
920#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000921
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100922/*
923 * Call the appropriate PRF function
924 */
Hanno Becker2793f742019-08-16 14:28:43 +0100925MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100926 mbedtls_md_type_t hash,
927 const unsigned char *secret, size_t slen,
928 const char *label,
929 const unsigned char *random, size_t rlen,
930 unsigned char *dstbuf, size_t dlen )
931{
932#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
933 (void) hash;
934#endif
935
936#if defined(MBEDTLS_SSL_PROTO_SSL3)
937 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
938 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
939 else
940#endif
941#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100942 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100943 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
944 else
945#endif
946#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
947#if defined(MBEDTLS_SHA512_C)
948 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
949 hash == MBEDTLS_MD_SHA384 )
950 {
951 return( tls_prf_sha384( secret, slen, label, random, rlen,
952 dstbuf, dlen ) );
953 }
954 else
955#endif
956#if defined(MBEDTLS_SHA256_C)
957 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
958 {
959 return( tls_prf_sha256( secret, slen, label, random, rlen,
960 dstbuf, dlen ) );
961 }
962#endif
963#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
964
965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
966}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200967
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100968#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100969MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100970 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
971{
972 const char *sender;
973 mbedtls_md5_context md5;
974 mbedtls_sha1_context sha1;
975
976 unsigned char padbuf[48];
977 unsigned char md5sum[16];
978 unsigned char sha1sum[20];
979
980 mbedtls_ssl_session *session = ssl->session_negotiate;
981 if( !session )
982 session = ssl->session;
983
984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
985
986 mbedtls_md5_init( &md5 );
987 mbedtls_sha1_init( &sha1 );
988
989 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
990 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
991
992 /*
993 * SSLv3:
994 * hash =
995 * MD5( master + pad2 +
996 * MD5( handshake + sender + master + pad1 ) )
997 * + SHA1( master + pad2 +
998 * SHA1( handshake + sender + master + pad1 ) )
999 */
1000
1001#if !defined(MBEDTLS_MD5_ALT)
1002 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1003 md5.state, sizeof( md5.state ) );
1004#endif
1005
1006#if !defined(MBEDTLS_SHA1_ALT)
1007 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1008 sha1.state, sizeof( sha1.state ) );
1009#endif
1010
1011 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
1012 : "SRVR";
1013
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001014 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001015
1016 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
1017 mbedtls_md5_update_ret( &md5, session->master, 48 );
1018 mbedtls_md5_update_ret( &md5, padbuf, 48 );
1019 mbedtls_md5_finish_ret( &md5, md5sum );
1020
1021 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
1022 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
1023 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
1024 mbedtls_sha1_finish_ret( &sha1, sha1sum );
1025
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001026 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001027
1028 mbedtls_md5_starts_ret( &md5 );
1029 mbedtls_md5_update_ret( &md5, session->master, 48 );
1030 mbedtls_md5_update_ret( &md5, padbuf, 48 );
1031 mbedtls_md5_update_ret( &md5, md5sum, 16 );
1032 mbedtls_md5_finish_ret( &md5, buf );
1033
1034 mbedtls_sha1_starts_ret( &sha1 );
1035 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
1036 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
1037 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
1038 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
1039
1040 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
1041
1042 mbedtls_md5_free( &md5 );
1043 mbedtls_sha1_free( &sha1 );
1044
1045 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1046 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
1047 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
1048
1049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1050}
1051#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1052
1053#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +01001054MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001055 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1056{
1057 int len = 12;
1058 const char *sender;
1059 mbedtls_md5_context md5;
1060 mbedtls_sha1_context sha1;
1061 unsigned char padbuf[36];
1062
1063 mbedtls_ssl_session *session = ssl->session_negotiate;
1064 if( !session )
1065 session = ssl->session;
1066
1067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1068
1069 mbedtls_md5_init( &md5 );
1070 mbedtls_sha1_init( &sha1 );
1071
1072 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1073 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1074
1075 /*
1076 * TLSv1:
1077 * hash = PRF( master, finished_label,
1078 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1079 */
1080
1081#if !defined(MBEDTLS_MD5_ALT)
1082 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1083 md5.state, sizeof( md5.state ) );
1084#endif
1085
1086#if !defined(MBEDTLS_SHA1_ALT)
1087 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1088 sha1.state, sizeof( sha1.state ) );
1089#endif
1090
1091 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1092 ? "client finished"
1093 : "server finished";
1094
1095 mbedtls_md5_finish_ret( &md5, padbuf );
1096 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1097
1098 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1099 mbedtls_ssl_suite_get_mac(
1100 mbedtls_ssl_ciphersuite_from_id(
1101 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1102 session->master, 48, sender,
1103 padbuf, 36, buf, len );
1104
1105 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1106
1107 mbedtls_md5_free( &md5 );
1108 mbedtls_sha1_free( &sha1 );
1109
1110 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1111
1112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1113}
1114#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1115
1116#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1117#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001118MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001119 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1120{
1121 int len = 12;
1122 const char *sender;
1123 mbedtls_sha256_context sha256;
1124 unsigned char padbuf[32];
1125
1126 mbedtls_ssl_session *session = ssl->session_negotiate;
1127 if( !session )
1128 session = ssl->session;
1129
1130 mbedtls_sha256_init( &sha256 );
1131
1132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1133
1134 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1135
1136 /*
1137 * TLSv1.2:
1138 * hash = PRF( master, finished_label,
1139 * Hash( handshake ) )[0.11]
1140 */
1141
1142#if !defined(MBEDTLS_SHA256_ALT)
1143 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1144 sha256.state, sizeof( sha256.state ) );
1145#endif
1146
1147 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1148 ? "client finished"
1149 : "server finished";
1150
1151 mbedtls_sha256_finish_ret( &sha256, padbuf );
1152
1153 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1154 mbedtls_ssl_suite_get_mac(
1155 mbedtls_ssl_ciphersuite_from_id(
1156 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1157 session->master, 48, sender,
1158 padbuf, 32, buf, len );
1159
1160 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1161
1162 mbedtls_sha256_free( &sha256 );
1163
1164 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1165
1166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1167}
1168#endif /* MBEDTLS_SHA256_C */
1169
1170#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001171MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001172 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1173{
1174 int len = 12;
1175 const char *sender;
1176 mbedtls_sha512_context sha512;
1177 unsigned char padbuf[48];
1178
1179 mbedtls_ssl_session *session = ssl->session_negotiate;
1180 if( !session )
1181 session = ssl->session;
1182
1183 mbedtls_sha512_init( &sha512 );
1184
1185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1186
1187 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1188
1189 /*
1190 * TLSv1.2:
1191 * hash = PRF( master, finished_label,
1192 * Hash( handshake ) )[0.11]
1193 */
1194
1195#if !defined(MBEDTLS_SHA512_ALT)
1196 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1197 sha512.state, sizeof( sha512.state ) );
1198#endif
1199
1200 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1201 ? "client finished"
1202 : "server finished";
1203
1204 mbedtls_sha512_finish_ret( &sha512, padbuf );
1205
1206 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1207 mbedtls_ssl_suite_get_mac(
1208 mbedtls_ssl_ciphersuite_from_id(
1209 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1210 session->master, 48, sender,
1211 padbuf, 48, buf, len );
1212
1213 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1214
1215 mbedtls_sha512_free( &sha512 );
1216
1217 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1218
1219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1220}
1221#endif /* MBEDTLS_SHA512_C */
1222#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1223
Hanno Becker2793f742019-08-16 14:28:43 +01001224MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1225 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001226 mbedtls_md_type_t hash,
1227 mbedtls_ssl_context *ssl,
1228 unsigned char *buf,
1229 int from )
1230{
1231#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1232 (void) hash;
1233#endif
1234
1235#if defined(MBEDTLS_SSL_PROTO_SSL3)
1236 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1237 ssl_calc_finished_ssl( ssl, buf, from );
1238 else
1239#endif
1240#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001241 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001242 ssl_calc_finished_tls( ssl, buf, from );
1243 else
1244#endif
1245#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1246#if defined(MBEDTLS_SHA512_C)
1247 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1248 hash == MBEDTLS_MD_SHA384 )
1249 {
1250 ssl_calc_finished_tls_sha384( ssl, buf, from );
1251 }
1252 else
1253#endif
1254#if defined(MBEDTLS_SHA256_C)
1255 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1256 ssl_calc_finished_tls_sha256( ssl, buf, from );
1257 else
1258#endif
1259#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1261
1262 return( 0 );
1263}
1264
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001265/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001266 * Populate a transform structure with session keys and all the other
1267 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001268 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001269 * Parameters:
1270 * - [in/out]: transform: structure to populate
1271 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001272 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001273 * - [in] ciphersuite
1274 * - [in] master
1275 * - [in] encrypt_then_mac
1276 * - [in] trunc_hmac
1277 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001278 * - [in] tls_prf: pointer to PRF to use for key derivation
1279 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001280 * - [in] minor_ver: SSL/TLS minor version
1281 * - [in] endpoint: client or server
1282 * - [in] ssl: optionally used for:
1283 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1284 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1285 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001286 */
Hanno Becker298a4702019-08-16 10:21:32 +01001287/* Force compilers to inline this function if it's used only
1288 * from one place, because at least ARMC5 doesn't do that
1289 * automatically. */
1290#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1291MBEDTLS_ALWAYS_INLINE static inline
1292#else
1293static
1294#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1295int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001296 int ciphersuite,
1297 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001298#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001299#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1300 int encrypt_then_mac,
1301#endif
1302#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1303 int trunc_hmac,
1304#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001305#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001306#if defined(MBEDTLS_ZLIB_SUPPORT)
1307 int compression,
1308#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001309 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001310 int minor_ver,
1311 unsigned endpoint,
Manuel Pégourié-Gonnard13bebd02020-03-13 11:28:19 +01001312#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1313 const
1314#endif
1315 mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001316{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001317 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 unsigned char keyblk[256];
1319 unsigned char *key1;
1320 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001321 unsigned char *mac_enc;
1322 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001323 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001324 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001325 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001326 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001328 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001329
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001330#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1331 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1332 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001333 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001334 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001335#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001336
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001337 /*
1338 * Some data just needs copying into the structure
1339 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001340#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1341 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001342 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001343#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001344
1345#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001346 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001347#else
1348 ((void) minor_ver);
1349#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001351#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001352 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001353#endif
1354
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001355 /*
1356 * Get various info structures
1357 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001358 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001359 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001360 {
1361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001362 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1364 }
1365
Hanno Becker473f98f2019-06-26 10:27:32 +01001366 cipher_info = mbedtls_cipher_info_from_type(
1367 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001368 if( cipher_info == NULL )
1369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001371 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001373 }
1374
Hanno Becker473f98f2019-06-26 10:27:32 +01001375 md_info = mbedtls_md_info_from_type(
1376 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001377 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001380 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 }
1383
Hanno Beckera5a2b082019-05-15 14:03:01 +01001384#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001385 /* Copy own and peer's CID if the use of the CID
1386 * extension has been negotiated. */
1387 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1388 {
1389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001390
Hanno Becker4932f9f2019-05-03 15:23:51 +01001391 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001392 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1393 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001394 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001395 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001396
1397 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001398 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1399 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001400 ssl->handshake->peer_cid_len );
1401 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1402 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001403 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001404#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001405
Paul Bakker5121ce52009-01-03 21:22:43 +00001406 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001407 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001408 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001409 ret = ssl_prf( minor_ver,
1410 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1411 master, 48, "key expansion", randbytes, 64,
1412 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001413 if( ret != 0 )
1414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001416 return( ret );
1417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001420 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001421 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001422 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 /*
1426 * Determine the appropriate key, IV and MAC length.
1427 */
Paul Bakker68884e32013-01-07 18:20:04 +01001428
Hanno Beckere7f2df02017-12-27 08:17:40 +00001429 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001430
Hanno Beckerf1229442018-01-03 15:32:31 +00001431#if defined(MBEDTLS_GCM_C) || \
1432 defined(MBEDTLS_CCM_C) || \
1433 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001435 cipher_info->mode == MBEDTLS_MODE_CCM ||
1436 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001438 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001439 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001440 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1441 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001442
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001443 /* All modes haves 96-bit IVs;
1444 * GCM and CCM has 4 implicit and 8 explicit bytes
1445 * ChachaPoly has all 12 bytes implicit
1446 */
Paul Bakker68884e32013-01-07 18:20:04 +01001447 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001448 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1449 transform->fixed_ivlen = 12;
1450 else
1451 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001452 }
1453 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001454#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1455#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1456 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1457 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001458 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001459 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1461 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001464 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001465 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001466
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001467 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001468 mac_key_len = mbedtls_md_get_size( md_info );
1469 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001472 /*
1473 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1474 * (rfc 6066 page 13 or rfc 2104 section 4),
1475 * so we only need to adjust the length here.
1476 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001477 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001480
1481#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1482 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001483 * HMAC implementation which also truncates the key
1484 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001485 mac_key_len = transform->maclen;
1486#endif
1487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001489
1490 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001491 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001492 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001493 else
1494#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1495 {
1496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1497 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001499
Hanno Beckera9d5c452019-07-25 16:47:12 +01001500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001501 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001502 (unsigned) transform->ivlen,
1503 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
1505 /*
1506 * Finally setup the cipher contexts, IVs and MAC secrets.
1507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001509 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001511 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001512 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001513
Paul Bakker68884e32013-01-07 18:20:04 +01001514 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001515 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001517 /*
1518 * This is not used in TLS v1.1.
1519 */
Paul Bakker48916f92012-09-16 19:57:18 +00001520 iv_copy_len = ( transform->fixed_ivlen ) ?
1521 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001522 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1523 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001524 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 }
1526 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#endif /* MBEDTLS_SSL_CLI_C */
1528#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001529 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001531 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001532 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Hanno Becker81c7b182017-11-09 18:39:33 +00001534 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001535 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001537 /*
1538 * This is not used in TLS v1.1.
1539 */
Paul Bakker48916f92012-09-16 19:57:18 +00001540 iv_copy_len = ( transform->fixed_ivlen ) ?
1541 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001542 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1543 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001544 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001545 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001546 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552
Hanno Becker92231322018-01-03 15:32:51 +00001553#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001555 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001556 {
Hanno Becker92231322018-01-03 15:32:51 +00001557 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001561 }
1562
Teppo Järvelin91d79382019-10-02 09:09:31 +03001563 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1564 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001565 }
1566 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1568#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1569 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001570 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001571 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001572 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1573 For AEAD-based ciphersuites, there is nothing to do here. */
1574 if( mac_key_len != 0 )
1575 {
1576 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1577 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1578 }
Paul Bakker68884e32013-01-07 18:20:04 +01001579 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001580 else
1581#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001585 }
Hanno Becker92231322018-01-03 15:32:51 +00001586#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1589 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001592
Hanno Beckere7f2df02017-12-27 08:17:40 +00001593 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001594 transform->iv_enc, transform->iv_dec,
1595 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001596 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001597 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1600 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001601 }
1602 }
Hanno Becker92231322018-01-03 15:32:51 +00001603#else
1604 ((void) mac_dec);
1605 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001607
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001608#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1609 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001610 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001611 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001612 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001613 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001614 iv_copy_len );
1615 }
1616#endif
1617
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001618 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001619 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001621 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001622 return( ret );
1623 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001624
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001625 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001626 cipher_info ) ) != 0 )
1627 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001629 return( ret );
1630 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001633 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001637 return( ret );
1638 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001641 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001645 return( ret );
1646 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#if defined(MBEDTLS_CIPHER_MODE_CBC)
1649 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1652 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001655 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001656 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1659 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001662 return( ret );
1663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001667 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001669 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001671 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001674
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001675 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1676 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001677
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001678 if( deflateInit( &transform->ctx_deflate,
1679 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001680 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1683 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001684 }
1685 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001687
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 return( 0 );
1689}
1690
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001691#if defined(MBEDTLS_SSL_PROTO_SSL3)
1692static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1693 unsigned char hash[36],
1694 size_t *hlen )
1695{
1696 mbedtls_md5_context md5;
1697 mbedtls_sha1_context sha1;
1698 unsigned char pad_1[48];
1699 unsigned char pad_2[48];
1700
1701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1702
1703 mbedtls_md5_init( &md5 );
1704 mbedtls_sha1_init( &sha1 );
1705
1706 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1707 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1708
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001709 mbedtls_platform_memset( pad_1, 0x36, 48 );
1710 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001711
1712 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1713 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1714 mbedtls_md5_finish_ret( &md5, hash );
1715
1716 mbedtls_md5_starts_ret( &md5 );
1717 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1718 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1719 mbedtls_md5_update_ret( &md5, hash, 16 );
1720 mbedtls_md5_finish_ret( &md5, hash );
1721
1722 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1723 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1724 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1725
1726 mbedtls_sha1_starts_ret( &sha1 );
1727 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1728 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1729 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1730 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1731
1732 *hlen = 36;
1733
1734 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1736
1737 mbedtls_md5_free( &md5 );
1738 mbedtls_sha1_free( &sha1 );
1739
1740 return;
1741}
1742#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1743
1744#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1745static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1746 unsigned char hash[36],
1747 size_t *hlen )
1748{
1749 mbedtls_md5_context md5;
1750 mbedtls_sha1_context sha1;
1751
1752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1753
1754 mbedtls_md5_init( &md5 );
1755 mbedtls_sha1_init( &sha1 );
1756
1757 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1758 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1759
1760 mbedtls_md5_finish_ret( &md5, hash );
1761 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1762
1763 *hlen = 36;
1764
1765 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1767
1768 mbedtls_md5_free( &md5 );
1769 mbedtls_sha1_free( &sha1 );
1770
1771 return;
1772}
1773#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1774
1775#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1776#if defined(MBEDTLS_SHA256_C)
1777static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1778 unsigned char hash[32],
1779 size_t *hlen )
1780{
1781 mbedtls_sha256_context sha256;
1782
1783 mbedtls_sha256_init( &sha256 );
1784
1785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1786
1787 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1788 mbedtls_sha256_finish_ret( &sha256, hash );
1789
1790 *hlen = 32;
1791
1792 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1794
1795 mbedtls_sha256_free( &sha256 );
1796
1797 return;
1798}
1799#endif /* MBEDTLS_SHA256_C */
1800
1801#if defined(MBEDTLS_SHA512_C)
1802static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1803 unsigned char hash[48],
1804 size_t *hlen )
1805{
1806 mbedtls_sha512_context sha512;
1807
1808 mbedtls_sha512_init( &sha512 );
1809
1810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1811
1812 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1813 mbedtls_sha512_finish_ret( &sha512, hash );
1814
1815 *hlen = 48;
1816
1817 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1819
1820 mbedtls_sha512_free( &sha512 );
1821
1822 return;
1823}
1824#endif /* MBEDTLS_SHA512_C */
1825#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1826
Hanno Becker2f41b242019-08-15 17:29:43 +01001827int mbedtls_ssl_calc_verify( int minor_ver,
1828 mbedtls_md_type_t hash,
1829 mbedtls_ssl_context const *ssl,
1830 unsigned char *dst,
1831 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001832{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001833#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1834 (void) hash;
1835#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001836
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001837#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001838 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001839 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001840 else
1841#endif
1842#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001843 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001844 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001845 else
1846#endif
1847#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1848#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001849 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1850 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001851 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001852 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001853 }
1854 else
1855#endif
1856#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001857 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001858 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001859 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001860 }
1861 else
1862#endif
1863#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1864 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1866 }
1867
1868 return( 0 );
1869}
1870
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001871/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001872 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001873 *
1874 * Parameters:
1875 * [in/out] handshake
1876 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1877 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001878 * [out] master
1879 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001880 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001881static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001882 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001883 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001884{
1885 int ret;
Andrzej Kurek84bde412020-07-06 15:27:34 -04001886 volatile unsigned char *master_dup = master;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001887
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001888/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1889/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1890/* (void) ssl; */
1891/* #endif */
1892
1893 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1894 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001895
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001896#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001897 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001898 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1900 return( 0 );
1901 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001902#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001903
1904 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1905 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001906
1907#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001908 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1909 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001910 {
1911 unsigned char session_hash[48];
1912 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001913
Hanno Becker2f41b242019-08-15 17:29:43 +01001914 mbedtls_ssl_calc_verify(
1915 mbedtls_ssl_get_minor_ver( ssl ),
1916 mbedtls_ssl_suite_get_mac( ciphersuite ),
1917 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001918
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001919 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1920 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001921
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001922 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1923 mbedtls_ssl_suite_get_mac( ciphersuite ),
1924 handshake->premaster, handshake->pmslen,
1925 "extended master secret",
1926 session_hash, hash_len,
1927 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001928 }
1929 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001930#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001931 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001932 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1933 mbedtls_ssl_suite_get_mac( ciphersuite ),
1934 handshake->premaster, handshake->pmslen,
1935 "master secret",
1936 handshake->randbytes, 64,
1937 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001938 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001939 if( ret != 0 )
1940 {
1941 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1942 return( ret );
1943 }
1944
1945 mbedtls_platform_zeroize( handshake->premaster,
1946 sizeof(handshake->premaster) );
Andrzej Kurek84bde412020-07-06 15:27:34 -04001947 /* Secure against buffer substitution */
1948 if( master_dup == master )
1949 {
1950 return( 0 );
1951 }
1952 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001953}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001954
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001955int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1956{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001957 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001958
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001960 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001961 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001962 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001963 ssl->session_negotiate->master,
1964 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001965 if( ret != 0 )
1966 {
1967 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1968 return( ret );
1969 }
1970
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001971 /* Swap the client and server random values:
1972 * - MS derivation wanted client+server (RFC 5246 8.1)
1973 * - key derivation wants server+client (RFC 5246 6.3) */
1974 {
1975 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001976 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1977 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1978 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001979 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1980 }
1981
1982 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001983 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001984 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1985 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001986#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001987#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001988 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001989#endif
1990#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001991 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001992#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001993#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001994#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001995 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001996#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001997 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001998 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001999 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
2000 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02002001 if( ret == 0 )
2002 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002003 mbedtls_platform_random_delay();
Jarno Lamsa4031a452019-12-19 08:11:12 +02002004 if( ret == 0 )
2005 {
2006 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
2007 }
2008 else
2009 {
2010 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2011 }
2012 }
2013 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02002014 {
2015 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
2016 return( ret );
2017 }
2018
2019 /* We no longer need Server/ClientHello.random values */
2020 mbedtls_platform_zeroize( ssl->handshake->randbytes,
2021 sizeof( ssl->handshake->randbytes ) );
2022
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02002023 /* Allocate compression buffer */
2024#if defined(MBEDTLS_ZLIB_SUPPORT)
jiblime92af9a92019-12-18 21:40:01 -08002025 if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02002026 ssl->compress_buf == NULL )
2027 {
2028 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
2029 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
2030 if( ssl->compress_buf == NULL )
2031 {
2032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02002033 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02002034 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
2035 }
2036 }
2037#endif
2038
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
2040
2041 return( 0 );
2042}
2043
Hanno Becker09d23642019-07-22 17:18:18 +01002044int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
2045{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04002046 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Becker09d23642019-07-22 17:18:18 +01002047
2048 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
2049 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01002050#if defined(MBEDTLS_USE_TINYCRYPT)
2051 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01002052 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01002053 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01002054 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2055 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2056 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2057 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2058 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002059 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002060 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2061 ssl->handshake->ecdh_privkey,
2062 ssl->handshake->premaster );
2063 if( ret == UECC_FAULT_DETECTED )
2064 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2065 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002066 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002067 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01002068
2069 ssl->handshake->pmslen = NUM_ECC_BYTES;
2070 }
2071 else
2072#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002073#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2074 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2075 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2076 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002077 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002078 ssl->handshake->premaster,
2079 MBEDTLS_PREMASTER_SIZE,
2080 &ssl->handshake->pmslen,
2081 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002082 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2083 if( ret == 0 )
2084 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002085 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002086 if( ret == 0 )
2087 {
2088 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2089 }
2090 else
2091 {
Piotr Nowickie048b912020-06-05 17:59:28 +02002092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret",
2093 MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2094 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002095 }
2096 }
2097 else
Hanno Becker09d23642019-07-22 17:18:18 +01002098 {
2099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2100 return( ret );
2101 }
2102
2103 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2104 }
2105 else
2106#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002107#if defined(MBEDTLS_ECDH_C) && \
2108 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2109 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2110 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2111 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002112 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2113 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2114 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2115 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2116 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2117 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2118 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2119 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2120 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002121 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002122 &ssl->handshake->pmslen,
2123 ssl->handshake->premaster,
2124 MBEDTLS_MPI_MAX_SIZE,
2125 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002126 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2127 if( ret == 0 )
2128 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002129 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002130 if( ret == 0 )
2131 {
2132 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2133 }
2134 else
2135 {
2136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002137 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002138 }
2139 }
2140 else
Hanno Becker09d23642019-07-22 17:18:18 +01002141 {
2142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2143 return( ret );
2144 }
2145
2146 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2147 }
2148 else
2149#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2150 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2151 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2152 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2153#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2154 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2155 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002156 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2157 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2158 if( ret == 0 )
2159 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002160 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002161 if( ret == 0 )
2162 {
2163 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2164 }
2165 else
2166 {
2167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002168 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002169 }
2170 }
2171 else
Hanno Becker09d23642019-07-22 17:18:18 +01002172 {
2173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2174 return( ret );
2175 }
2176 }
2177 else
2178#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2179#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2180 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2181 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2182 {
2183 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2184 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2185 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002186 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002187 if( ret == 0 )
2188 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002189 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002190 if( ret == 0 )
2191 {
2192 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2193 }
2194 else
2195 {
2196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002197 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002198 }
2199 }
2200 else
Hanno Becker09d23642019-07-22 17:18:18 +01002201 {
2202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2203 return( ret );
2204 }
2205 }
2206 else
2207#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2208#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2209 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2210 == MBEDTLS_KEY_EXCHANGE_RSA )
2211 {
2212 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002213 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002214 * ssl_rsa_generate_partial_pms(). Only the
2215 * PMS length needs to be set. */
2216 ssl->handshake->pmslen = 48;
2217 }
2218 else
2219#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2220 {
2221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2223 }
2224
2225 return( 0 );
2226}
2227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2229int 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 +02002230{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002231 unsigned char *p = ssl->handshake->premaster;
2232 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002233 const unsigned char *psk = ssl->conf->psk;
2234 size_t psk_len = ssl->conf->psk_len;
2235
2236 /* If the psk callback was called, use its result */
2237 if( ssl->handshake->psk != NULL )
2238 {
2239 psk = ssl->handshake->psk;
2240 psk_len = ssl->handshake->psk_len;
2241 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002242
2243 /*
2244 * PMS = struct {
2245 * opaque other_secret<0..2^16-1>;
2246 * opaque psk<0..2^16-1>;
2247 * };
2248 * with "other_secret" depending on the particular key exchange
2249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2251 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002252 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002253 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002255
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002256 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002257
2258 if( end < p || (size_t)( end - p ) < psk_len )
2259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2260
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002261 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002262 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002263 }
2264 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2266#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2267 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002268 {
2269 /*
2270 * other_secret already set by the ClientKeyExchange message,
2271 * and is 48 bytes long
2272 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002273 if( end - p < 2 )
2274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2275
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002276 *p++ = 0;
2277 *p++ = 48;
2278 p += 48;
2279 }
2280 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2282#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2283 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002284 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002285 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002286 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002287
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002288 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002290 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002291 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002292 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002295 return( ret );
2296 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002297 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002298 p += len;
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002301 }
2302 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2304#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2305 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002306 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002307 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002308 size_t zlen;
2309
Hanno Becker982da7e2019-09-02 09:47:39 +01002310#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002311 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2312 ssl->handshake->ecdh_privkey,
2313 p + 2 );
2314 if( ret == UECC_FAULT_DETECTED )
2315 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2316 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002317 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002318
2319 zlen = NUM_ECC_BYTES;
2320#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002322 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002323 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002324 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002327 return( ret );
2328 }
2329
Hanno Becker982da7e2019-09-02 09:47:39 +01002330 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2331 MBEDTLS_DEBUG_ECDH_Z );
2332#endif /* MBEDTLS_USE_TINYCRYPT */
2333
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002334 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002335 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002336 }
2337 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002342 }
2343
2344 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002345 if( end - p < 2 )
2346 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002347
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002348 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002349
2350 if( end < p || (size_t)( end - p ) < psk_len )
2351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2352
Teppo Järvelin91d79382019-10-02 09:09:31 +03002353 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002354 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002355
2356 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2357
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002358 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2359
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002360 return( 0 );
2361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002365/*
2366 * SSLv3.0 MAC functions
2367 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002368#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002369static void ssl_mac( mbedtls_md_context_t *md_ctx,
2370 const unsigned char *secret,
2371 const unsigned char *buf, size_t len,
2372 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002373 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002374{
2375 unsigned char header[11];
2376 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002377 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2379 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002380
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002381 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002383 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002384 else
Paul Bakker68884e32013-01-07 18:20:04 +01002385 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
Teppo Järvelin91d79382019-10-02 09:09:31 +03002387 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002388 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002389 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002390
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002391 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 mbedtls_md_starts( md_ctx );
2393 mbedtls_md_update( md_ctx, secret, md_size );
2394 mbedtls_md_update( md_ctx, padding, padlen );
2395 mbedtls_md_update( md_ctx, header, 11 );
2396 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002397 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002399 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 mbedtls_md_starts( md_ctx );
2401 mbedtls_md_update( md_ctx, secret, md_size );
2402 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002403 mbedtls_md_update( md_ctx, out, md_size );
2404 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002407
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002408/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002409 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002410#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002411 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2412 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2413 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2414/* This function makes sure every byte in the memory region is accessed
2415 * (in ascending addresses order) */
2416static void ssl_read_memory( unsigned char *p, size_t len )
2417{
2418 unsigned char acc = 0;
2419 volatile unsigned char force;
2420
2421 for( ; len != 0; p++, len-- )
2422 acc ^= *p;
2423
2424 force = acc;
2425 (void) force;
2426}
2427#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2428
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002429/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002431 */
Hanno Becker3307b532017-12-27 21:37:21 +00002432
Hanno Beckera5a2b082019-05-15 14:03:01 +01002433#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002434/* This functions transforms a DTLS plaintext fragment and a record content
2435 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002436 *
2437 * struct {
2438 * opaque content[DTLSPlaintext.length];
2439 * ContentType real_type;
2440 * uint8 zeros[length_of_padding];
2441 * } DTLSInnerPlaintext;
2442 *
2443 * Input:
2444 * - `content`: The beginning of the buffer holding the
2445 * plaintext to be wrapped.
2446 * - `*content_size`: The length of the plaintext in Bytes.
2447 * - `max_len`: The number of Bytes available starting from
2448 * `content`. This must be `>= *content_size`.
2449 * - `rec_type`: The desired record content type.
2450 *
2451 * Output:
2452 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2453 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2454 *
2455 * Returns:
2456 * - `0` on success.
2457 * - A negative error code if `max_len` didn't offer enough space
2458 * for the expansion.
2459 */
2460static int ssl_cid_build_inner_plaintext( unsigned char *content,
2461 size_t *content_size,
2462 size_t remaining,
2463 uint8_t rec_type )
2464{
2465 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002466 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2467 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2468 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Andrzej Kurek84bde412020-07-06 15:27:34 -04002469 volatile unsigned char *content_dup = content;
2470 volatile size_t *content_size_dup = content_size;
2471 volatile size_t remaining_dup = remaining;
2472
Hanno Becker92c930f2019-04-29 17:31:37 +01002473
2474 /* Write real content type */
2475 if( remaining == 0 )
Andrzej Kurek84bde412020-07-06 15:27:34 -04002476 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Hanno Becker92c930f2019-04-29 17:31:37 +01002477 content[ len ] = rec_type;
2478 len++;
2479 remaining--;
2480
2481 if( remaining < pad )
Andrzej Kurek84bde412020-07-06 15:27:34 -04002482 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002483 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002484 len += pad;
2485 remaining -= pad;
2486
2487 *content_size = len;
Andrzej Kurek84bde412020-07-06 15:27:34 -04002488
2489 /* Secure against buffer substitution */
2490 if( content_dup == content && content_size_dup == content_size &&
2491 ( remaining_dup - 1 - pad ) == remaining )
2492 {
2493 return( 0 );
2494 }
2495 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Hanno Becker92c930f2019-04-29 17:31:37 +01002496}
2497
Hanno Becker7dc25772019-05-20 15:08:01 +01002498/* This function parses a DTLSInnerPlaintext structure.
2499 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002500static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2501 size_t *content_size,
2502 uint8_t *rec_type )
2503{
2504 size_t remaining = *content_size;
2505
2506 /* Determine length of padding by skipping zeroes from the back. */
2507 do
2508 {
2509 if( remaining == 0 )
2510 return( -1 );
2511 remaining--;
2512 } while( content[ remaining ] == 0 );
2513
2514 *content_size = remaining;
2515 *rec_type = content[ remaining ];
2516
2517 return( 0 );
2518}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002519#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002520
Hanno Becker99abf512019-05-20 14:50:53 +01002521/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002522 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002523static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002524 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002525 mbedtls_record *rec )
2526{
Hanno Becker99abf512019-05-20 14:50:53 +01002527 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002528 *
2529 * additional_data = seq_num + TLSCompressed.type +
2530 * TLSCompressed.version + TLSCompressed.length;
2531 *
Hanno Becker99abf512019-05-20 14:50:53 +01002532 * For the CID extension, this is extended as follows
2533 * (quoting draft-ietf-tls-dtls-connection-id-05,
2534 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002535 *
2536 * additional_data = seq_num + DTLSPlaintext.type +
2537 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002538 * cid +
2539 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002540 * length_of_DTLSInnerPlaintext;
2541 */
2542
Teppo Järvelin91d79382019-10-02 09:09:31 +03002543 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002544 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002545 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002546
Hanno Beckera5a2b082019-05-15 14:03:01 +01002547#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002548 if( rec->cid_len != 0 )
2549 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002550 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002551 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002552 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2553 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002554 *add_data_len = 13 + 1 + rec->cid_len;
2555 }
2556 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002557#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002558 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002559 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002560 *add_data_len = 13;
2561 }
Hanno Becker3307b532017-12-27 21:37:21 +00002562}
2563
Hanno Becker611a83b2018-01-03 14:27:32 +00002564int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2565 mbedtls_ssl_transform *transform,
2566 mbedtls_record *rec,
2567 int (*f_rng)(void *, unsigned char *, size_t),
2568 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002571 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002572 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002573 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002574 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002575 size_t post_avail;
2576
2577 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002578#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002579 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002580 ((void) ssl);
2581#endif
2582
2583 /* The PRNG is used for dynamic IV generation that's used
2584 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2585#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2586 ( defined(MBEDTLS_AES_C) || \
2587 defined(MBEDTLS_ARIA_C) || \
2588 defined(MBEDTLS_CAMELLIA_C) ) && \
2589 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2590 ((void) f_rng);
2591 ((void) p_rng);
2592#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Hanno Becker3307b532017-12-27 21:37:21 +00002596 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002597 {
Hanno Becker3307b532017-12-27 21:37:21 +00002598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2599 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2600 }
Hanno Becker505089d2019-05-01 09:45:57 +01002601 if( rec == NULL
2602 || rec->buf == NULL
2603 || rec->buf_len < rec->data_offset
2604 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002605#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002606 || rec->cid_len != 0
2607#endif
2608 )
Hanno Becker3307b532017-12-27 21:37:21 +00002609 {
2610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002612 }
2613
Hanno Becker3307b532017-12-27 21:37:21 +00002614 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002615 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002617 data, rec->data_len );
2618
2619 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2620
2621 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2622 {
2623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2624 (unsigned) rec->data_len,
2625 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2627 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002628
Hanno Beckera5a2b082019-05-15 14:03:01 +01002629#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002630 /*
2631 * Add CID information
2632 */
2633 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002634 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2635 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002636 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002637
2638 if( rec->cid_len != 0 )
2639 {
Andrzej Kurek84bde412020-07-06 15:27:34 -04002640 int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Becker92c930f2019-04-29 17:31:37 +01002641 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002642 * Wrap plaintext into DTLSInnerPlaintext structure.
2643 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002644 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002645 * Note that this changes `rec->data_len`, and hence
2646 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002647 */
Andrzej Kurek84bde412020-07-06 15:27:34 -04002648 if( ( ret = ssl_cid_build_inner_plaintext( data,
2649 &rec->data_len,
2650 post_avail,
2651 rec->type ) ) != 0 )
Hanno Becker92c930f2019-04-29 17:31:37 +01002652 {
Andrzej Kurek84bde412020-07-06 15:27:34 -04002653 return( ret );
Hanno Becker92c930f2019-04-29 17:31:37 +01002654 }
2655
2656 rec->type = MBEDTLS_SSL_MSG_CID;
2657 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002658#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002659
Hanno Becker92c930f2019-04-29 17:31:37 +01002660 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2661
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002663 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002665#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( mode == MBEDTLS_MODE_STREAM ||
2667 ( mode == MBEDTLS_MODE_CBC
2668#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002669 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002670#endif
2671 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 {
Hanno Becker3307b532017-12-27 21:37:21 +00002673 if( post_avail < transform->maclen )
2674 {
2675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2676 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2677 }
2678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002680 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2681 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002682 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002683 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002684 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2685 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002686 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002687 }
2688 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002689#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2691 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002692 if( mbedtls_ssl_ver_geq(
2693 mbedtls_ssl_transform_get_minor_ver( transform ),
2694 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002695 {
Hanno Becker992b6872017-11-09 18:57:39 +00002696 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2697
Hanno Beckere83efe62019-04-29 13:52:53 +01002698 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002699
Hanno Becker3307b532017-12-27 21:37:21 +00002700 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002701 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002702 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2703 data, rec->data_len );
2704 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2705 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2706
Teppo Järvelin91d79382019-10-02 09:09:31 +03002707 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002708 }
2709 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002710#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002714 }
2715
Hanno Becker3307b532017-12-27 21:37:21 +00002716 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2717 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718
Hanno Becker3307b532017-12-27 21:37:21 +00002719 rec->data_len += transform->maclen;
2720 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002721 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002722 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002723#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002725 /*
2726 * Encrypt
2727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2729 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002731 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002732 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002734 "including %d bytes of padding",
2735 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Hanno Becker3307b532017-12-27 21:37:21 +00002737 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2738 transform->iv_enc, transform->ivlen,
2739 data, rec->data_len,
2740 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002743 return( ret );
2744 }
2745
Hanno Becker3307b532017-12-27 21:37:21 +00002746 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2749 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002750 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 }
Paul Bakker68884e32013-01-07 18:20:04 +01002752 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002754
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002755#if defined(MBEDTLS_GCM_C) || \
2756 defined(MBEDTLS_CCM_C) || \
2757 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002759 mode == MBEDTLS_MODE_CCM ||
2760 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002761 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002762 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002763 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002764 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002765
Hanno Becker3307b532017-12-27 21:37:21 +00002766 /* Check that there's space for both the authentication tag
2767 * and the explicit IV before and after the record content. */
2768 if( post_avail < transform->taglen ||
2769 rec->data_offset < explicit_iv_len )
2770 {
2771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2772 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2773 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002774
Paul Bakker68884e32013-01-07 18:20:04 +01002775 /*
2776 * Generate IV
2777 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002778 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2779 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002780 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002781 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2782 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002783 explicit_iv_len );
2784 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002785 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002786 }
2787 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2788 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002789 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002790 unsigned char i;
2791
Teppo Järvelin91d79382019-10-02 09:09:31 +03002792 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002793
2794 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002795 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002796 }
2797 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002798 {
2799 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002802 }
2803
Hanno Beckere83efe62019-04-29 13:52:53 +01002804 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002805
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002806 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2807 iv, transform->ivlen );
2808 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002809 data - explicit_iv_len, explicit_iv_len );
2810 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002811 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002813 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002814 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002815
Paul Bakker68884e32013-01-07 18:20:04 +01002816 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002817 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002818 */
Hanno Becker3307b532017-12-27 21:37:21 +00002819
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002820 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002821 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002822 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002823 data, rec->data_len, /* source */
2824 data, &rec->data_len, /* destination */
2825 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002828 return( ret );
2829 }
2830
Hanno Becker3307b532017-12-27 21:37:21 +00002831 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2832 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002833
Hanno Becker3307b532017-12-27 21:37:21 +00002834 rec->data_len += transform->taglen + explicit_iv_len;
2835 rec->data_offset -= explicit_iv_len;
2836 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2841#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002842 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002845 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002846 size_t padlen, i;
2847 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002848
Hanno Becker3307b532017-12-27 21:37:21 +00002849 /* Currently we're always using minimal padding
2850 * (up to 255 bytes would be allowed). */
2851 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2852 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 padlen = 0;
2854
Hanno Becker3307b532017-12-27 21:37:21 +00002855 /* Check there's enough space in the buffer for the padding. */
2856 if( post_avail < padlen + 1 )
2857 {
2858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2859 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2860 }
2861
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002863 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002864
Hanno Becker3307b532017-12-27 21:37:21 +00002865 rec->data_len += padlen + 1;
2866 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002869 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002870 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2871 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002872 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002873 if( mbedtls_ssl_ver_geq(
2874 mbedtls_ssl_transform_get_minor_ver( transform ),
2875 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002876 {
Hanno Becker3307b532017-12-27 21:37:21 +00002877 if( f_rng == NULL )
2878 {
2879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2881 }
2882
2883 if( rec->data_offset < transform->ivlen )
2884 {
2885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2886 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2887 }
2888
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002889 /*
2890 * Generate IV
2891 */
Hanno Becker3307b532017-12-27 21:37:21 +00002892 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002893 if( ret != 0 )
2894 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002895
Teppo Järvelin91d79382019-10-02 09:09:31 +03002896 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002897 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002898
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002899 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002903 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002904 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002905 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Hanno Becker3307b532017-12-27 21:37:21 +00002907 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2908 transform->iv_enc,
2909 transform->ivlen,
2910 data, rec->data_len,
2911 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002914 return( ret );
2915 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002916
Hanno Becker3307b532017-12-27 21:37:21 +00002917 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002921 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002924 if( mbedtls_ssl_ver_lt(
2925 mbedtls_ssl_transform_get_minor_ver( transform ),
2926 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002927 {
2928 /*
2929 * Save IV in SSL3 and TLS1
2930 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002931 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002932 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 }
Hanno Becker3307b532017-12-27 21:37:21 +00002934 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002935#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002936 {
2937 data -= transform->ivlen;
2938 rec->data_offset -= transform->ivlen;
2939 rec->data_len += transform->ivlen;
2940 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002943 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002944 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002945 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2946
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002947 /*
2948 * MAC(MAC_write_key, seq_num +
2949 * TLSCipherText.type +
2950 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002951 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002952 * IV + // except for TLS 1.0
2953 * ENC(content + padding + padding_length));
2954 */
Hanno Becker3307b532017-12-27 21:37:21 +00002955
2956 if( post_avail < transform->maclen)
2957 {
2958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2959 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2960 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002961
Hanno Beckere83efe62019-04-29 13:52:53 +01002962 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002965 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002966 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002967
Hanno Becker3307b532017-12-27 21:37:21 +00002968 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002969 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002970 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2971 data, rec->data_len );
2972 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2973 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002974
Teppo Järvelin91d79382019-10-02 09:09:31 +03002975 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002976
Hanno Becker3307b532017-12-27 21:37:21 +00002977 rec->data_len += transform->maclen;
2978 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002979 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002982 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002983 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002985 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002989 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002991 /* Make extra sure authentication was performed, exactly once */
2992 if( auth_done != 1 )
2993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002996 }
2997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
3000 return( 0 );
3001}
3002
Hanno Becker40478be2019-07-12 08:23:59 +01003003int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00003004 mbedtls_ssl_transform *transform,
3005 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003006{
Hanno Becker4c6876b2017-12-27 21:28:58 +00003007 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003009 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00003010#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01003011 size_t padlen = 0, correct = 1;
3012#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003013 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01003014 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01003015 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003016
Hanno Becker611a83b2018-01-03 14:27:32 +00003017#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02003018 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003019 ((void) ssl);
3020#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003023 if( rec == NULL ||
3024 rec->buf == NULL ||
3025 rec->buf_len < rec->data_offset ||
3026 rec->buf_len - rec->data_offset < rec->data_len )
3027 {
3028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003030 }
3031
Hanno Becker4c6876b2017-12-27 21:28:58 +00003032 data = rec->buf + rec->data_offset;
3033 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
Hanno Beckera5a2b082019-05-15 14:03:01 +01003035#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01003036 /*
3037 * Match record's CID with incoming CID.
3038 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01003039 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03003040 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01003041 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01003042 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01003043 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003044#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
3047 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01003048 {
3049 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003050 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3051 transform->iv_dec,
3052 transform->ivlen,
3053 data, rec->data_len,
3054 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02003057 return( ret );
3058 }
3059
Hanno Becker4c6876b2017-12-27 21:28:58 +00003060 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02003061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02003064 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003065 }
Paul Bakker68884e32013-01-07 18:20:04 +01003066 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003068#if defined(MBEDTLS_GCM_C) || \
3069 defined(MBEDTLS_CCM_C) || \
3070 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003072 mode == MBEDTLS_MODE_CCM ||
3073 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003074 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003075 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003076 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003077
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003078 /*
3079 * Prepare IV from explicit and implicit data.
3080 */
3081
3082 /* Check that there's enough space for the explicit IV
3083 * (at the beginning of the record) and the MAC (at the
3084 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003085 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003088 "+ taglen (%d)", rec->data_len,
3089 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003091 }
Paul Bakker68884e32013-01-07 18:20:04 +01003092
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003093#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003094 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3095 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003096 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003097
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003098 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003099 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003100 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003101 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003102 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003103 else
3104#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3105#if defined(MBEDTLS_CHACHAPOLY_C)
3106 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003107 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003108 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003109 unsigned char i;
3110
Teppo Järvelin91d79382019-10-02 09:09:31 +03003111 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003112
3113 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003114 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003115 }
3116 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003117#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003118 {
3119 /* Reminder if we ever add an AEAD mode with a different size */
3120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3121 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3122 }
3123
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003124 /* Group changes to data, data_len, and add_data, because
3125 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003126 data += explicit_iv_len;
3127 rec->data_offset += explicit_iv_len;
3128 rec->data_len -= explicit_iv_len + transform->taglen;
3129
Hanno Beckere83efe62019-04-29 13:52:53 +01003130 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003131 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003132 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003133
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003134 /* Because of the check above, we know that there are
3135 * explicit_iv_len Bytes preceeding data, and taglen
3136 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003137 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003138 * mbedtls_cipher_auth_decrypt() below. */
3139
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003140 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003141 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003142 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003143
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003144 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003145 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003146 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003147 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3148 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003149 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003150 data, rec->data_len,
3151 data, &olen,
3152 data + rec->data_len,
3153 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3158 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003159
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003160 return( ret );
3161 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003162 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003163
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003164 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003165 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003169 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003171 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3173#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003174 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003177 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003178
Paul Bakker5121ce52009-01-03 21:22:43 +00003179 /*
Paul Bakker45829992013-01-03 14:52:21 +01003180 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003183 if( mbedtls_ssl_ver_geq(
3184 mbedtls_ssl_transform_get_minor_ver( transform ),
3185 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003186 {
3187 /* The ciphertext is prefixed with the CBC IV. */
3188 minlen += transform->ivlen;
3189 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003190#endif
Paul Bakker45829992013-01-03 14:52:21 +01003191
Hanno Becker4c6876b2017-12-27 21:28:58 +00003192 /* Size considerations:
3193 *
3194 * - The CBC cipher text must not be empty and hence
3195 * at least of size transform->ivlen.
3196 *
3197 * Together with the potential IV-prefix, this explains
3198 * the first of the two checks below.
3199 *
3200 * - The record must contain a MAC, either in plain or
3201 * encrypted, depending on whether Encrypt-then-MAC
3202 * is used or not.
3203 * - If it is, the message contains the IV-prefix,
3204 * the CBC ciphertext, and the MAC.
3205 * - If it is not, the padded plaintext, and hence
3206 * the CBC ciphertext, has at least length maclen + 1
3207 * because there is at least the padding length byte.
3208 *
3209 * As the CBC ciphertext is not empty, both cases give the
3210 * lower bound minlen + maclen + 1 on the record size, which
3211 * we test for in the second check below.
3212 */
3213 if( rec->data_len < minlen + transform->ivlen ||
3214 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003217 "+ 1 ) ( + expl IV )", rec->data_len,
3218 transform->ivlen,
3219 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003221 }
3222
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003223 /*
3224 * Authenticate before decrypt if enabled
3225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003227 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003228 {
Hanno Becker992b6872017-11-09 18:57:39 +00003229 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003232
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003233 /* Update data_len in tandem with add_data.
3234 *
3235 * The subtraction is safe because of the previous check
3236 * data_len >= minlen + maclen + 1.
3237 *
3238 * Afterwards, we know that data + data_len is followed by at
3239 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003240 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003241 *
3242 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003243 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003244 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003245
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003246 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003247 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3248 add_data_len );
3249 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3250 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003251 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3252 data, rec->data_len );
3253 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3254 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003255
Hanno Becker4c6876b2017-12-27 21:28:58 +00003256 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3257 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003258 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003259 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003260
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003261 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003262 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003263 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003267 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003268 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003271
3272 /*
3273 * Check length sanity
3274 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003275
3276 /* We know from above that data_len > minlen >= 0,
3277 * so the following check in particular implies that
3278 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003279 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003282 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003284 }
3285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003287 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003288 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003289 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003290 if( mbedtls_ssl_ver_geq(
3291 mbedtls_ssl_transform_get_minor_ver( transform ),
3292 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003293 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003294 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003295 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003296
Hanno Becker4c6876b2017-12-27 21:28:58 +00003297 data += transform->ivlen;
3298 rec->data_offset += transform->ivlen;
3299 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003300 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003302
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003303 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3304
Hanno Becker4c6876b2017-12-27 21:28:58 +00003305 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3306 transform->iv_dec, transform->ivlen,
3307 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003310 return( ret );
3311 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003312
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003313 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003314 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3317 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003318 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003321 if( mbedtls_ssl_ver_lt(
3322 mbedtls_ssl_transform_get_minor_ver( transform ),
3323 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003324 {
3325 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003326 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3327 * records is equivalent to CBC decryption of the concatenation
3328 * of the records; in other words, IVs are maintained across
3329 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003330 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003331 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003332 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003334#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Hanno Becker4c6876b2017-12-27 21:28:58 +00003336 /* Safe since data_len >= minlen + maclen + 1, so after having
3337 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003338 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3339 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003340 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003341
Hanno Becker4c6876b2017-12-27 21:28:58 +00003342 if( auth_done == 1 )
3343 {
3344 correct *= ( rec->data_len >= padlen + 1 );
3345 padlen *= ( rec->data_len >= padlen + 1 );
3346 }
3347 else
Paul Bakker45829992013-01-03 14:52:21 +01003348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003350 if( rec->data_len < transform->maclen + padlen + 1 )
3351 {
3352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3353 rec->data_len,
3354 transform->maclen,
3355 padlen + 1 ) );
3356 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003357#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003358
3359 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3360 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003362
Hanno Becker4c6876b2017-12-27 21:28:58 +00003363 padlen++;
3364
3365 /* Regardless of the validity of the padding,
3366 * we have data_len >= padlen here. */
3367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003368#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003369 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3370 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003371 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003372 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374#if defined(MBEDTLS_SSL_DEBUG_ALL)
3375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003376 "should be no more than %d",
3377 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003378#endif
Paul Bakker45829992013-01-03 14:52:21 +01003379 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 }
3381 }
3382 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3384#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3385 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003386 if( mbedtls_ssl_ver_gt(
3387 mbedtls_ssl_transform_get_minor_ver( transform ),
3388 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003389 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003390 /* The padding check involves a series of up to 256
3391 * consecutive memory reads at the end of the record
3392 * plaintext buffer. In order to hide the length and
3393 * validity of the padding, always perform exactly
3394 * `min(256,plaintext_len)` reads (but take into account
3395 * only the last `padlen` bytes for the padding check). */
3396 size_t pad_count = 0;
3397 size_t real_count = 0;
3398 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003399
Hanno Becker4c6876b2017-12-27 21:28:58 +00003400 /* Index of first padding byte; it has been ensured above
3401 * that the subtraction is safe. */
3402 size_t const padding_idx = rec->data_len - padlen;
3403 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3404 size_t const start_idx = rec->data_len - num_checks;
3405 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003406
Hanno Becker4c6876b2017-12-27 21:28:58 +00003407 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003408 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003409 real_count |= ( idx >= padding_idx );
3410 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003411 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003412 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003415 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003417#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003418 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003420 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3422 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003426 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003427
Hanno Becker4c6876b2017-12-27 21:28:58 +00003428 /* If the padding was found to be invalid, padlen == 0
3429 * and the subtraction is safe. If the padding was found valid,
3430 * padlen hasn't been changed and the previous assertion
3431 * data_len >= padlen still holds. */
3432 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003433 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003434 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003436 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +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é-Gonnardf7dc3782013-09-13 14:10:44 +02003440 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003442#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003444 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003446
3447 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003448 * Authenticate if not done yet.
3449 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003451#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003452 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003453 {
Hanno Becker992b6872017-11-09 18:57:39 +00003454 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003455
Hanno Becker4c6876b2017-12-27 21:28:58 +00003456 /* If the initial value of padlen was such that
3457 * data_len < maclen + padlen + 1, then padlen
3458 * got reset to 1, and the initial check
3459 * data_len >= minlen + maclen + 1
3460 * guarantees that at this point we still
3461 * have at least data_len >= maclen.
3462 *
3463 * If the initial value of padlen was such that
3464 * data_len >= maclen + padlen + 1, then we have
3465 * subtracted either padlen + 1 (if the padding was correct)
3466 * or 0 (if the padding was incorrect) since then,
3467 * hence data_len >= maclen in any case.
3468 */
3469 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003470 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003473 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3474 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003475 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003476 ssl_mac( &transform->md_ctx_dec,
3477 transform->mac_dec,
3478 data, rec->data_len,
3479 rec->ctr, rec->type,
3480 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003481 }
3482 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3484#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3485 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003486 if( mbedtls_ssl_ver_gt(
3487 mbedtls_ssl_transform_get_minor_ver( transform ),
3488 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003489 {
3490 /*
3491 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003492 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003493 *
3494 * Known timing attacks:
3495 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3496 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003497 * To compensate for different timings for the MAC calculation
3498 * depending on how much padding was removed (which is determined
3499 * by padlen), process extra_run more blocks through the hash
3500 * function.
3501 *
3502 * The formula in the paper is
3503 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3504 * where L1 is the size of the header plus the decrypted message
3505 * plus CBC padding and L2 is the size of the header plus the
3506 * decrypted message. This is for an underlying hash function
3507 * with 64-byte blocks.
3508 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3509 * correctly. We round down instead of up, so -56 is the correct
3510 * value for our calculations instead of -55.
3511 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003512 * Repeat the formula rather than defining a block_size variable.
3513 * This avoids requiring division by a variable at runtime
3514 * (which would be marginally less efficient and would require
3515 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003516 */
3517 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003518 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003519
3520 /*
3521 * The next two sizes are the minimum and maximum values of
3522 * in_msglen over all padlen values.
3523 *
3524 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003525 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003526 *
3527 * Note that max_len + maclen is never more than the buffer
3528 * length, as we previously did in_msglen -= maclen too.
3529 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003530 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003531 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3532
Hanno Becker4c6876b2017-12-27 21:28:58 +00003533 memset( tmp, 0, sizeof( tmp ) );
3534
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003535 switch( mbedtls_md_get_type(
3536 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003537 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003538#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3539 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003540 case MBEDTLS_MD_MD5:
3541 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003542 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003543 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003544 extra_run =
3545 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3546 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003547 break;
3548#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003549#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003550 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003551 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003552 extra_run =
3553 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3554 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003555 break;
3556#endif
3557 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3560 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003561
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003562 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003563
Hanno Beckere83efe62019-04-29 13:52:53 +01003564 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3565 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003566 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3567 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003568 /* Make sure we access everything even when padlen > 0. This
3569 * makes the synchronisation requirements for just-in-time
3570 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003571 ssl_read_memory( data + rec->data_len, padlen );
3572 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003573
3574 /* Call mbedtls_md_process at least once due to cache attacks
3575 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003576 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003577 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003578
Hanno Becker4c6876b2017-12-27 21:28:58 +00003579 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003580
3581 /* Make sure we access all the memory that could contain the MAC,
3582 * before we check it in the next code block. This makes the
3583 * synchronisation requirements for just-in-time Prime+Probe
3584 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003585 ssl_read_memory( data + min_len,
3586 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003587 }
3588 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3590 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3593 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003595
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003596#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003597 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3598 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003600
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003601 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003602 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604#if defined(MBEDTLS_SSL_DEBUG_ALL)
3605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003606#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003607 correct = 0;
3608 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003609 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003610 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003611
3612 /*
3613 * Finally check the correct flag
3614 */
3615 if( correct == 0 )
3616 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003617#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003618
3619 /* Make extra sure authentication was performed, exactly once */
3620 if( auth_done != 1 )
3621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003625
Hanno Beckera5a2b082019-05-15 14:03:01 +01003626#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003627 if( rec->cid_len != 0 )
3628 {
3629 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3630 &rec->type );
3631 if( ret != 0 )
3632 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3633 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003634#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
3638 return( 0 );
3639}
3640
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003641#undef MAC_NONE
3642#undef MAC_PLAINTEXT
3643#undef MAC_CIPHERTEXT
3644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003646/*
3647 * Compression/decompression functions
3648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003650{
3651 int ret;
3652 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003653 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003654 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003655 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003658
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003659 if( len_pre == 0 )
3660 return( 0 );
3661
Teppo Järvelin91d79382019-10-02 09:09:31 +03003662 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003665 ssl->out_msglen ) );
3666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003668 ssl->out_msg, ssl->out_msglen );
3669
Paul Bakker48916f92012-09-16 19:57:18 +00003670 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3671 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3672 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003673 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003674
Paul Bakker48916f92012-09-16 19:57:18 +00003675 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003676 if( ret != Z_OK )
3677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3679 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003680 }
3681
Angus Grattond8213d02016-05-25 20:56:48 +10003682 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003683 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003686 ssl->out_msglen ) );
3687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003689 ssl->out_msg, ssl->out_msglen );
3690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003692
3693 return( 0 );
3694}
3695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003697{
3698 int ret;
3699 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003700 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003701 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003702 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003705
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003706 if( len_pre == 0 )
3707 return( 0 );
3708
Teppo Järvelin91d79382019-10-02 09:09:31 +03003709 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003712 ssl->in_msglen ) );
3713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003715 ssl->in_msg, ssl->in_msglen );
3716
Paul Bakker48916f92012-09-16 19:57:18 +00003717 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3718 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3719 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003720 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003721 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003722
Paul Bakker48916f92012-09-16 19:57:18 +00003723 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003724 if( ret != Z_OK )
3725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3727 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003728 }
3729
Angus Grattond8213d02016-05-25 20:56:48 +10003730 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003731 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003734 ssl->in_msglen ) );
3735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003736 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003737 ssl->in_msg, ssl->in_msglen );
3738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003740
3741 return( 0 );
3742}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3746static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748#if defined(MBEDTLS_SSL_PROTO_DTLS)
3749static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003750{
3751 /* If renegotiation is not enforced, retransmit until we would reach max
3752 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003753 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003754 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003755 uint32_t ratio =
3756 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3757 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003758 unsigned char doublings = 1;
3759
3760 while( ratio != 0 )
3761 {
3762 ++doublings;
3763 ratio >>= 1;
3764 }
3765
3766 if( ++ssl->renego_records_seen > doublings )
3767 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003769 return( 0 );
3770 }
3771 }
3772
3773 return( ssl_write_hello_request( ssl ) );
3774}
3775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003777
Paul Bakker5121ce52009-01-03 21:22:43 +00003778/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003779 * Fill the input message buffer by appending data to it.
3780 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003781 *
3782 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3783 * available (from this read and/or a previous one). Otherwise, an error code
3784 * is returned (possibly EOF or WANT_READ).
3785 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003786 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3787 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3788 * since we always read a whole datagram at once.
3789 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003790 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003791 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003793int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003794{
Paul Bakker23986e52011-04-24 08:57:21 +00003795 int ret;
3796 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003799
Hanno Beckera58a8962019-06-13 16:11:15 +01003800 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3801 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003804 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003806 }
3807
Angus Grattond8213d02016-05-25 20:56:48 +10003808 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003812 }
3813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003815 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003817 uint32_t timeout;
3818
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003819 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003820 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3821 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003822 {
3823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3824 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3826 }
3827
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003828 /*
3829 * The point is, we need to always read a full datagram at once, so we
3830 * sometimes read more then requested, and handle the additional data.
3831 * It could be the rest of the current record (while fetching the
3832 * header) and/or some other records in the same datagram.
3833 */
3834
3835 /*
3836 * Move to the next record in the already read datagram if applicable
3837 */
3838 if( ssl->next_record_offset != 0 )
3839 {
3840 if( ssl->in_left < ssl->next_record_offset )
3841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3843 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003844 }
3845
3846 ssl->in_left -= ssl->next_record_offset;
3847
3848 if( ssl->in_left != 0 )
3849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003851 ssl->next_record_offset ) );
3852 memmove( ssl->in_hdr,
3853 ssl->in_hdr + ssl->next_record_offset,
3854 ssl->in_left );
3855 }
3856
3857 ssl->next_record_offset = 0;
3858 }
3859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003861 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003862
3863 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003864 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003865 */
3866 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003869 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003870 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003871
3872 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003873 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003874 * are not at the beginning of a new record, the caller did something
3875 * wrong.
3876 */
3877 if( ssl->in_left != 0 )
3878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3880 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003881 }
3882
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003883 /*
3884 * Don't even try to read if time's out already.
3885 * This avoids by-passing the timer when repeatedly receiving messages
3886 * that will end up being dropped.
3887 */
3888 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003889 {
3890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003891 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003892 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003893 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003894 {
Angus Grattond8213d02016-05-25 20:56:48 +10003895 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003898 timeout = ssl->handshake->retransmit_timeout;
3899 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003900 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003903
Hanno Beckera58a8962019-06-13 16:11:15 +01003904 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3905 {
3906 ret = mbedtls_ssl_get_recv_timeout( ssl )
3907 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3908 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003909 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003910 {
3911 ret = mbedtls_ssl_get_recv( ssl )
3912 ( ssl->p_bio, ssl->in_hdr, len );
3913 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003916
3917 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003919 }
3920
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003921 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003924 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003927 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003928 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003931 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003932 }
3933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003937 return( ret );
3938 }
3939
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003940 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003941 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003943 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3944 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003946 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003947 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003950 return( ret );
3951 }
3952
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003953 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003954 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003956 }
3957
Paul Bakker5121ce52009-01-03 21:22:43 +00003958 if( ret < 0 )
3959 return( ret );
3960
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003961 ssl->in_left = ret;
3962 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003963 MBEDTLS_SSL_TRANSPORT_ELSE
3964#endif /* MBEDTLS_SSL_PROTO_DTLS */
3965#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003968 ssl->in_left, nb_want ) );
3969
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003970 while( ssl->in_left < nb_want )
3971 {
3972 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003973
3974 if( ssl_check_timer( ssl ) != 0 )
3975 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3976 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003977 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003978 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003979 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003980 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3981 ssl->in_hdr + ssl->in_left, len,
3982 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003983 }
3984 else
3985 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003986 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003987 ssl->in_hdr + ssl->in_left, len );
3988 }
3989 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003992 ssl->in_left, nb_want ) );
3993 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003994
3995 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003997
3998 if( ret < 0 )
3999 return( ret );
4000
mohammad160352aecb92018-03-28 23:41:40 -07004001 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08004002 {
Darryl Green11999bb2018-03-13 15:22:58 +00004003 MBEDTLS_SSL_DEBUG_MSG( 1,
4004 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07004005 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08004006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4007 }
4008
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01004009 ssl->in_left += ret;
4010 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01004012#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
4016 return( 0 );
4017}
4018
4019/*
4020 * Flush any data not yet written
4021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004023{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004024 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01004025 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00004026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004028
Hanno Beckera58a8962019-06-13 16:11:15 +01004029 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01004032 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004034 }
4035
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004036 /* Avoid incrementing counter if data is flushed */
4037 if( ssl->out_left == 0 )
4038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004040 return( 0 );
4041 }
4042
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 while( ssl->out_left > 0 )
4044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01004046 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
Hanno Becker2b1e3542018-08-06 11:19:13 +01004048 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01004049 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00004050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004052
4053 if( ret <= 0 )
4054 return( ret );
4055
mohammad160352aecb92018-03-28 23:41:40 -07004056 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08004057 {
Darryl Green11999bb2018-03-13 15:22:58 +00004058 MBEDTLS_SSL_DEBUG_MSG( 1,
4059 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07004060 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08004061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4062 }
4063
Paul Bakker5121ce52009-01-03 21:22:43 +00004064 ssl->out_left -= ret;
4065 }
4066
Hanno Becker2b1e3542018-08-06 11:19:13 +01004067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004068 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004069 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004070 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004071 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004072 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01004073#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004074#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01004075 {
4076 ssl->out_hdr = ssl->out_buf + 8;
4077 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004078#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004079 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004082
4083 return( 0 );
4084}
4085
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004086/*
4087 * Functions to handle the DTLS retransmission state machine
4088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004090/*
4091 * Append current handshake message to current outgoing flight
4092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004095 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4097 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4098 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004099
4100 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004101 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004102 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004105 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004106 }
4107
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004108 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004109 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004113 }
4114
4115 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004116 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004117 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004118 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004119 msg->next = NULL;
4120
4121 /* Append to the current flight */
4122 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004123 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004124 else
4125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004127 while( cur->next != NULL )
4128 cur = cur->next;
4129 cur->next = msg;
4130 }
4131
Hanno Becker3b235902018-08-06 09:54:53 +01004132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004133 return( 0 );
4134}
4135
4136/*
4137 * Free the current flight of handshake messages
4138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141 mbedtls_ssl_flight_item *cur = flight;
4142 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004143
4144 while( cur != NULL )
4145 {
4146 next = cur->next;
4147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 mbedtls_free( cur->p );
4149 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004150
4151 cur = next;
4152 }
4153}
4154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4156static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004157#endif
4158
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004159/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004160 * Swap transform_out and out_ctr with the alternative ones
4161 */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004162static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004165 unsigned char tmp_out_ctr[8];
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004166#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4167 int ret;
4168#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004169
4170 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004173 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004174 }
4175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004177
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004178 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004179 tmp_transform = ssl->transform_out;
4180 ssl->transform_out = ssl->handshake->alt_transform_out;
4181 ssl->handshake->alt_transform_out = tmp_transform;
4182
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004183 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004184 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4185 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4186 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004187
4188 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004189 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4192 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4197 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004198 }
4199 }
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004200#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
4201
4202 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004203}
4204
4205/*
4206 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004207 */
4208int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4209{
4210 int ret = 0;
4211
4212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4213
4214 ret = mbedtls_ssl_flight_transmit( ssl );
4215
4216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4217
4218 return( ret );
4219}
4220
4221/*
4222 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004223 *
4224 * Need to remember the current message in case flush_output returns
4225 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004226 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004227 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004228int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004229{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004230 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004234 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004236
4237 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004238 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004239 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4240 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004243 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004244
4245 while( ssl->handshake->cur_msg != NULL )
4246 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004247 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004248 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004249
Hanno Beckere1dcb032018-08-17 16:47:58 +01004250 int const is_finished =
4251 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4252 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4253
Hanno Becker04da1892018-08-14 13:22:10 +01004254 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4255 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4256
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004257 /* Swap epochs before sending Finished: we can't do it after
4258 * sending ChangeCipherSpec, in case write returns WANT_READ.
4259 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004260 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004261 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004263 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4264 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004265 }
4266
Hanno Becker67bc7c32018-08-06 11:33:50 +01004267 ret = ssl_get_remaining_payload_in_datagram( ssl );
4268 if( ret < 0 )
4269 return( ret );
4270 max_frag_len = (size_t) ret;
4271
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004272 /* CCS is copied as is, while HS messages may need fragmentation */
4273 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4274 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004275 if( max_frag_len == 0 )
4276 {
4277 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4278 return( ret );
4279
4280 continue;
4281 }
4282
Teppo Järvelin91d79382019-10-02 09:09:31 +03004283 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004284 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004285 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004286
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004287 /* Update position inside current message */
4288 ssl->handshake->cur_msg_p += cur->len;
4289 }
4290 else
4291 {
4292 const unsigned char * const p = ssl->handshake->cur_msg_p;
4293 const size_t hs_len = cur->len - 12;
4294 const size_t frag_off = p - ( cur->p + 12 );
4295 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004296 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004297
Hanno Beckere1dcb032018-08-17 16:47:58 +01004298 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004299 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004300 if( is_finished )
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004301 {
4302 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4303 return( ret );
4304 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004305
Hanno Becker67bc7c32018-08-06 11:33:50 +01004306 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4307 return( ret );
4308
4309 continue;
4310 }
4311 max_hs_frag_len = max_frag_len - 12;
4312
4313 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4314 max_hs_frag_len : rem_len;
4315
4316 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004317 {
4318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004319 (unsigned) cur_hs_frag_len,
4320 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004321 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004322
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004323 /* Messages are stored with handshake headers as if not fragmented,
4324 * copy beginning of headers then fill fragmentation fields.
4325 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004326 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004327
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004328 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4329 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4330 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004331
4332 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4333
Hanno Becker3f7b9732018-08-28 09:53:25 +01004334 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004335 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004336 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004337 ssl->out_msgtype = cur->type;
4338
4339 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004340 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004341 }
4342
4343 /* If done with the current message move to the next one if any */
4344 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4345 {
4346 if( cur->next != NULL )
4347 {
4348 ssl->handshake->cur_msg = cur->next;
4349 ssl->handshake->cur_msg_p = cur->next->p + 12;
4350 }
4351 else
4352 {
4353 ssl->handshake->cur_msg = NULL;
4354 ssl->handshake->cur_msg_p = NULL;
4355 }
4356 }
4357
4358 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004359 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004362 return( ret );
4363 }
4364 }
4365
Hanno Becker67bc7c32018-08-06 11:33:50 +01004366 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4367 return( ret );
4368
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004369 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4371 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004372 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004375 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4376 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004377
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004379
4380 return( 0 );
4381}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004382
4383/*
4384 * To be called when the last message of an incoming flight is received.
4385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004387{
4388 /* We won't need to resend that one any more */
4389 ssl_flight_free( ssl->handshake->flight );
4390 ssl->handshake->flight = NULL;
4391 ssl->handshake->cur_msg = NULL;
4392
4393 /* The next incoming flight will start with this msg_seq */
4394 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4395
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004396 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004397 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004398
Hanno Becker0271f962018-08-16 13:23:47 +01004399 /* Clear future message buffering structure. */
4400 ssl_buffering_free( ssl );
4401
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004402 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004403 ssl_set_timer( ssl, 0 );
4404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4406 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004409 }
4410 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004412}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004413
4414/*
4415 * To be called when the last message of an outgoing flight is send.
4416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004417void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004418{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004419 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004420 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4423 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004426 }
4427 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004431
Paul Bakker5121ce52009-01-03 21:22:43 +00004432/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004433 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004435
4436/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004437 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004438 *
4439 * - fill in handshake headers
4440 * - update handshake checksum
4441 * - DTLS: save message for resending
4442 * - then pass to the record layer
4443 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004444 * DTLS: except for HelloRequest, messages are only queued, and will only be
4445 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004446 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004447 * Inputs:
4448 * - ssl->out_msglen: 4 + actual handshake message len
4449 * (4 is the size of handshake headers for TLS)
4450 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4451 * - ssl->out_msg + 4: the handshake message body
4452 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004453 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004454 * - ssl->out_msglen: the length of the record contents
4455 * (including handshake headers but excluding record headers)
4456 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004457 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004458int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004459{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004460 int ret;
4461 const size_t hs_len = ssl->out_msglen - 4;
4462 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004463
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4465
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004466 /*
4467 * Sanity checks
4468 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004469 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004470 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4471 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004472 /* In SSLv3, the client might send a NoCertificate alert. */
4473#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004474 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004475 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004476 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4477 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004478#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4479 {
4480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4481 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4482 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004484
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004485 /* Whenever we send anything different from a
4486 * HelloRequest we should be in a handshake - double check. */
4487 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4488 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004489 ssl->handshake == NULL )
4490 {
4491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4492 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4493 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004495#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004496 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004497 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004499 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004502 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004503#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004504
Hanno Beckerb50a2532018-08-06 11:52:54 +01004505 /* Double-check that we did not exceed the bounds
4506 * of the outgoing record buffer.
4507 * This should never fail as the various message
4508 * writing functions must obey the bounds of the
4509 * outgoing record buffer, but better be safe.
4510 *
4511 * Note: We deliberately do not check for the MTU or MFL here.
4512 */
4513 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4514 {
4515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4516 "size %u, maximum %u",
4517 (unsigned) ssl->out_msglen,
4518 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4520 }
4521
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004522 /*
4523 * Fill handshake headers
4524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004526 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004527 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004528
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004529 /*
4530 * DTLS has additional fields in the Handshake layer,
4531 * between the length field and the actual payload:
4532 * uint16 message_seq;
4533 * uint24 fragment_offset;
4534 * uint24 fragment_length;
4535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004537 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004538 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004539 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004540 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004541 {
4542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4543 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004544 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004545 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4547 }
4548
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004549 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004550 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004551
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004552 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004553 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004554 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004555 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4556 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004557 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004558 }
4559 else
4560 {
4561 ssl->out_msg[4] = 0;
4562 ssl->out_msg[5] = 0;
4563 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004564
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004565 /* Handshake hashes are computed without fragmentation,
4566 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004567 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004568 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004569 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004571
Hanno Becker0207e532018-08-28 10:28:28 +01004572 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004573 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004574 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004575 }
4576
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004577 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004579 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004580 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4581 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004582 {
4583 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004586 return( ret );
4587 }
4588 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004589 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004590#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004591 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004592 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004593 {
4594 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4595 return( ret );
4596 }
4597 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004598
4599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4600
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004601 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004602}
4603
4604/*
4605 * Record layer functions
4606 */
4607
4608/*
4609 * Write current record.
4610 *
4611 * Uses:
4612 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4613 * - ssl->out_msglen: length of the record content (excl headers)
4614 * - ssl->out_msg: record content
4615 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004616int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004617{
4618 int ret, done = 0;
4619 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004620 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004621
4622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004625 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004627 {
4628 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004631 return( ret );
4632 }
4633
4634 len = ssl->out_msglen;
4635 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4639 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 ret = mbedtls_ssl_hw_record_write( ssl );
4644 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4647 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004648 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004649
4650 if( ret == 0 )
4651 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004653#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004654 if( !done )
4655 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004656 unsigned i;
4657 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004658 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004659
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004660 /* Skip writing the record content type to after the encryption,
4661 * as it may change when using the CID extension. */
4662
Hanno Becker2881d802019-05-22 14:44:53 +01004663 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4664 mbedtls_ssl_get_minor_ver( ssl ),
4665 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004666
Teppo Järvelin91d79382019-10-02 09:09:31 +03004667 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004668 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004669
Paul Bakker48916f92012-09-16 19:57:18 +00004670 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004671 {
Hanno Becker3307b532017-12-27 21:37:21 +00004672 mbedtls_record rec;
4673
4674 rec.buf = ssl->out_iv;
4675 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4676 ( ssl->out_iv - ssl->out_buf );
4677 rec.data_len = ssl->out_msglen;
4678 rec.data_offset = ssl->out_msg - rec.buf;
4679
Teppo Järvelin91d79382019-10-02 09:09:31 +03004680 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004681 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4682 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004683 ssl->conf->transport, rec.ver );
4684 rec.type = ssl->out_msgtype;
4685
Hanno Beckera5a2b082019-05-15 14:03:01 +01004686#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004687 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004688 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004689#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004690
Hanno Becker611a83b2018-01-03 14:27:32 +00004691 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004692 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004693 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004695 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004696 return( ret );
4697 }
4698
Hanno Becker3307b532017-12-27 21:37:21 +00004699 if( rec.data_offset != 0 )
4700 {
4701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4703 }
4704
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004705 /* Update the record content type and CID. */
4706 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004707#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004708 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4709 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004710#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004711 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004712 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004713 encrypted_fi = 1;
4714 }
4715
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004716 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004717 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4718 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004719 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004720 }
4721
Hanno Becker43395762019-05-03 14:46:38 +01004722 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004723
4724#if defined(MBEDTLS_SSL_PROTO_DTLS)
4725 /* In case of DTLS, double-check that we don't exceed
4726 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004727 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004728 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004729 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004730 if( ret < 0 )
4731 return( ret );
4732
4733 if( protected_record_size > (size_t) ret )
4734 {
4735 /* Should never happen */
4736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4737 }
4738 }
4739#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004740
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004741 /* Now write the potentially updated record content type. */
4742 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004744 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004745 "version = [%d:%d], msglen = %d",
4746 ssl->out_hdr[0], ssl->out_hdr[1],
4747 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004750 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004751
4752 ssl->out_left += protected_record_size;
4753 ssl->out_hdr += protected_record_size;
4754 ssl_update_out_pointers( ssl, ssl->transform_out );
4755
Hanno Becker04484622018-08-06 09:49:38 +01004756 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4757 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4758 break;
4759
4760 /* The loop goes to its end iff the counter is wrapping */
4761 if( i == ssl_ep_len( ssl ) )
4762 {
4763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4764 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4765 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004766 }
4767
Hanno Becker67bc7c32018-08-06 11:33:50 +01004768#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004769 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004770 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004771 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004772 size_t remaining;
4773 ret = ssl_get_remaining_payload_in_datagram( ssl );
4774 if( ret < 0 )
4775 {
4776 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4777 ret );
4778 return( ret );
4779 }
4780
4781 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004782 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004783 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004784 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004785 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004786 else
4787 {
Hanno Becker513815a2018-08-20 11:56:09 +01004788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004789 }
4790 }
4791#endif /* MBEDTLS_SSL_PROTO_DTLS */
4792
4793 if( ( flush == SSL_FORCE_FLUSH ) &&
4794 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 return( ret );
4798 }
4799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004801
4802 return( 0 );
4803}
4804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004806
4807static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4808{
4809 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004810 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4811 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004812 {
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04004813 return( PROPER_HS_FRAGMENT );
Hanno Beckere25e3b72018-08-16 09:30:53 +01004814 }
4815 return( 0 );
4816}
Hanno Becker44650b72018-08-16 12:51:11 +01004817
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004818static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004819{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004820 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004821}
4822
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004823static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004824{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004825 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004826}
4827
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004828static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004829{
4830 uint32_t msg_len, frag_off, frag_len;
4831
4832 msg_len = ssl_get_hs_total_len( ssl );
4833 frag_off = ssl_get_hs_frag_off( ssl );
4834 frag_len = ssl_get_hs_frag_len( ssl );
4835
4836 if( frag_off > msg_len )
4837 return( -1 );
4838
4839 if( frag_len > msg_len - frag_off )
4840 return( -1 );
4841
4842 if( frag_len + 12 > ssl->in_msglen )
4843 return( -1 );
4844
4845 return( 0 );
4846}
4847
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004848/*
4849 * Mark bits in bitmask (used for DTLS HS reassembly)
4850 */
4851static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4852{
4853 unsigned int start_bits, end_bits;
4854
4855 start_bits = 8 - ( offset % 8 );
4856 if( start_bits != 8 )
4857 {
4858 size_t first_byte_idx = offset / 8;
4859
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004860 /* Special case */
4861 if( len <= start_bits )
4862 {
4863 for( ; len != 0; len-- )
4864 mask[first_byte_idx] |= 1 << ( start_bits - len );
4865
4866 /* Avoid potential issues with offset or len becoming invalid */
4867 return;
4868 }
4869
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004870 offset += start_bits; /* Now offset % 8 == 0 */
4871 len -= start_bits;
4872
4873 for( ; start_bits != 0; start_bits-- )
4874 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4875 }
4876
4877 end_bits = len % 8;
4878 if( end_bits != 0 )
4879 {
4880 size_t last_byte_idx = ( offset + len ) / 8;
4881
4882 len -= end_bits; /* Now len % 8 == 0 */
4883
4884 for( ; end_bits != 0; end_bits-- )
4885 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4886 }
4887
4888 memset( mask + offset / 8, 0xFF, len / 8 );
4889}
4890
4891/*
4892 * Check that bitmask is full
4893 */
4894static int ssl_bitmask_check( unsigned char *mask, size_t len )
4895{
4896 size_t i;
4897
4898 for( i = 0; i < len / 8; i++ )
4899 if( mask[i] != 0xFF )
Andrzej Kurek84bde412020-07-06 15:27:34 -04004900 return( 0x75555555 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004901
4902 for( i = 0; i < len % 8; i++ )
4903 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
Andrzej Kurek84bde412020-07-06 15:27:34 -04004904 return( 0x75555555 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004905
Andrzej Kurek84bde412020-07-06 15:27:34 -04004906 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004907}
4908
Hanno Becker56e205e2018-08-16 09:06:12 +01004909/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004910static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004911 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004912{
Hanno Becker56e205e2018-08-16 09:06:12 +01004913 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004914
Hanno Becker56e205e2018-08-16 09:06:12 +01004915 alloc_len = 12; /* Handshake header */
4916 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004917
Hanno Beckerd07df862018-08-16 09:14:58 +01004918 if( add_bitmap )
4919 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004920
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004921 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004922}
Hanno Becker56e205e2018-08-16 09:06:12 +01004923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004925
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004926static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004927{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004928 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004929}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004930
Simon Butcher99000142016-10-13 17:21:01 +01004931int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004936 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004938 }
4939
Hanno Becker12555c62018-08-16 12:47:53 +01004940 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004943 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004944 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004947 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004948 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004949 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004950 unsigned int recv_msg_seq = (unsigned int)
4951 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004952
Hanno Becker44650b72018-08-16 12:51:11 +01004953 if( ssl_check_hs_header( ssl ) != 0 )
4954 {
4955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4956 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4957 }
4958
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004959 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004960 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4961 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4962 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4963 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004964 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004965 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4966 {
4967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4968 recv_msg_seq,
4969 ssl->handshake->in_msg_seq ) );
4970 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4971 }
4972
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004973 /* Retransmit only on last message from previous flight, to avoid
4974 * too many retransmissions.
4975 * Besides, No sane server ever retransmits HelloVerifyRequest */
4976 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004977 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004980 "message_seq = %d, start_of_flight = %d",
4981 recv_msg_seq,
4982 ssl->handshake->in_flight_start_seq ) );
4983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004987 return( ret );
4988 }
4989 }
4990 else
4991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004993 "message_seq = %d, expected = %d",
4994 recv_msg_seq,
4995 ssl->handshake->in_msg_seq ) );
4996 }
4997
Hanno Becker90333da2017-10-10 11:27:13 +01004998 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004999 }
5000 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02005001
Hanno Becker6d97ef52018-08-16 13:09:04 +01005002 /* Message reassembly is handled alongside buffering of future
5003 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01005004 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01005005 * handshake logic layer. */
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04005006 if( ssl_hs_is_proper_fragment( ssl ) == PROPER_HS_FRAGMENT )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02005007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01005009 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02005010 }
5011 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005012 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005014#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02005015 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005016 /* With TLS we don't handle fragmentation (for now) */
5017 if( ssl->in_msglen < ssl->in_hslen )
5018 {
5019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
5020 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5021 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005022 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02005023#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005024
Simon Butcher99000142016-10-13 17:21:01 +01005025 return( 0 );
5026}
5027
5028void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
5029{
Hanno Becker0271f962018-08-16 13:23:47 +01005030 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01005031
Hanno Becker0271f962018-08-16 13:23:47 +01005032 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01005033 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005034
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005035 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005037 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005038 ssl->handshake != NULL )
5039 {
Hanno Becker0271f962018-08-16 13:23:47 +01005040 unsigned offset;
5041 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01005042
Hanno Becker0271f962018-08-16 13:23:47 +01005043 /* Increment handshake sequence number */
5044 hs->in_msg_seq++;
5045
5046 /*
5047 * Clear up handshake buffering and reassembly structure.
5048 */
5049
5050 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01005051 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01005052
5053 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01005054 for( offset = 0, hs_buf = &hs->buffering.hs[0];
5055 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01005056 offset++, hs_buf++ )
5057 {
5058 *hs_buf = *(hs_buf + 1);
5059 }
5060
5061 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02005062 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005063 }
5064#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005065}
5066
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005067/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005068 * DTLS anti-replay: RFC 6347 4.1.2.6
5069 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005070 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
5071 * Bit n is set iff record number in_window_top - n has been seen.
5072 *
5073 * Usually, in_window_top is the last record number seen and the lsb of
5074 * in_window is set. The only exception is the initial state (record number 0
5075 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5078static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005079{
5080 ssl->in_window_top = 0;
5081 ssl->in_window = 0;
5082}
5083
5084static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5085{
5086 return( ( (uint64_t) buf[0] << 40 ) |
5087 ( (uint64_t) buf[1] << 32 ) |
5088 ( (uint64_t) buf[2] << 24 ) |
5089 ( (uint64_t) buf[3] << 16 ) |
5090 ( (uint64_t) buf[4] << 8 ) |
5091 ( (uint64_t) buf[5] ) );
5092}
5093
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005094static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5095{
5096 int ret;
5097 unsigned char *original_in_ctr;
5098
5099 // save original in_ctr
5100 original_in_ctr = ssl->in_ctr;
5101
5102 // use counter from record
5103 ssl->in_ctr = record_in_ctr;
5104
5105 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5106
5107 // restore the counter
5108 ssl->in_ctr = original_in_ctr;
5109
5110 return ret;
5111}
5112
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005113/*
5114 * Return 0 if sequence number is acceptable, -1 otherwise
5115 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005116int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005117{
5118 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5119 uint64_t bit;
5120
Hanno Becker7f376f42019-06-12 16:20:48 +01005121 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5122 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5123 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005124 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005125 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005126
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005127 if( rec_seqnum > ssl->in_window_top )
5128 return( 0 );
5129
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005130 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005131
5132 if( bit >= 64 )
5133 return( -1 );
5134
5135 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5136 return( -1 );
5137
5138 return( 0 );
5139}
5140
5141/*
5142 * Update replay window on new validated record
5143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005145{
5146 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5147
Hanno Becker7f376f42019-06-12 16:20:48 +01005148 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5149 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5150 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005151 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005152 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005153
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005154 if( rec_seqnum > ssl->in_window_top )
5155 {
5156 /* Update window_top and the contents of the window */
5157 uint64_t shift = rec_seqnum - ssl->in_window_top;
5158
5159 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005160 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005161 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005162 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005163 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005164 ssl->in_window |= 1;
5165 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005166
5167 ssl->in_window_top = rec_seqnum;
5168 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005169 else
5170 {
5171 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005172 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005173
5174 if( bit < 64 ) /* Always true, but be extra sure */
5175 ssl->in_window |= (uint64_t) 1 << bit;
5176 }
5177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005179
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005180#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005181/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005182static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5183
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005184/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005185 * Without any SSL context, check if a datagram looks like a ClientHello with
5186 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005187 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005188 *
5189 * - if cookie is valid, return 0
5190 * - if ClientHello looks superficially valid but cookie is not,
5191 * fill obuf and set olen, then
5192 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5193 * - otherwise return a specific error code
5194 */
5195static int ssl_check_dtls_clihlo_cookie(
5196 mbedtls_ssl_cookie_write_t *f_cookie_write,
5197 mbedtls_ssl_cookie_check_t *f_cookie_check,
5198 void *p_cookie,
5199 const unsigned char *cli_id, size_t cli_id_len,
5200 const unsigned char *in, size_t in_len,
5201 unsigned char *obuf, size_t buf_len, size_t *olen )
5202{
5203 size_t sid_len, cookie_len;
5204 unsigned char *p;
5205
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005206 /*
5207 * Structure of ClientHello with record and handshake headers,
5208 * and expected values. We don't need to check a lot, more checks will be
5209 * done when actually parsing the ClientHello - skipping those checks
5210 * avoids code duplication and does not make cookie forging any easier.
5211 *
5212 * 0-0 ContentType type; copied, must be handshake
5213 * 1-2 ProtocolVersion version; copied
5214 * 3-4 uint16 epoch; copied, must be 0
5215 * 5-10 uint48 sequence_number; copied
5216 * 11-12 uint16 length; (ignored)
5217 *
5218 * 13-13 HandshakeType msg_type; (ignored)
5219 * 14-16 uint24 length; (ignored)
5220 * 17-18 uint16 message_seq; copied
5221 * 19-21 uint24 fragment_offset; copied, must be 0
5222 * 22-24 uint24 fragment_length; (ignored)
5223 *
5224 * 25-26 ProtocolVersion client_version; (ignored)
5225 * 27-58 Random random; (ignored)
5226 * 59-xx SessionID session_id; 1 byte len + sid_len content
5227 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5228 * ...
5229 *
5230 * Minimum length is 61 bytes.
5231 */
5232 if( in_len < 61 ||
5233 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5234 in[3] != 0 || in[4] != 0 ||
5235 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5236 {
5237 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5238 }
5239
5240 sid_len = in[59];
5241 if( sid_len > in_len - 61 )
5242 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5243
5244 cookie_len = in[60 + sid_len];
5245 if( cookie_len > in_len - 60 )
5246 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5247
5248 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5249 cli_id, cli_id_len ) == 0 )
5250 {
5251 /* Valid cookie */
5252 return( 0 );
5253 }
5254
5255 /*
5256 * If we get here, we've got an invalid cookie, let's prepare HVR.
5257 *
5258 * 0-0 ContentType type; copied
5259 * 1-2 ProtocolVersion version; copied
5260 * 3-4 uint16 epoch; copied
5261 * 5-10 uint48 sequence_number; copied
5262 * 11-12 uint16 length; olen - 13
5263 *
5264 * 13-13 HandshakeType msg_type; hello_verify_request
5265 * 14-16 uint24 length; olen - 25
5266 * 17-18 uint16 message_seq; copied
5267 * 19-21 uint24 fragment_offset; copied
5268 * 22-24 uint24 fragment_length; olen - 25
5269 *
5270 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5271 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5272 *
5273 * Minimum length is 28.
5274 */
5275 if( buf_len < 28 )
5276 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5277
5278 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005279 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005280 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5281 obuf[25] = 0xfe;
5282 obuf[26] = 0xff;
5283
5284 /* Generate and write actual cookie */
5285 p = obuf + 28;
5286 if( f_cookie_write( p_cookie,
5287 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5288 {
5289 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5290 }
5291
5292 *olen = p - obuf;
5293
5294 /* Go back and fill length fields */
5295 obuf[27] = (unsigned char)( *olen - 28 );
5296
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005297 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005298 obuf[22] = obuf[14];
5299 obuf[23] = obuf[15];
5300 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005301
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005302 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005303
5304 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5305}
5306
5307/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005308 * Handle possible client reconnect with the same UDP quadruplet
5309 * (RFC 6347 Section 4.2.8).
5310 *
5311 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5312 * that looks like a ClientHello.
5313 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005314 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005315 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005316 * - if the input looks like a ClientHello with a valid cookie,
5317 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005318 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005319 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005320 *
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005321 * This function is called (through ssl_check_client_reconnect()) when an
5322 * unexpected record is found in ssl_get_next_record(), which will discard the
5323 * record if we return 0, and bubble up the return value otherwise (this
5324 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
5325 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005326 */
5327static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5328{
5329 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005330 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005331
Hanno Becker87b56262019-07-10 14:37:41 +01005332 if( ssl->conf->f_cookie_write == NULL ||
5333 ssl->conf->f_cookie_check == NULL )
5334 {
5335 /* If we can't use cookies to verify reachability of the peer,
5336 * drop the record. */
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
5338 "can't check reconnect validity" ) );
Hanno Becker87b56262019-07-10 14:37:41 +01005339 return( 0 );
5340 }
5341
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005342 ret = ssl_check_dtls_clihlo_cookie(
5343 ssl->conf->f_cookie_write,
5344 ssl->conf->f_cookie_check,
5345 ssl->conf->p_cookie,
5346 ssl->cli_id, ssl->cli_id_len,
5347 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005348 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005349
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005350 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5351
5352 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005353 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005354 int send_ret;
5355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
5356 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
5357 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08005358 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005359 * If the error is permanent we'll catch it later,
5360 * if it's not, then hopefully it'll work next time. */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005361 send_ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
5362 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_get_send", send_ret );
5363 (void) send_ret;
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005364 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005365 }
5366
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005367 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005368 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005370 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5371 {
5372 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5373 return( ret );
5374 }
5375
5376 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005377 }
5378
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005379 return( ret );
5380}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005381#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005382
Hanno Becker46483f12019-05-03 13:25:54 +01005383static int ssl_check_record_type( uint8_t record_type )
5384{
5385 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5386 record_type != MBEDTLS_SSL_MSG_ALERT &&
5387 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5388 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5389 {
5390 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5391 }
5392
5393 return( 0 );
5394}
5395
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005396/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005397 * ContentType type;
5398 * ProtocolVersion version;
5399 * uint16 epoch; // DTLS only
5400 * uint48 sequence_number; // DTLS only
5401 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005402 *
5403 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005404 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005405 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5406 *
5407 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005408 * 1. proceed with the record if this function returns 0
5409 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5410 * 3. return CLIENT_RECONNECT if this function return that value
5411 * 4. drop the whole datagram if this function returns anything else.
5412 * Point 2 is needed when the peer is resending, and we have already received
5413 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005414 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005415static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005416 unsigned char *buf,
5417 size_t len,
5418 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005419{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005420 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005421
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005422 size_t const rec_hdr_type_offset = 0;
5423 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005424
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005425 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5426 rec_hdr_type_len;
5427 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005428
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005429 size_t const rec_hdr_ctr_len = 8;
5430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005431 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005432 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5433 rec_hdr_version_len;
5434
Hanno Beckera5a2b082019-05-15 14:03:01 +01005435#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005436 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5437 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005438 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005439#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5440#endif /* MBEDTLS_SSL_PROTO_DTLS */
5441
5442 size_t rec_hdr_len_offset; /* To be determined */
5443 size_t const rec_hdr_len_len = 2;
5444
5445 /*
5446 * Check minimum lengths for record header.
5447 */
5448
5449#if defined(MBEDTLS_SSL_PROTO_DTLS)
5450 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5451 {
5452 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5453 }
5454 MBEDTLS_SSL_TRANSPORT_ELSE
5455#endif /* MBEDTLS_SSL_PROTO_DTLS */
5456#if defined(MBEDTLS_SSL_PROTO_TLS)
5457 {
5458 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5459 }
5460#endif /* MBEDTLS_SSL_PROTO_DTLS */
5461
5462 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5463 {
5464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5465 (unsigned) len,
5466 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5467 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5468 }
5469
5470 /*
5471 * Parse and validate record content type
5472 */
5473
5474 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005475
5476 /* Check record content type */
5477#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5478 rec->cid_len = 0;
5479
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005480 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005481 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5482 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005483 {
5484 /* Shift pointers to account for record header including CID
5485 * struct {
5486 * ContentType special_type = tls12_cid;
5487 * ProtocolVersion version;
5488 * uint16 epoch;
5489 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005490 * opaque cid[cid_length]; // Additional field compared to
5491 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005492 * uint16 length;
5493 * opaque enc_content[DTLSCiphertext.length];
5494 * } DTLSCiphertext;
5495 */
5496
5497 /* So far, we only support static CID lengths
5498 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005499 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5500 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005501
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005502 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005503 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5505 (unsigned) len,
5506 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005507 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005508 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005509
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005510 /* configured CID len is guaranteed at most 255, see
5511 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5512 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005513 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5514 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005515 }
5516 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005517#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005518 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005519 if( ssl_check_record_type( rec->type ) )
5520 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5522 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005523 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5524 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005525 }
5526
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005527 /*
5528 * Parse and validate record version
5529 */
5530
Hanno Becker8061c6e2019-07-26 08:07:03 +01005531 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5532 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005533 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5534 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005535 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005536
Hanno Becker2881d802019-05-22 14:44:53 +01005537 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5540 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005541 }
5542
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005543 if( mbedtls_ssl_ver_gt( minor_ver,
5544 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5547 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005548 }
5549
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005550 /*
5551 * Parse/Copy record sequence number.
5552 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005553
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005554#if defined(MBEDTLS_SSL_PROTO_DTLS)
5555 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5556 {
5557 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005558 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5559 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005560 rec_hdr_ctr_len );
5561 }
5562 MBEDTLS_SSL_TRANSPORT_ELSE
5563#endif /* MBEDTLS_SSL_PROTO_DTLS */
5564#if defined(MBEDTLS_SSL_PROTO_TLS)
5565 {
5566 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005567 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5568 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005569 }
5570#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005571
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005572 /*
5573 * Parse record length.
5574 */
5575
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005576 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005577 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005578 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5579
Hanno Becker8b09b732019-05-08 12:03:28 +01005580 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005581 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005582 rec->type,
5583 major_ver, minor_ver, rec->data_len ) );
5584
5585 rec->buf = buf;
5586 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005587
Hanno Beckerec014082019-07-26 08:20:27 +01005588 if( rec->data_len == 0 )
5589 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5590
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005591 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005592 * DTLS-related tests.
5593 * Check epoch before checking length constraint because
5594 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5595 * message gets duplicated before the corresponding Finished message,
5596 * the second ChangeCipherSpec should be discarded because it belongs
5597 * to an old epoch, but not because its length is shorter than
5598 * the minimum record length for packets using the new record transform.
5599 * Note that these two kinds of failures are handled differently,
5600 * as an unexpected record is silently skipped but an invalid
5601 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005602 */
5603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005604 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005605 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005606 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005607
Hanno Beckere0452772019-07-10 17:12:07 +01005608 /* Check that the datagram is large enough to contain a record
5609 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005610 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005611 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5613 (unsigned) len,
5614 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005615 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5616 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005617
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005618 /* Records from other, non-matching epochs are silently discarded.
5619 * (The case of same-port Client reconnects must be considered in
5620 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005621 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005622 {
5623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5624 "expected %d, received %d",
5625 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005626
5627 /* Records from the next epoch are considered for buffering
5628 * (concretely: early Finished messages). */
5629 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5630 {
5631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5632 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5633 }
5634
Hanno Becker87b56262019-07-10 14:37:41 +01005635 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005636 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005637#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005638 /* For records from the correct epoch, check whether their
5639 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005640 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5641 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005642 {
5643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5644 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5645 }
5646#endif
5647 }
5648#endif /* MBEDTLS_SSL_PROTO_DTLS */
5649
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005650 return( 0 );
5651}
Paul Bakker5121ce52009-01-03 21:22:43 +00005652
Hanno Becker87b56262019-07-10 14:37:41 +01005653
5654#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5655static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5656{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005657 unsigned int rec_epoch = (unsigned int)
5658 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005659
5660 /*
5661 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5662 * access the first byte of record content (handshake type), as we
5663 * have an active transform (possibly iv_len != 0), so use the
5664 * fact that the record header len is 13 instead.
5665 */
5666 if( rec_epoch == 0 &&
5667 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5668 MBEDTLS_SSL_IS_SERVER &&
5669 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5670 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5671 ssl->in_left > 13 &&
5672 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5673 {
5674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5675 "from the same port" ) );
5676 return( ssl_handle_possible_reconnect( ssl ) );
5677 }
5678
5679 return( 0 );
5680}
5681#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5682
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005683/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005684 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005685 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005686static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5687 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005688{
5689 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005691 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005692 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5695 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 ret = mbedtls_ssl_hw_record_read( ssl );
5700 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5703 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005704 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005705
5706 if( ret == 0 )
5707 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005708 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005710 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005712 unsigned char const old_msg_type = rec->type;
5713
Hanno Becker611a83b2018-01-03 14:27:32 +00005714 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005715 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005718
Hanno Beckera5a2b082019-05-15 14:03:01 +01005719#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005720 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005721 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5722 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005723 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005725 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005726 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005727#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005728
Paul Bakker5121ce52009-01-03 21:22:43 +00005729 return( ret );
5730 }
5731
Hanno Becker106f3da2019-07-12 09:35:58 +01005732 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005733 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005734 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005735 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005736 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005737
Paul Bakker5121ce52009-01-03 21:22:43 +00005738 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005739 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005740
Hanno Beckera5a2b082019-05-15 14:03:01 +01005741#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005742 /* We have already checked the record content type
5743 * in ssl_parse_record_header(), failing or silently
5744 * dropping the record in the case of an unknown type.
5745 *
5746 * Since with the use of CIDs, the record content type
5747 * might change during decryption, re-check the record
5748 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005749 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005750 {
5751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5752 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5753 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005754#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005755
Hanno Becker106f3da2019-07-12 09:35:58 +01005756 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005757 {
5758#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005759 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005760 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005761 {
5762 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5764 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5765 }
5766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5767
5768 ssl->nb_zero++;
5769
5770 /*
5771 * Three or more empty messages may be a DoS attack
5772 * (excessive CPU consumption).
5773 */
5774 if( ssl->nb_zero > 3 )
5775 {
5776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005777 "messages, possible DoS attack" ) );
5778 /* Treat the records as if they were not properly authenticated,
5779 * thereby failing the connection if we see more than allowed
5780 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005781 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5782 }
5783 }
5784 else
5785 ssl->nb_zero = 0;
5786
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005787 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5788#if defined(MBEDTLS_SSL_PROTO_TLS)
5789 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005790 {
5791 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005792 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005793 if( ++ssl->in_ctr[i - 1] != 0 )
5794 break;
5795
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005796 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005797 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005798 {
5799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5800 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5801 }
5802 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005803#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005804
Paul Bakker5121ce52009-01-03 21:22:43 +00005805 }
5806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005808 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005811 }
5812#endif
5813
Hanno Beckerf0242852019-07-09 17:30:02 +01005814 /* Check actual (decrypted) record content length against
5815 * configured maximum. */
5816 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5817 {
5818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5819 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5820 }
5821
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005822 return( 0 );
5823}
5824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005826
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005827/*
5828 * Read a record.
5829 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005830 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5831 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5832 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005833 */
Hanno Becker1097b342018-08-15 14:09:41 +01005834
5835/* Helper functions for mbedtls_ssl_read_record(). */
5836static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005837static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5838static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005839
Hanno Becker327c93b2018-08-15 13:56:18 +01005840int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005841 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005842{
5843 int ret;
5844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005846
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005847 if( ssl->keep_current_message == 0 )
5848 {
5849 do {
Simon Butcher99000142016-10-13 17:21:01 +01005850
Hanno Becker26994592018-08-15 14:14:59 +01005851 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005852 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005853 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005854
Hanno Beckere74d5562018-08-15 14:26:08 +01005855 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005856 {
Hanno Becker40f50842018-08-15 14:48:01 +01005857#if defined(MBEDTLS_SSL_PROTO_DTLS)
5858 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005859
Hanno Becker40f50842018-08-15 14:48:01 +01005860 /* We only check for buffered messages if the
5861 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005862 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005863 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005864 {
Hanno Becker40f50842018-08-15 14:48:01 +01005865 if( ssl_load_buffered_message( ssl ) == 0 )
5866 have_buffered = 1;
5867 }
5868
5869 if( have_buffered == 0 )
5870#endif /* MBEDTLS_SSL_PROTO_DTLS */
5871 {
5872 ret = ssl_get_next_record( ssl );
5873 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5874 continue;
5875
5876 if( ret != 0 )
5877 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005878 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005879 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005880 return( ret );
5881 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005882 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005883 }
5884
5885 ret = mbedtls_ssl_handle_message_type( ssl );
5886
Hanno Becker40f50842018-08-15 14:48:01 +01005887#if defined(MBEDTLS_SSL_PROTO_DTLS)
5888 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5889 {
5890 /* Buffer future message */
5891 ret = ssl_buffer_message( ssl );
5892 if( ret != 0 )
5893 return( ret );
5894
5895 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5896 }
5897#endif /* MBEDTLS_SSL_PROTO_DTLS */
5898
Hanno Becker90333da2017-10-10 11:27:13 +01005899 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5900 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005901
5902 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005903 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005904 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005905 return( ret );
5906 }
5907
Hanno Becker327c93b2018-08-15 13:56:18 +01005908 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005909 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005910 {
5911 mbedtls_ssl_update_handshake_status( ssl );
5912 }
Simon Butcher99000142016-10-13 17:21:01 +01005913 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005914 else
Simon Butcher99000142016-10-13 17:21:01 +01005915 {
Hanno Becker02f59072018-08-15 14:00:24 +01005916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005917 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005918 }
5919
5920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5921
5922 return( 0 );
5923}
5924
Hanno Becker40f50842018-08-15 14:48:01 +01005925#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005926static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005927{
Hanno Becker40f50842018-08-15 14:48:01 +01005928 if( ssl->in_left > ssl->next_record_offset )
5929 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005930
Hanno Becker40f50842018-08-15 14:48:01 +01005931 return( 0 );
5932}
5933
5934static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5935{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005936 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005937 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005938 int ret = 0;
5939
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005940 if( hs == NULL )
5941 return( -1 );
5942
Hanno Beckere00ae372018-08-20 09:39:42 +01005943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5944
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005945 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5946 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5947 {
5948 /* Check if we have seen a ChangeCipherSpec before.
5949 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005950 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005951 {
5952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5953 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005954 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005955 }
5956
Hanno Becker39b8bc92018-08-28 17:17:13 +01005957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005958 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5959 ssl->in_msglen = 1;
5960 ssl->in_msg[0] = 1;
5961
5962 /* As long as they are equal, the exact value doesn't matter. */
5963 ssl->in_left = 0;
5964 ssl->next_record_offset = 0;
5965
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005966 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005967 goto exit;
5968 }
Hanno Becker37f95322018-08-16 13:55:32 +01005969
Hanno Beckerb8f50142018-08-28 10:01:34 +01005970#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005971 /* Debug only */
5972 {
5973 unsigned offset;
5974 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5975 {
5976 hs_buf = &hs->buffering.hs[offset];
5977 if( hs_buf->is_valid == 1 )
5978 {
5979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5980 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005981 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005982 }
5983 }
5984 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005985#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005986
5987 /* Check if we have buffered and/or fully reassembled the
5988 * next handshake message. */
5989 hs_buf = &hs->buffering.hs[0];
5990 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5991 {
5992 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005993 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005994
5995 /* Double-check that we haven't accidentally buffered
5996 * a message that doesn't fit into the input buffer. */
5997 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5998 {
5999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6000 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6001 }
6002
6003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
6004 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
6005 hs_buf->data, msg_len + 12 );
6006
6007 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6008 ssl->in_hslen = msg_len + 12;
6009 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03006010 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01006011
6012 ret = 0;
6013 goto exit;
6014 }
6015 else
6016 {
6017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
6018 hs->in_msg_seq ) );
6019 }
6020
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006021 ret = -1;
6022
6023exit:
6024
6025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
6026 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006027}
6028
Hanno Beckera02b0b42018-08-21 17:20:27 +01006029static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
6030 size_t desired )
6031{
6032 int offset;
6033 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
6035 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01006036
Hanno Becker01315ea2018-08-21 17:22:17 +01006037 /* Get rid of future records epoch first, if such exist. */
6038 ssl_free_buffered_record( ssl );
6039
6040 /* Check if we have enough space available now. */
6041 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6042 hs->buffering.total_bytes_buffered ) )
6043 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01006045 return( 0 );
6046 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01006047
Hanno Becker4f432ad2018-08-28 10:02:32 +01006048 /* We don't have enough space to buffer the next expected handshake
6049 * message. Remove buffers used for future messages to gain space,
6050 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01006051 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
6052 offset >= 0; offset-- )
6053 {
6054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
6055 offset ) );
6056
Hanno Beckerb309b922018-08-23 13:18:05 +01006057 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01006058
6059 /* Check if we have enough space available now. */
6060 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6061 hs->buffering.total_bytes_buffered ) )
6062 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01006064 return( 0 );
6065 }
6066 }
6067
6068 return( -1 );
6069}
6070
Hanno Becker40f50842018-08-15 14:48:01 +01006071static int ssl_buffer_message( mbedtls_ssl_context *ssl )
6072{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006073 int ret = 0;
6074 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6075
6076 if( hs == NULL )
6077 return( 0 );
6078
6079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
6080
6081 switch( ssl->in_msgtype )
6082 {
6083 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006085
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006086 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006087 break;
6088
6089 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006090 {
6091 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006092 unsigned recv_msg_seq = (unsigned)
6093 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006094
Hanno Becker37f95322018-08-16 13:55:32 +01006095 mbedtls_ssl_hs_buffer *hs_buf;
6096 size_t msg_len = ssl->in_hslen - 12;
6097
6098 /* We should never receive an old handshake
6099 * message - double-check nonetheless. */
6100 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6101 {
6102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6104 }
6105
6106 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6107 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6108 {
6109 /* Silently ignore -- message too far in the future */
6110 MBEDTLS_SSL_DEBUG_MSG( 2,
6111 ( "Ignore future HS message with sequence number %u, "
6112 "buffering window %u - %u",
6113 recv_msg_seq, ssl->handshake->in_msg_seq,
6114 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6115
6116 goto exit;
6117 }
6118
6119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6120 recv_msg_seq, recv_msg_seq_offset ) );
6121
6122 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6123
6124 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006125 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006126 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006127 size_t reassembly_buf_sz;
6128
Hanno Becker37f95322018-08-16 13:55:32 +01006129 hs_buf->is_fragmented =
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04006130 ( ssl_hs_is_proper_fragment( ssl ) == PROPER_HS_FRAGMENT );
Hanno Becker37f95322018-08-16 13:55:32 +01006131
6132 /* We copy the message back into the input buffer
6133 * after reassembly, so check that it's not too large.
6134 * This is an implementation-specific limitation
6135 * and not one from the standard, hence it is not
6136 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006137 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006138 {
6139 /* Ignore message */
6140 goto exit;
6141 }
6142
Hanno Beckere0b150f2018-08-21 15:51:03 +01006143 /* Check if we have enough space to buffer the message. */
6144 if( hs->buffering.total_bytes_buffered >
6145 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6146 {
6147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6149 }
6150
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006151 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6152 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006153
6154 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6155 hs->buffering.total_bytes_buffered ) )
6156 {
6157 if( recv_msg_seq_offset > 0 )
6158 {
6159 /* If we can't buffer a future message because
6160 * of space limitations -- ignore. */
6161 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",
6162 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6163 (unsigned) hs->buffering.total_bytes_buffered ) );
6164 goto exit;
6165 }
Hanno Beckere1801392018-08-21 16:51:05 +01006166 else
6167 {
6168 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",
6169 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6170 (unsigned) hs->buffering.total_bytes_buffered ) );
6171 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006172
Hanno Beckera02b0b42018-08-21 17:20:27 +01006173 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006174 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006175 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",
6176 (unsigned) msg_len,
6177 (unsigned) reassembly_buf_sz,
6178 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006179 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006180 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6181 goto exit;
6182 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006183 }
6184
6185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6186 msg_len ) );
6187
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006188 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6189 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006190 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006191 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006192 goto exit;
6193 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006194 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006195
6196 /* Prepare final header: copy msg_type, length and message_seq,
6197 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006198 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006199 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006200 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006201
6202 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006203
6204 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006205 }
6206 else
6207 {
6208 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006209 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) // use regular memcmp as msg type is public
Hanno Becker37f95322018-08-16 13:55:32 +01006210 {
6211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6212 /* Ignore */
6213 goto exit;
6214 }
6215 }
6216
Hanno Becker4422bbb2018-08-20 09:40:19 +01006217 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006218 {
6219 size_t frag_len, frag_off;
6220 unsigned char * const msg = hs_buf->data + 12;
6221
6222 /*
6223 * Check and copy current fragment
6224 */
6225
6226 /* Validation of header fields already done in
6227 * mbedtls_ssl_prepare_handshake_record(). */
6228 frag_off = ssl_get_hs_frag_off( ssl );
6229 frag_len = ssl_get_hs_frag_len( ssl );
6230
6231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6232 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006233 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006234
6235 if( hs_buf->is_fragmented )
6236 {
6237 unsigned char * const bitmask = msg + msg_len;
6238 ssl_bitmask_set( bitmask, frag_off, frag_len );
6239 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6240 msg_len ) == 0 );
6241 }
6242 else
6243 {
6244 hs_buf->is_complete = 1;
6245 }
6246
6247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6248 hs_buf->is_complete ? "" : "not yet " ) );
6249 }
6250
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006251 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006252 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006253
6254 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006255 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006256 break;
6257 }
6258
6259exit:
6260
6261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6262 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006263}
6264#endif /* MBEDTLS_SSL_PROTO_DTLS */
6265
Hanno Becker1097b342018-08-15 14:09:41 +01006266static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006267{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006268 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006269 * Consume last content-layer message and potentially
6270 * update in_msglen which keeps track of the contents'
6271 * consumption state.
6272 *
6273 * (1) Handshake messages:
6274 * Remove last handshake message, move content
6275 * and adapt in_msglen.
6276 *
6277 * (2) Alert messages:
6278 * Consume whole record content, in_msglen = 0.
6279 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006280 * (3) Change cipher spec:
6281 * Consume whole record content, in_msglen = 0.
6282 *
6283 * (4) Application data:
6284 * Don't do anything - the record layer provides
6285 * the application data as a stream transport
6286 * and consumes through mbedtls_ssl_read only.
6287 *
6288 */
6289
6290 /* Case (1): Handshake messages */
6291 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006292 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006293 /* Hard assertion to be sure that no application data
6294 * is in flight, as corrupting ssl->in_msglen during
6295 * ssl->in_offt != NULL is fatal. */
6296 if( ssl->in_offt != NULL )
6297 {
6298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6300 }
6301
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006302 /*
6303 * Get next Handshake message in the current record
6304 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006305
Hanno Becker4a810fb2017-05-24 16:27:30 +01006306 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006307 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006308 * current handshake content: If DTLS handshake
6309 * fragmentation is used, that's the fragment
6310 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006311 * size here is faulty and should be changed at
6312 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006313 * (2) While it doesn't seem to cause problems, one
6314 * has to be very careful not to assume that in_hslen
6315 * is always <= in_msglen in a sensible communication.
6316 * Again, it's wrong for DTLS handshake fragmentation.
6317 * The following check is therefore mandatory, and
6318 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006319 * Additionally, ssl->in_hslen might be arbitrarily out of
6320 * bounds after handling a DTLS message with an unexpected
6321 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006322 */
6323 if( ssl->in_hslen < ssl->in_msglen )
6324 {
6325 ssl->in_msglen -= ssl->in_hslen;
6326 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6327 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006328
Hanno Becker4a810fb2017-05-24 16:27:30 +01006329 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6330 ssl->in_msg, ssl->in_msglen );
6331 }
6332 else
6333 {
6334 ssl->in_msglen = 0;
6335 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006336
Hanno Becker4a810fb2017-05-24 16:27:30 +01006337 ssl->in_hslen = 0;
6338 }
6339 /* Case (4): Application data */
6340 else if( ssl->in_offt != NULL )
6341 {
6342 return( 0 );
6343 }
6344 /* Everything else (CCS & Alerts) */
6345 else
6346 {
6347 ssl->in_msglen = 0;
6348 }
6349
Hanno Becker1097b342018-08-15 14:09:41 +01006350 return( 0 );
6351}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006352
Hanno Beckere74d5562018-08-15 14:26:08 +01006353static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6354{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006355 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006356 return( 1 );
6357
6358 return( 0 );
6359}
6360
Hanno Becker5f066e72018-08-16 14:56:31 +01006361#if defined(MBEDTLS_SSL_PROTO_DTLS)
6362
6363static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6364{
6365 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6366 if( hs == NULL )
6367 return;
6368
Hanno Becker01315ea2018-08-21 17:22:17 +01006369 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006370 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006371 hs->buffering.total_bytes_buffered -=
6372 hs->buffering.future_record.len;
6373
6374 mbedtls_free( hs->buffering.future_record.data );
6375 hs->buffering.future_record.data = NULL;
6376 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006377}
6378
6379static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6380{
6381 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6382 unsigned char * rec;
6383 size_t rec_len;
6384 unsigned rec_epoch;
6385
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006386 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006387 return( 0 );
6388
6389 if( hs == NULL )
6390 return( 0 );
6391
Hanno Becker5f066e72018-08-16 14:56:31 +01006392 rec = hs->buffering.future_record.data;
6393 rec_len = hs->buffering.future_record.len;
6394 rec_epoch = hs->buffering.future_record.epoch;
6395
6396 if( rec == NULL )
6397 return( 0 );
6398
Hanno Becker4cb782d2018-08-20 11:19:05 +01006399 /* Only consider loading future records if the
6400 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006401 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006402 return( 0 );
6403
Hanno Becker5f066e72018-08-16 14:56:31 +01006404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6405
6406 if( rec_epoch != ssl->in_epoch )
6407 {
6408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6409 goto exit;
6410 }
6411
6412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6413
6414 /* Double-check that the record is not too large */
6415 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6416 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6417 {
6418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6420 }
6421
Teppo Järvelin91d79382019-10-02 09:09:31 +03006422 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006423 ssl->in_left = rec_len;
6424 ssl->next_record_offset = 0;
6425
6426 ssl_free_buffered_record( ssl );
6427
6428exit:
6429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6430 return( 0 );
6431}
6432
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006433static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6434 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006435{
6436 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006437
6438 /* Don't buffer future records outside handshakes. */
6439 if( hs == NULL )
6440 return( 0 );
6441
6442 /* Only buffer handshake records (we are only interested
6443 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006444 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006445 return( 0 );
6446
6447 /* Don't buffer more than one future epoch record. */
6448 if( hs->buffering.future_record.data != NULL )
6449 return( 0 );
6450
Hanno Becker01315ea2018-08-21 17:22:17 +01006451 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006452 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006453 hs->buffering.total_bytes_buffered ) )
6454 {
6455 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 +01006456 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006457 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006458 return( 0 );
6459 }
6460
Hanno Becker5f066e72018-08-16 14:56:31 +01006461 /* Buffer record */
6462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6463 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006464 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006465
6466 /* ssl_parse_record_header() only considers records
6467 * of the next epoch as candidates for buffering. */
6468 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006469 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006470
6471 hs->buffering.future_record.data =
6472 mbedtls_calloc( 1, hs->buffering.future_record.len );
6473 if( hs->buffering.future_record.data == NULL )
6474 {
6475 /* If we run out of RAM trying to buffer a
6476 * record from the next epoch, just ignore. */
6477 return( 0 );
6478 }
6479
Teppo Järvelin91d79382019-10-02 09:09:31 +03006480 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006481
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006482 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006483 return( 0 );
6484}
6485
6486#endif /* MBEDTLS_SSL_PROTO_DTLS */
6487
Hanno Beckere74d5562018-08-15 14:26:08 +01006488static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006489{
6490 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006491 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006492
Hanno Becker5f066e72018-08-16 14:56:31 +01006493#if defined(MBEDTLS_SSL_PROTO_DTLS)
6494 /* We might have buffered a future record; if so,
6495 * and if the epoch matches now, load it.
6496 * On success, this call will set ssl->in_left to
6497 * the length of the buffered record, so that
6498 * the calls to ssl_fetch_input() below will
6499 * essentially be no-ops. */
6500 ret = ssl_load_buffered_record( ssl );
6501 if( ret != 0 )
6502 return( ret );
6503#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006504
Hanno Becker8b09b732019-05-08 12:03:28 +01006505 /* Ensure that we have enough space available for the default form
6506 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6507 * with no space for CIDs counted in). */
6508 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6509 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006512 return( ret );
6513 }
6514
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006515 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6516 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006519 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006520 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006521 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6522 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006523 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006524 if( ret != 0 )
6525 return( ret );
6526
6527 /* Fall through to handling of unexpected records */
6528 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6529 }
6530
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006531 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6532 {
Hanno Becker87b56262019-07-10 14:37:41 +01006533#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006534 /* Reset in pointers to default state for TLS/DTLS records,
6535 * assuming no CID and no offset between record content and
6536 * record plaintext. */
6537 ssl_update_in_pointers( ssl );
6538
Hanno Becker69412452019-07-12 08:33:49 +01006539 /* Setup internal message pointers from record structure. */
6540 ssl->in_msgtype = rec.type;
6541#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6542 ssl->in_len = ssl->in_cid + rec.cid_len;
6543#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006544 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006545 ssl->in_msglen = rec.data_len;
6546
Hanno Becker87b56262019-07-10 14:37:41 +01006547 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02006548 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker87b56262019-07-10 14:37:41 +01006549 if( ret != 0 )
6550 return( ret );
6551#endif
6552
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006553 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006554 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006555
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6557 "(header)" ) );
6558 }
6559 else
6560 {
6561 /* Skip invalid record and the rest of the datagram */
6562 ssl->next_record_offset = 0;
6563 ssl->in_left = 0;
6564
6565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6566 "(header)" ) );
6567 }
6568
6569 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006570 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006571 }
Hanno Becker87b56262019-07-10 14:37:41 +01006572 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006573#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006574 {
6575 return( ret );
6576 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006577 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006579#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006580 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006581 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006582 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006583 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006584 if( ssl->next_record_offset < ssl->in_left )
6585 {
6586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6587 }
6588 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006589 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006590#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006591#if defined(MBEDTLS_SSL_PROTO_TLS)
6592 {
Hanno Beckere0452772019-07-10 17:12:07 +01006593 /*
6594 * Fetch record contents from underlying transport.
6595 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006596 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006597 if( ret != 0 )
6598 {
6599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6600 return( ret );
6601 }
6602
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006603 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006604 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006605#endif /* MBEDTLS_SSL_PROTO_TLS */
6606
6607 /*
6608 * Decrypt record contents.
6609 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006610
Hanno Beckera89610a2019-07-11 13:07:45 +01006611 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006614 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006615 {
6616 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006617 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006618 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006619 /* Except when waiting for Finished as a bad mac here
6620 * probably means something went wrong in the handshake
6621 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6622 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6623 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6624 {
6625#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6626 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6627 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006628 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006629 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6630 }
6631#endif
6632 return( ret );
6633 }
6634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006636 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6637 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6640 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006641 }
6642#endif
6643
Hanno Becker4a810fb2017-05-24 16:27:30 +01006644 /* As above, invalid records cause
6645 * dismissal of the whole datagram. */
6646
6647 ssl->next_record_offset = 0;
6648 ssl->in_left = 0;
6649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006651 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006652 }
6653
6654 return( ret );
6655 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006656 MBEDTLS_SSL_TRANSPORT_ELSE
6657#endif /* MBEDTLS_SSL_PROTO_DTLS */
6658#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006659 {
6660 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6662 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006663 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006664 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006666 }
6667#endif
6668 return( ret );
6669 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006670#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006671 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006672
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006673
6674 /* Reset in pointers to default state for TLS/DTLS records,
6675 * assuming no CID and no offset between record content and
6676 * record plaintext. */
6677 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006678#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6679 ssl->in_len = ssl->in_cid + rec.cid_len;
6680#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006681 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006682
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006683 /* The record content type may change during decryption,
6684 * so re-read it. */
6685 ssl->in_msgtype = rec.type;
6686 /* Also update the input buffer, because unfortunately
6687 * the server-side ssl_parse_client_hello() reparses the
6688 * record header when receiving a ClientHello initiating
6689 * a renegotiation. */
6690 ssl->in_hdr[0] = rec.type;
6691 ssl->in_msg = rec.buf + rec.data_offset;
6692 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006693 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006694
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006695#if defined(MBEDTLS_ZLIB_SUPPORT)
6696 if( ssl->transform_in != NULL &&
6697 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6698 {
6699 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6700 {
6701 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6702 return( ret );
6703 }
6704
6705 /* Check actual (decompress) record content length against
6706 * configured maximum. */
6707 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6708 {
6709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6710 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6711 }
6712 }
6713#endif /* MBEDTLS_ZLIB_SUPPORT */
6714
Simon Butcher99000142016-10-13 17:21:01 +01006715 return( 0 );
6716}
6717
6718int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6719{
6720 int ret;
6721
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006722 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006723 * Handle particular types of records
6724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006726 {
Simon Butcher99000142016-10-13 17:21:01 +01006727 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6728 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006729 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006730 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006731 }
6732
Hanno Beckere678eaa2018-08-21 14:57:46 +01006733 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006734 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006735 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006736 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6738 ssl->in_msglen ) );
6739 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006740 }
6741
Hanno Beckere678eaa2018-08-21 14:57:46 +01006742 if( ssl->in_msg[0] != 1 )
6743 {
6744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6745 ssl->in_msg[0] ) );
6746 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6747 }
6748
6749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006750 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006751 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6752 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6753 {
6754 if( ssl->handshake == NULL )
6755 {
6756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6757 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6758 }
6759
6760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6761 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6762 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006763#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006764 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006767 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006768 if( ssl->in_msglen != 2 )
6769 {
6770 /* Note: Standard allows for more than one 2 byte alert
6771 to be packed in a single message, but Mbed TLS doesn't
6772 currently support this. */
6773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6774 ssl->in_msglen ) );
6775 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6776 }
6777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 ssl->in_msg[0], ssl->in_msg[1] ) );
6780
6781 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006782 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006787 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 }
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6792 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6795 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006796 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006797
6798#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6799 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6800 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6801 {
Hanno Becker90333da2017-10-10 11:27:13 +01006802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006803 /* Will be handled when trying to parse ServerHello */
6804 return( 0 );
6805 }
6806#endif
6807
6808#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006809 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006810 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6811 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006812 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6813 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6814 {
6815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6816 /* Will be handled in mbedtls_ssl_parse_certificate() */
6817 return( 0 );
6818 }
6819#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6820
6821 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006822 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006823 }
6824
Hanno Beckerc76c6192017-06-06 10:03:17 +01006825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006826 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006827 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006828 /* Drop unexpected ApplicationData records,
6829 * except at the beginning of renegotiations */
6830 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6831 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6832#if defined(MBEDTLS_SSL_RENEGOTIATION)
6833 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6834 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006835#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006836 )
6837 {
6838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6839 return( MBEDTLS_ERR_SSL_NON_FATAL );
6840 }
6841
6842 if( ssl->handshake != NULL &&
6843 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6844 {
6845 ssl_handshake_wrapup_free_hs_transform( ssl );
6846 }
6847 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006848#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006849
Paul Bakker5121ce52009-01-03 21:22:43 +00006850 return( 0 );
6851}
6852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006854{
6855 int ret;
6856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6858 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6859 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006860 {
6861 return( ret );
6862 }
6863
6864 return( 0 );
6865}
6866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006868 unsigned char level,
6869 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006870{
6871 int ret;
6872
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006873 if( ssl == NULL || ssl->conf == NULL )
6874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006880 ssl->out_msglen = 2;
6881 ssl->out_msg[0] = level;
6882 ssl->out_msg[1] = message;
6883
Hanno Becker67bc7c32018-08-06 11:33:50 +01006884 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006887 return( ret );
6888 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006890
6891 return( 0 );
6892}
6893
Hanno Becker17572472019-02-08 07:19:04 +00006894#if defined(MBEDTLS_X509_CRT_PARSE_C)
6895static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6896{
6897#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6898 if( session->peer_cert != NULL )
6899 {
6900 mbedtls_x509_crt_free( session->peer_cert );
6901 mbedtls_free( session->peer_cert );
6902 session->peer_cert = NULL;
6903 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006904#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006905 if( session->peer_cert_digest != NULL )
6906 {
6907 /* Zeroization is not necessary. */
6908 mbedtls_free( session->peer_cert_digest );
6909 session->peer_cert_digest = NULL;
6910 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6911 session->peer_cert_digest_len = 0;
6912 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006913#else
6914 ((void) session);
6915#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006916}
6917#endif /* MBEDTLS_X509_CRT_PARSE_C */
6918
Paul Bakker5121ce52009-01-03 21:22:43 +00006919/*
6920 * Handshake functions
6921 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006922#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006923/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006925{
Hanno Beckerdf645962019-06-26 13:02:22 +01006926 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6927 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006930
Hanno Becker5097cba2019-02-05 13:36:46 +00006931 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006934 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6935 {
6936 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6937 }
6938 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6939 {
6940 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6941 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006942 else
6943 {
6944 ssl->state = MBEDTLS_SSL_INVALID;
6945 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006946 return( 0 );
6947 }
6948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6950 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006951}
6952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006954{
Hanno Beckerdf645962019-06-26 13:02:22 +01006955 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6956 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006959
Hanno Becker5097cba2019-02-05 13:36:46 +00006960 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006963 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6964 {
6965 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6966 }
6967 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6968 {
6969 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6970 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006971 else
6972 {
6973 ssl->state = MBEDTLS_SSL_INVALID;
6974 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006975 return( 0 );
6976 }
6977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006980}
Gilles Peskinef9828522017-05-03 12:28:43 +02006981
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006982#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006983/* Some certificate support -> implement write and parse */
6984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006988 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006990 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6991 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006994
Hanno Becker5097cba2019-02-05 13:36:46 +00006995 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006998 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6999 {
7000 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7001 }
7002 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7003 {
7004 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7005 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007006 else
7007 {
7008 ssl->state = MBEDTLS_SSL_INVALID;
7009 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02007010 return( 0 );
7011 }
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007014 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7015 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00007016 {
7017 if( ssl->client_auth == 0 )
7018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02007020 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7021 {
7022 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7023 }
7024 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7025 {
7026 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7027 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007028 else
7029 {
7030 ssl->state = MBEDTLS_SSL_INVALID;
7031 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007032 return( 0 );
7033 }
7034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00007036 /*
7037 * If using SSLv3 and got no cert, send an Alert message
7038 * (otherwise an empty Certificate message will be sent).
7039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01007041 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007042 {
7043 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
7045 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
7046 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00007047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007049 goto write_msg;
7050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007052 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053#endif /* MBEDTLS_SSL_CLI_C */
7054#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007055 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00007056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
7060 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007061 }
7062 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007063#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007066
7067 /*
7068 * 0 . 0 handshake type
7069 * 1 . 3 handshake length
7070 * 4 . 6 length of all certs
7071 * 7 . 9 length of cert. 1
7072 * 10 . n-1 peer certificate
7073 * n . n+2 length of cert. 2
7074 * n+3 . ... upper level cert, etc.
7075 */
7076 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007078
Paul Bakker29087132010-03-21 21:03:34 +00007079 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007080 {
7081 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10007082 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00007083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10007085 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087 }
7088
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007089 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007090
Teppo Järvelin91d79382019-10-02 09:09:31 +03007091 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092 i += n; crt = crt->next;
7093 }
7094
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007095 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007096
7097 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7099 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007100
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007101#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007102write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007103#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007104
Jarno Lamsa2b205162019-11-12 15:36:21 +02007105 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7106 {
7107 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7108 }
7109 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7110 {
7111 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7112 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007113 else
7114 {
7115 ssl->state = MBEDTLS_SSL_INVALID;
7116 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007117
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007118 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 return( ret );
7122 }
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007125
Paul Bakkered27a042013-04-18 22:46:23 +02007126 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007127}
7128
Hanno Becker285ff0c2019-01-31 07:44:03 +00007129#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007130
7131#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007132static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7133 unsigned char *crt_buf,
7134 size_t crt_buf_len )
7135{
7136 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7137
7138 if( peer_crt == NULL )
Andrzej Kurek84bde412020-07-06 15:27:34 -04007139 return( 0x75555555 );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007140
7141 if( peer_crt->raw.len != crt_buf_len )
Andrzej Kurek84bde412020-07-06 15:27:34 -04007142 return( 0x75555555 );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007143
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007144 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007145}
Hanno Becker5882dd02019-06-06 16:25:57 +01007146#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007147static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7148 unsigned char *crt_buf,
7149 size_t crt_buf_len )
7150{
7151 int ret;
7152 unsigned char const * const peer_cert_digest =
7153 ssl->session->peer_cert_digest;
7154 mbedtls_md_type_t const peer_cert_digest_type =
7155 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007156 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007157 mbedtls_md_info_from_type( peer_cert_digest_type );
7158 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7159 size_t digest_len;
7160
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007161 if( peer_cert_digest == NULL ||
7162 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7163 {
Andrzej Kurek84bde412020-07-06 15:27:34 -04007164 return( 0x75555555 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007165 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007166
7167 digest_len = mbedtls_md_get_size( digest_info );
7168 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
Andrzej Kurek84bde412020-07-06 15:27:34 -04007169 return( 0x75555555 );
Hanno Beckerdf759382019-02-05 17:02:46 +00007170
7171 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7172 if( ret != 0 )
Andrzej Kurek84bde412020-07-06 15:27:34 -04007173 return( 0x75555555 );
Hanno Beckerdf759382019-02-05 17:02:46 +00007174
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007175 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007176}
Hanno Becker5882dd02019-06-06 16:25:57 +01007177#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007178#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007179
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007180/*
7181 * Once the certificate message is read, parse it into a cert chain and
7182 * perform basic checks, but leave actual verification to the caller
7183 */
Hanno Becker35e41772019-02-05 15:37:23 +00007184static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7185 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007186{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007187 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007188#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7189 int crt_cnt=0;
7190#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007191 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007192 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007197 mbedtls_ssl_pend_fatal_alert( ssl,
7198 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007200 }
7201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7203 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007206 mbedtls_ssl_pend_fatal_alert( ssl,
7207 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007209 }
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007212
Paul Bakker5121ce52009-01-03 21:22:43 +00007213 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007215 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007216 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007217
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007218 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007222 mbedtls_ssl_pend_fatal_alert( ssl,
7223 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007225 }
7226
Hanno Becker33c3dc82019-01-30 14:46:46 +00007227 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7228 i += 3;
7229
Hanno Becker33c3dc82019-01-30 14:46:46 +00007230 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007231 while( i < ssl->in_hslen )
7232 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007233 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007234 if ( i + 3 > ssl->in_hslen ) {
7235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007236 mbedtls_ssl_pend_fatal_alert( ssl,
7237 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007238 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7239 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007240 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7241 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007242 if( ssl->in_msg[i] != 0 )
7243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007245 mbedtls_ssl_pend_fatal_alert( ssl,
7246 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007248 }
7249
Hanno Becker33c3dc82019-01-30 14:46:46 +00007250 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007251 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007252 i += 3;
7253
7254 if( n < 128 || i + n > ssl->in_hslen )
7255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007257 mbedtls_ssl_pend_fatal_alert( ssl,
7258 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007260 }
7261
Hanno Becker33c3dc82019-01-30 14:46:46 +00007262 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007263#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7264 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007265 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7266 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007267 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007268 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007269 /* During client-side renegotiation, check that the server's
7270 * end-CRTs hasn't changed compared to the initial handshake,
7271 * mitigating the triple handshake attack. On success, reuse
7272 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7274 if( ssl_check_peer_crt_unchanged( ssl,
7275 &ssl->in_msg[i],
7276 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007277 {
Hanno Becker35e41772019-02-05 15:37:23 +00007278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007279 mbedtls_ssl_pend_fatal_alert( ssl,
7280 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007281 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007282 }
Hanno Becker35e41772019-02-05 15:37:23 +00007283
7284 /* Now we can safely free the original chain. */
7285 ssl_clear_peer_cert( ssl->session );
7286 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007287#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7288
Hanno Becker33c3dc82019-01-30 14:46:46 +00007289 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007290#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007291 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007292#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007293 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007294 * it in-place from the input buffer instead of making a copy. */
7295 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7296#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007297 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007298 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007299 case 0: /*ok*/
7300 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7301 /* Ignore certificate with an unknown algorithm: maybe a
7302 prior certificate was already trusted. */
7303 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007304
Hanno Becker33c3dc82019-01-30 14:46:46 +00007305 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7306 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7307 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007308
Hanno Becker33c3dc82019-01-30 14:46:46 +00007309 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7310 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7311 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007312
Hanno Becker33c3dc82019-01-30 14:46:46 +00007313 default:
7314 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7315 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007316 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007317 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7318 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007319 }
7320
7321 i += n;
7322 }
7323
Hanno Becker35e41772019-02-05 15:37:23 +00007324 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007325 return( 0 );
7326}
7327
Hanno Beckerb8a08572019-02-05 12:49:06 +00007328#if defined(MBEDTLS_SSL_SRV_C)
7329static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7330{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007331 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007332 return( -1 );
7333
7334#if defined(MBEDTLS_SSL_PROTO_SSL3)
7335 /*
7336 * Check if the client sent an empty certificate
7337 */
Hanno Becker2881d802019-05-22 14:44:53 +01007338 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007339 {
7340 if( ssl->in_msglen == 2 &&
7341 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7342 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7343 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7344 {
7345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7346 return( 0 );
7347 }
7348
7349 return( -1 );
7350 }
7351#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7352
7353#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7354 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7355 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7356 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7357 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007358 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) // use regular memcmp as comparing public data
Hanno Beckerb8a08572019-02-05 12:49:06 +00007359 {
7360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7361 return( 0 );
7362 }
7363
Hanno Beckerb8a08572019-02-05 12:49:06 +00007364#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7365 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007366
7367 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007368}
7369#endif /* MBEDTLS_SSL_SRV_C */
7370
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007371/* Check if a certificate message is expected.
7372 * Return either
7373 * - SSL_CERTIFICATE_EXPECTED, or
7374 * - SSL_CERTIFICATE_SKIP
7375 * indicating whether a Certificate message is expected or not.
7376 */
7377#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007378#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007379static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7380 int authmode )
7381{
Hanno Becker473f98f2019-06-26 10:27:32 +01007382 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007383 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007384
7385 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7386 return( SSL_CERTIFICATE_SKIP );
7387
7388#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007389 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007390 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007391 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7392 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7393 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007394 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007395 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007396
7397 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7398 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007399 ssl->session_negotiate->verify_result =
7400 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7401 return( SSL_CERTIFICATE_SKIP );
7402 }
7403 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007404#else
7405 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007406#endif /* MBEDTLS_SSL_SRV_C */
7407
7408 return( SSL_CERTIFICATE_EXPECTED );
7409}
7410
Hanno Becker3cf50612019-02-05 14:36:34 +00007411static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007412 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007413 mbedtls_x509_crt *chain,
7414 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007415{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007416 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007417 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007418 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007419 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007420 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007421 mbedtls_x509_crt *ca_chain;
7422 mbedtls_x509_crl *ca_crl;
7423
7424 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007425 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007426 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007427 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007428
Jarno Lamsae1621d42019-12-19 08:58:56 +02007429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007430#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7431 if( ssl->handshake->sni_ca_chain != NULL )
7432 {
7433 ca_chain = ssl->handshake->sni_ca_chain;
7434 ca_crl = ssl->handshake->sni_ca_crl;
7435 }
7436 else
7437#endif
7438 {
7439 ca_chain = ssl->conf->ca_chain;
7440 ca_crl = ssl->conf->ca_crl;
7441 }
7442
7443 /*
7444 * Main check: verify certificate
7445 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007446 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007447 chain,
7448 ca_chain, ca_crl,
7449 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007450#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007451 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007452#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007453 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007454#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7455 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7456#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7457 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007458
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007459 if( verify_ret == 0 )
7460 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007461 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007462 if( verify_ret == 0 )
7463 {
7464 flow_counter++;
7465 }
7466 else
7467 {
7468 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7469 }
7470 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007471 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007472 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007473 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007474 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007475 }
7476
7477#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007478 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007479 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7480#endif
7481
7482 /*
7483 * Secondary checks: always done, but change 'ret' only if it was 0
7484 */
7485
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007486#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007487 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007488#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007489 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007490#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007491 mbedtls_pk_context *pk;
7492 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7493 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007494 {
7495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007496 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007497 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007498
7499 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007500 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007501 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007502 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007503 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007504
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007505 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007506#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007507
7508 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007509 {
7510 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7511
7512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007513 if( verify_ret == 0 )
7514 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007515 flow_counter++;
7516 }
7517 if( ret == 0 )
7518 {
7519 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007520 }
7521 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007522#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007523
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007524 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007525 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007526 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7527 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007528 &ssl->session_negotiate->verify_result );
7529 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007530 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007531 flow_counter++;
7532 }
7533 else
7534 {
7535 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007537 if( verify_ret == 0 )
7538 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007539 }
7540
7541 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7542 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7543 * with details encoded in the verification flags. All other kinds
7544 * of error codes, including those from the user provided f_vrfy
7545 * functions, are treated as fatal and lead to a failure of
7546 * ssl_parse_certificate even if verification was optional. */
7547 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007548 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7549 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007550 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007551 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007552 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7553 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7554 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7555 {
7556 verify_ret = 0;
7557 flow_counter++;
7558 }
7559 else
7560 {
7561 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7562 }
7563 } else {
7564 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007565 }
7566
7567 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7568 {
7569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007570 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007571 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007572 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007573 else
7574 {
7575 flow_counter++;
7576 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007577
Hanno Becker8c13ee62019-02-26 16:48:17 +00007578 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007579 {
7580 uint8_t alert;
7581
7582 /* The certificate may have been rejected for several reasons.
7583 Pick one and send the corresponding alert. Which alert to send
7584 may be a subject of debate in some cases. */
7585 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7586 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7587 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7588 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7589 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7590 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7591 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7592 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7593 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7594 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7595 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7596 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7597 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7598 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7599 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7600 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7601 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7602 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7603 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7604 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7605 else
7606 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007607 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007608 }
7609
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007610 if( verify_ret == 0 &&
7611#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7612 flow_counter == 5 )
7613#else
7614 flow_counter == 4 )
7615#endif
7616 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007617 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007618 if( verify_ret == 0 &&
7619#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7620 flow_counter == 5 )
7621#else
7622 flow_counter == 4 )
7623#endif
7624 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007626 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7627 }
7628 else
7629 {
7630 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7631 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007632 } else {
7633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007634 }
7635
Hanno Becker3cf50612019-02-05 14:36:34 +00007636#if defined(MBEDTLS_DEBUG_C)
7637 if( ssl->session_negotiate->verify_result != 0 )
7638 {
7639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7640 ssl->session_negotiate->verify_result ) );
7641 }
7642 else
7643 {
7644 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7645 }
7646#endif /* MBEDTLS_DEBUG_C */
7647
Hanno Becker8c13ee62019-02-26 16:48:17 +00007648 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007649}
7650
Hanno Becker34106f62019-02-08 14:59:05 +00007651#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007652
7653#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007654static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7655 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007656{
7657 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007658 /* Remember digest of the peer's end-CRT. */
7659 ssl->session_negotiate->peer_cert_digest =
7660 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7661 if( ssl->session_negotiate->peer_cert_digest == NULL )
7662 {
7663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7664 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007665 mbedtls_ssl_pend_fatal_alert( ssl,
7666 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007667
7668 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7669 }
7670
7671 ret = mbedtls_md( mbedtls_md_info_from_type(
7672 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7673 start, len,
7674 ssl->session_negotiate->peer_cert_digest );
7675
7676 ssl->session_negotiate->peer_cert_digest_type =
7677 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7678 ssl->session_negotiate->peer_cert_digest_len =
7679 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7680
7681 return( ret );
7682}
Hanno Becker5882dd02019-06-06 16:25:57 +01007683#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007684
7685static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7686 unsigned char *start, size_t len )
7687{
7688 unsigned char *end = start + len;
7689 int ret;
7690
7691 /* Make a copy of the peer's raw public key. */
7692 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7693 ret = mbedtls_pk_parse_subpubkey( &start, end,
7694 &ssl->handshake->peer_pubkey );
7695 if( ret != 0 )
7696 {
7697 /* We should have parsed the public key before. */
7698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7699 }
7700
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007701 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007702 return( 0 );
7703}
7704#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7705
Hanno Becker3cf50612019-02-05 14:36:34 +00007706int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7707{
7708 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007709 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007710#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7711 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7712 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007713 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007714#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007715 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007716#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007717 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007718 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007719
7720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7721
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007722 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7723 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007724 {
7725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007726 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007727 }
7728
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007729#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7730 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007731 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007732 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007733 chain = ssl->handshake->ecrs_peer_cert;
7734 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007735 goto crt_verify;
7736 }
7737#endif
7738
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007739 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007740 {
7741 /* mbedtls_ssl_read_record may have sent an alert already. We
7742 let it decide whether to alert. */
7743 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007744 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007745 }
7746
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007747#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007748 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7749 {
7750 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007751
Hanno Beckerb8a08572019-02-05 12:49:06 +00007752 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007753 ret = 0;
7754 else
7755 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007756
Hanno Becker613d4902019-02-05 13:11:17 +00007757 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007758 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007759#endif /* MBEDTLS_SSL_SRV_C */
7760
Hanno Becker35e41772019-02-05 15:37:23 +00007761 /* Clear existing peer CRT structure in case we tried to
7762 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007763 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007764
7765 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7766 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007767 {
7768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7769 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007770 mbedtls_ssl_pend_fatal_alert( ssl,
7771 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007772
Hanno Beckere4aeb762019-02-05 17:19:52 +00007773 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7774 goto exit;
7775 }
7776 mbedtls_x509_crt_init( chain );
7777
7778 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007779 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007780 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007781
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007782#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7783 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007784 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007785
7786crt_verify:
7787 if( ssl->handshake->ecrs_enabled)
7788 rs_ctx = &ssl->handshake->ecrs_ctx;
7789#endif
7790
Hanno Becker3cf50612019-02-05 14:36:34 +00007791 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007792 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007793 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007794 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007795
Hanno Becker3008d282019-02-05 17:02:28 +00007796#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007797 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007798 size_t pk_len;
7799 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007800
Hanno Becker34106f62019-02-08 14:59:05 +00007801 /* We parse the CRT chain without copying, so
7802 * these pointers point into the input buffer,
7803 * and are hence still valid after freeing the
7804 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007805
Hanno Becker5882dd02019-06-06 16:25:57 +01007806#if defined(MBEDTLS_SSL_RENEGOTIATION)
7807 unsigned char *crt_start;
7808 size_t crt_len;
7809
Hanno Becker34106f62019-02-08 14:59:05 +00007810 crt_start = chain->raw.p;
7811 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007812#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007813
Hanno Becker8c13ee62019-02-26 16:48:17 +00007814 pk_start = chain->cache->pk_raw.p;
7815 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007816
7817 /* Free the CRT structures before computing
7818 * digest and copying the peer's public key. */
7819 mbedtls_x509_crt_free( chain );
7820 mbedtls_free( chain );
7821 chain = NULL;
7822
Hanno Becker5882dd02019-06-06 16:25:57 +01007823#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007824 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007825 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007826 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007827#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007828
Hanno Becker34106f62019-02-08 14:59:05 +00007829 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007830 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007831 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007832 }
Hanno Becker34106f62019-02-08 14:59:05 +00007833#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7834 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007835 ssl->session_negotiate->peer_cert = chain;
7836 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007837#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007840
Hanno Becker613d4902019-02-05 13:11:17 +00007841exit:
7842
Hanno Beckere4aeb762019-02-05 17:19:52 +00007843 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007844 {
7845 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7846 {
7847 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7848 }
7849 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7850 {
7851 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7852 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007853 else
7854 {
7855 ssl->state = MBEDTLS_SSL_INVALID;
7856 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007857 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007858
7859#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7860 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7861 {
7862 ssl->handshake->ecrs_peer_cert = chain;
7863 chain = NULL;
7864 }
7865#endif
7866
7867 if( chain != NULL )
7868 {
7869 mbedtls_x509_crt_free( chain );
7870 mbedtls_free( chain );
7871 }
7872
Paul Bakker5121ce52009-01-03 21:22:43 +00007873 return( ret );
7874}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007875#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007878{
7879 int ret;
7880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007884 ssl->out_msglen = 1;
7885 ssl->out_msg[0] = 1;
7886
Jarno Lamsa2b205162019-11-12 15:36:21 +02007887 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7888 {
7889 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7890 }
7891 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7892 {
7893 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7894 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007895 else
7896 {
7897 ssl->state = MBEDTLS_SSL_INVALID;
7898 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007899
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007900 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007901 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007903 return( ret );
7904 }
7905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007907
7908 return( 0 );
7909}
7910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007912{
7913 int ret;
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007916
Hanno Becker327c93b2018-08-15 13:56:18 +01007917 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007920 return( ret );
7921 }
7922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007926 mbedtls_ssl_pend_fatal_alert( ssl,
7927 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007929 }
7930
Hanno Beckere678eaa2018-08-21 14:57:46 +01007931 /* CCS records are only accepted if they have length 1 and content '1',
7932 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007933
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007934 /*
7935 * Switch to our negotiated transform and session parameters for inbound
7936 * data.
7937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007939 ssl->transform_in = ssl->transform_negotiate;
7940 ssl->session_in = ssl->session_negotiate;
7941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007943 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007946 ssl_dtls_replay_reset( ssl );
7947#endif
7948
7949 /* Increment epoch */
7950 if( ++ssl->in_epoch == 0 )
7951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007953 /* This is highly unlikely to happen for legitimate reasons, so
7954 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007956 }
7957 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007958 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007960#if defined(MBEDTLS_SSL_PROTO_TLS)
7961 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007962 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007963 }
7964#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007965
Hanno Beckerf5970a02019-05-08 09:38:41 +01007966 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7969 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007974 mbedtls_ssl_pend_fatal_alert( ssl,
7975 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007977 }
7978 }
7979#endif
7980
Jarno Lamsa2b205162019-11-12 15:36:21 +02007981 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7982 {
7983 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7984 }
7985 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7986 {
7987 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7988 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007989 else
7990 {
7991 ssl->state = MBEDTLS_SSL_INVALID;
7992 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007995
7996 return( 0 );
7997}
7998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02008000{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8002 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01008003 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008004 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02008005#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8007#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008008 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02008009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008011 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02008012#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02008014}
8015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008017{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008019
8020 /*
8021 * Free our handshake params
8022 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02008023 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00008025 ssl->handshake = NULL;
8026
8027 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008028 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00008029 */
8030 if( ssl->transform )
8031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 mbedtls_ssl_transform_free( ssl->transform );
8033 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008034 }
8035 ssl->transform = ssl->transform_negotiate;
8036 ssl->transform_negotiate = NULL;
8037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008039}
8040
Jarno Lamsae1621d42019-12-19 08:58:56 +02008041int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008042{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04008043 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008044
8045#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02008046 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008047 ? ssl->handshake->sni_authmode
8048 : mbedtls_ssl_conf_get_authmode( ssl->conf );
8049#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02008050 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008051#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008052#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8053 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
8054 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
8055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058#if defined(MBEDTLS_SSL_RENEGOTIATION)
8059 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008062 ssl->renego_records_seen = 0;
8063 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008064#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008065
8066 /*
8067 * Free the previous session and switch in the current one
8068 */
Paul Bakker0a597072012-09-25 21:55:46 +00008069 if( ssl->session )
8070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01008072 /* RFC 7366 3.1: keep the EtM state */
8073 ssl->session_negotiate->encrypt_then_mac =
8074 ssl->session->encrypt_then_mac;
8075#endif
8076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 mbedtls_ssl_session_free( ssl->session );
8078 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00008079 }
8080 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00008081 ssl->session_negotiate = NULL;
8082
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008083#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00008084 /*
8085 * Add cache entry
8086 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008087 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02008088 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008089 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008090 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008091 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008093 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008094#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008095
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008096 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8097 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8098#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8099 crt_expected == SSL_CERTIFICATE_SKIP )
8100#else
8101 1 )
8102#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008103 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008104 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008105 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8106 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8107#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8108 crt_expected == SSL_CERTIFICATE_SKIP )
8109#else
8110 1 )
8111#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008112 {
8113 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8114 }
8115 else
8116 {
8117 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8118 goto cleanup;
8119 }
8120 }
8121
Jarno Lamsae1621d42019-12-19 08:58:56 +02008122#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008123 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008124 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008125 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008126 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008127 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008128 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008129 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008130 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008131 }
8132 else
8133 {
8134 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008135 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008136 }
8137 }
8138#endif
8139
8140 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8141 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008142 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008143 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8144 {
8145 ret = 0;
8146 }
8147 else
8148 {
8149 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008150 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008151 }
8152 }
8153 else
8154 {
8155 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008156 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008157 }
8158
Jarno Lamsa06164052019-12-19 14:40:36 +02008159 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8160 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8161 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8162 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008163 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008164 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8165 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8166 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8167 {
8168 ret = 0;
8169 }
8170 else
8171 {
8172 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8173 goto cleanup;
8174 }
8175 }
8176 else
8177 {
8178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8180 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8181 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8182 }
8183
8184cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008186 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008187 ssl->handshake->flight != NULL )
8188 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008189 /* Cancel handshake timer */
8190 ssl_set_timer( ssl, 0 );
8191
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008192 /* Keep last flight around in case we need to resend it:
8193 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008195 }
8196 else
8197#endif
8198 ssl_handshake_wrapup_free_hs_transform( ssl );
8199
Jarno Lamsa2b205162019-11-12 15:36:21 +02008200 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008203 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008204}
8205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008207{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008208 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008211
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008212 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008213
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008214 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8215 mbedtls_ssl_suite_get_mac(
8216 mbedtls_ssl_ciphersuite_from_id(
8217 mbedtls_ssl_session_get_ciphersuite(
8218 ssl->session_negotiate ) ) ),
8219 ssl, ssl->out_msg + 4,
8220 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008221
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008222 /*
8223 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8224 * may define some other value. Currently (early 2016), no defined
8225 * ciphersuite does this (and this is unlikely to change as activity has
8226 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8227 */
Hanno Becker2881d802019-05-22 14:44:53 +01008228 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008231 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008232 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008233#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008234
Paul Bakker5121ce52009-01-03 21:22:43 +00008235 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8237 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008238
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008239#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008240 /*
8241 * In case of session resuming, invert the client and server
8242 * ChangeCipherSpec messages order.
8243 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008244 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008247 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8248 MBEDTLS_SSL_IS_CLIENT )
8249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008251 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008252#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008254 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8255 MBEDTLS_SSL_IS_SERVER )
8256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008258 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008259#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008260 }
8261 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008262#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008263 {
8264 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8265 {
8266 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8267 }
8268 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8269 {
8270 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8271 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008272 else
8273 {
8274 ssl->state = MBEDTLS_SSL_INVALID;
8275 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008277
Paul Bakker48916f92012-09-16 19:57:18 +00008278 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008279 * Switch to our negotiated transform and session parameters for outbound
8280 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008285 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008286 {
8287 unsigned char i;
8288
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008289 /* Remember current epoch settings for resending */
8290 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008291 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008292
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008293 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008294 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008295
8296 /* Increment epoch */
8297 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008298 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008299 break;
8300
8301 /* The loop goes to its end iff the counter is wrapping */
8302 if( i == 0 )
8303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8305 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008306 }
8307 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008308 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008310#if defined(MBEDTLS_SSL_PROTO_TLS)
8311 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008312 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008313 }
8314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008315
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008316 ssl->transform_out = ssl->transform_negotiate;
8317 ssl->session_out = ssl->session_negotiate;
8318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8320 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8325 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008326 }
8327 }
8328#endif
8329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008331 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008333#endif
8334
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008335 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008336 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008338 return( ret );
8339 }
8340
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008342 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008343 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8344 {
8345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8346 return( ret );
8347 }
8348#endif
8349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008351
8352 return( 0 );
8353}
8354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008356#define SSL_MAX_HASH_LEN 36
8357#else
8358#define SSL_MAX_HASH_LEN 12
8359#endif
8360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008362{
Paul Bakker23986e52011-04-24 08:57:21 +00008363 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008364 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008365 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008368
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008369 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8370 mbedtls_ssl_suite_get_mac(
8371 mbedtls_ssl_ciphersuite_from_id(
8372 mbedtls_ssl_session_get_ciphersuite(
8373 ssl->session_negotiate ) ) ),
8374 ssl, buf,
8375 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008376
Hanno Becker327c93b2018-08-15 13:56:18 +01008377 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008380 return( ret );
8381 }
8382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008386 mbedtls_ssl_pend_fatal_alert( ssl,
8387 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008389 }
8390
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008391 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008393 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008394 hash_len = 36;
8395 else
8396#endif
8397 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8400 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008403 mbedtls_ssl_pend_fatal_alert( ssl,
8404 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008406 }
8407
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008408 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008409 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008412 mbedtls_ssl_pend_fatal_alert( ssl,
8413 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008415 }
8416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008418 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008419 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008420#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008421
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008422#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008423 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008426 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008430 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008432#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008433 }
8434 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008435#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008436 {
8437 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8438 {
8439 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8440 }
8441 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8442 {
8443 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8444 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008445 else
8446 {
8447 ssl->state = MBEDTLS_SSL_INVALID;
8448 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008452 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008454#endif
8455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008457
8458 return( 0 );
8459}
8460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008462{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8466 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8467 mbedtls_md5_init( &handshake->fin_md5 );
8468 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008469 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8470 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008471#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8473#if defined(MBEDTLS_SHA256_C)
8474 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008475 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008476#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477#if defined(MBEDTLS_SHA512_C)
8478 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008479 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008480#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008482
Hanno Becker7e5437a2017-04-28 17:15:26 +01008483#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8484 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8485 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8486#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008488#if defined(MBEDTLS_DHM_C)
8489 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008491#if defined(MBEDTLS_ECDH_C)
8492 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008493#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008494#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008495 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008496#if defined(MBEDTLS_SSL_CLI_C)
8497 handshake->ecjpake_cache = NULL;
8498 handshake->ecjpake_cache_len = 0;
8499#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008500#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008501
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008502#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008503 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008504#endif
8505
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008506#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8507 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8508#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008509
8510#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8511 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8512 mbedtls_pk_init( &handshake->peer_pubkey );
8513#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008514}
8515
Hanno Becker611a83b2018-01-03 14:27:32 +00008516void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8521 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008522
Hanno Becker92231322018-01-03 15:32:51 +00008523#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008524 mbedtls_md_init( &transform->md_ctx_enc );
8525 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008526#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008527}
8528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008530{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008532}
8533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008535{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008536 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008537 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008539 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008541 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008542 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008543
8544 /*
8545 * Either the pointers are now NULL or cleared properly and can be freed.
8546 * Now allocate missing structures.
8547 */
8548 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008549 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008550 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008551 }
Paul Bakker48916f92012-09-16 19:57:18 +00008552
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008553 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008554 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008555 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008556 }
Paul Bakker48916f92012-09-16 19:57:18 +00008557
Paul Bakker82788fb2014-10-20 13:59:19 +02008558 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008559 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008560 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008561 }
Paul Bakker48916f92012-09-16 19:57:18 +00008562
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008563 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008564 if( ssl->handshake == NULL ||
8565 ssl->transform_negotiate == NULL ||
8566 ssl->session_negotiate == NULL )
8567 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570 mbedtls_free( ssl->handshake );
8571 mbedtls_free( ssl->transform_negotiate );
8572 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008573
8574 ssl->handshake = NULL;
8575 ssl->transform_negotiate = NULL;
8576 ssl->session_negotiate = NULL;
8577
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008578 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008579 }
8580
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008581 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008583 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008584 ssl_handshake_params_init( ssl->handshake );
8585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008587 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008588 {
8589 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008590
Hanno Becker2d9623f2019-06-13 12:07:05 +01008591 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008592 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8593 else
8594 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8595 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008596#endif
8597
Paul Bakker48916f92012-09-16 19:57:18 +00008598 return( 0 );
8599}
8600
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008601#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008602/* Dummy cookie callbacks for defaults */
8603static int ssl_cookie_write_dummy( void *ctx,
8604 unsigned char **p, unsigned char *end,
8605 const unsigned char *cli_id, size_t cli_id_len )
8606{
8607 ((void) ctx);
8608 ((void) p);
8609 ((void) end);
8610 ((void) cli_id);
8611 ((void) cli_id_len);
8612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008614}
8615
8616static int ssl_cookie_check_dummy( void *ctx,
8617 const unsigned char *cookie, size_t cookie_len,
8618 const unsigned char *cli_id, size_t cli_id_len )
8619{
8620 ((void) ctx);
8621 ((void) cookie);
8622 ((void) cookie_len);
8623 ((void) cli_id);
8624 ((void) cli_id_len);
8625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008627}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008628#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008629
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008630/* Once ssl->out_hdr as the address of the beginning of the
8631 * next outgoing record is set, deduce the other pointers.
8632 *
8633 * Note: For TLS, we save the implicit record sequence number
8634 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8635 * and the caller has to make sure there's space for this.
8636 */
8637
8638static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8639 mbedtls_ssl_transform *transform )
8640{
8641#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008642 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008643 {
8644 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008645#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008646 ssl->out_cid = ssl->out_ctr + 8;
8647 ssl->out_len = ssl->out_cid;
8648 if( transform != NULL )
8649 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008650#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008651 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008652#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008653 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008654 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008655 MBEDTLS_SSL_TRANSPORT_ELSE
8656#endif /* MBEDTLS_SSL_PROTO_DTLS */
8657#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008658 {
8659 ssl->out_ctr = ssl->out_hdr - 8;
8660 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008662 ssl->out_cid = ssl->out_len;
8663#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008664 ssl->out_iv = ssl->out_hdr + 5;
8665 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008666#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008667
8668 /* Adjust out_msg to make space for explicit IV, if used. */
8669 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008670 mbedtls_ssl_ver_geq(
8671 mbedtls_ssl_get_minor_ver( ssl ),
8672 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008673 {
8674 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8675 }
8676 else
8677 ssl->out_msg = ssl->out_iv;
8678}
8679
8680/* Once ssl->in_hdr as the address of the beginning of the
8681 * next incoming record is set, deduce the other pointers.
8682 *
8683 * Note: For TLS, we save the implicit record sequence number
8684 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8685 * and the caller has to make sure there's space for this.
8686 */
8687
Hanno Beckerf5970a02019-05-08 09:38:41 +01008688static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008689{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008690 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008691 * of unprotected TLS/DTLS records, with ssl->in_msg
8692 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008693 *
8694 * When decrypting a protected record, ssl->in_msg
8695 * will be shifted to point to the beginning of the
8696 * record plaintext.
8697 */
8698
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008700 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008701 {
Hanno Becker70e79282019-05-03 14:34:53 +01008702 /* This sets the header pointers to match records
8703 * without CID. When we receive a record containing
8704 * a CID, the fields are shifted accordingly in
8705 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008706 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008707#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008708 ssl->in_cid = ssl->in_ctr + 8;
8709 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008710#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008711 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008712#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008713 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008714 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008715 MBEDTLS_SSL_TRANSPORT_ELSE
8716#endif /* MBEDTLS_SSL_PROTO_DTLS */
8717#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008718 {
8719 ssl->in_ctr = ssl->in_hdr - 8;
8720 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008721#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008722 ssl->in_cid = ssl->in_len;
8723#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008724 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008725 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008726#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008727}
8728
Paul Bakker5121ce52009-01-03 21:22:43 +00008729/*
8730 * Initialize an SSL context
8731 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008732void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8733{
8734 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8735}
8736
8737/*
8738 * Setup an SSL context
8739 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008740
8741static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8742{
8743 /* Set the incoming and outgoing record pointers. */
8744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008745 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008746 {
8747 ssl->out_hdr = ssl->out_buf;
8748 ssl->in_hdr = ssl->in_buf;
8749 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008750 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008751#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008752#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008753 {
8754 ssl->out_hdr = ssl->out_buf + 8;
8755 ssl->in_hdr = ssl->in_buf + 8;
8756 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008757#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008758
8759 /* Derive other internal pointers. */
8760 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008761 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008762}
8763
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008764int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008765 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008766{
Paul Bakker48916f92012-09-16 19:57:18 +00008767 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008768
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008769 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008770
Hanno Beckeref982d52019-07-23 15:56:18 +01008771#if defined(MBEDTLS_USE_TINYCRYPT)
8772 uECC_set_rng( &uecc_rng_wrapper );
8773#endif
8774
Paul Bakker62f2dee2012-09-28 07:31:51 +00008775 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008776 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008777 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008778
8779 /* Set to NULL in case of an error condition */
8780 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008781
Angus Grattond8213d02016-05-25 20:56:48 +10008782 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8783 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008784 {
Angus Grattond8213d02016-05-25 20:56:48 +10008785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008786 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008787 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008788 }
8789
8790 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8791 if( ssl->out_buf == NULL )
8792 {
8793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008794 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008795 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008796 }
8797
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008798 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008799
Paul Bakker48916f92012-09-16 19:57:18 +00008800 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008801 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008802
Hanno Beckerc8f52992019-07-25 11:15:08 +01008803 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008804
Paul Bakker5121ce52009-01-03 21:22:43 +00008805 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008806
8807error:
8808 mbedtls_free( ssl->in_buf );
8809 mbedtls_free( ssl->out_buf );
8810
8811 ssl->conf = NULL;
8812
8813 ssl->in_buf = NULL;
8814 ssl->out_buf = NULL;
8815
8816 ssl->in_hdr = NULL;
8817 ssl->in_ctr = NULL;
8818 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008819 ssl->in_msg = NULL;
8820
8821 ssl->out_hdr = NULL;
8822 ssl->out_ctr = NULL;
8823 ssl->out_len = NULL;
8824 ssl->out_iv = NULL;
8825 ssl->out_msg = NULL;
8826
k-stachowiak9f7798e2018-07-31 16:52:32 +02008827 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008828}
8829
8830/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008831 * Reset an initialized and used SSL context for re-use while retaining
8832 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008833 *
8834 * If partial is non-zero, keep data in the input buffer and client ID.
8835 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008836 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008837static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008838{
Paul Bakker48916f92012-09-16 19:57:18 +00008839 int ret;
8840
Hanno Becker7e772132018-08-10 12:38:21 +01008841#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8842 !defined(MBEDTLS_SSL_SRV_C)
8843 ((void) partial);
8844#endif
8845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008847
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008848 /* Cancel any possibly running timer */
8849 ssl_set_timer( ssl, 0 );
8850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008851#if defined(MBEDTLS_SSL_RENEGOTIATION)
8852 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008853 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008854
8855 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008856 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8857 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008860
Paul Bakker7eb013f2011-10-06 12:37:39 +00008861 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008862 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008863
8864 ssl->in_msgtype = 0;
8865 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008867 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008868 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008871 ssl_dtls_replay_reset( ssl );
8872#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008873
8874 ssl->in_hslen = 0;
8875 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008876
8877 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008878
8879 ssl->out_msgtype = 0;
8880 ssl->out_msglen = 0;
8881 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8883 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008884 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008885#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008886
Hanno Becker19859472018-08-06 09:40:20 +01008887 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8888
Paul Bakker48916f92012-09-16 19:57:18 +00008889 ssl->transform_in = NULL;
8890 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008891
Hanno Becker78640902018-08-13 16:35:15 +01008892 ssl->session_in = NULL;
8893 ssl->session_out = NULL;
8894
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008895 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008896
8897#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008898 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008899#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8900 {
8901 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008902 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008903 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008905#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8906 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8909 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8912 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008913 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008914 }
8915#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008916
Paul Bakker48916f92012-09-16 19:57:18 +00008917 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008919 mbedtls_ssl_transform_free( ssl->transform );
8920 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008921 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008922 }
Paul Bakker48916f92012-09-16 19:57:18 +00008923
Paul Bakkerc0463502013-02-14 11:19:38 +01008924 if( ssl->session )
8925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008926 mbedtls_ssl_session_free( ssl->session );
8927 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008928 ssl->session = NULL;
8929 }
8930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008932 ssl->alpn_chosen = NULL;
8933#endif
8934
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008935#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008936#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008937 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008938#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008939 {
8940 mbedtls_free( ssl->cli_id );
8941 ssl->cli_id = NULL;
8942 ssl->cli_id_len = 0;
8943 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008944#endif
8945
Paul Bakker48916f92012-09-16 19:57:18 +00008946 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8947 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008948
8949 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008950}
8951
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008952/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008953 * Reset an initialized and used SSL context for re-use while retaining
8954 * all application-set variables, function pointers and data.
8955 */
8956int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8957{
8958 return( ssl_session_reset_int( ssl, 0 ) );
8959}
8960
8961/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008962 * SSL set accessors
8963 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008964#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008965void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008966{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008967 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008968}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008969#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008970
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008971void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008973 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008974}
8975
Hanno Becker7f376f42019-06-12 16:20:48 +01008976#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8977 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008978void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008979{
Hanno Becker7f376f42019-06-12 16:20:48 +01008980 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008981}
Hanno Becker7f376f42019-06-12 16:20:48 +01008982#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008983
Hanno Beckerde671542019-06-12 16:30:46 +01008984#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8985 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8986void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8987 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008988{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008989 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008990}
Hanno Beckerde671542019-06-12 16:30:46 +01008991#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008994
Hanno Becker1841b0a2018-08-24 11:13:57 +01008995void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8996 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008997{
8998 ssl->disable_datagram_packing = !allow_packing;
8999}
9000
Hanno Becker1f835fa2019-06-13 10:14:59 +01009001#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
9002 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01009003void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
9004 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02009005{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009006 conf->hs_timeout_min = min;
9007 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02009008}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009009#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
9010 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
9011void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
9012 uint32_t min, uint32_t max )
9013{
9014 ((void) conf);
9015 ((void) min);
9016 ((void) max);
9017}
9018#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
9019 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
9020
9021#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02009022
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009023void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00009024{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01009025#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
9026 conf->authmode = authmode;
9027#else
9028 ((void) conf);
9029 ((void) authmode);
9030#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009031}
9032
Hanno Becker9ec3fe02019-07-01 17:36:12 +01009033#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
9034 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009035void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009036 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00009037 void *p_vrfy )
9038{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009039 conf->f_vrfy = f_vrfy;
9040 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00009041}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01009042#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00009043
Hanno Beckerece325c2019-06-13 15:39:27 +01009044#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009045void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00009046 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00009047 void *p_rng )
9048{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01009049 conf->f_rng = f_rng;
9050 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00009051}
Hanno Beckerece325c2019-06-13 15:39:27 +01009052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00009053
Hanno Becker14a4a442019-07-02 17:00:34 +01009054#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009055void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02009056 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00009057 void *p_dbg )
9058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009059 conf->f_dbg = f_dbg;
9060 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00009061}
Hanno Becker14a4a442019-07-02 17:00:34 +01009062#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009063
Hanno Beckera58a8962019-06-13 16:11:15 +01009064#if !defined(MBEDTLS_SSL_CONF_RECV) && \
9065 !defined(MBEDTLS_SSL_CONF_SEND) && \
9066 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009067void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009068 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00009069 mbedtls_ssl_send_t *f_send,
9070 mbedtls_ssl_recv_t *f_recv,
9071 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009072{
Hanno Beckera58a8962019-06-13 16:11:15 +01009073 ssl->p_bio = p_bio;
9074 ssl->f_send = f_send;
9075 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009076 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009077}
Hanno Beckera58a8962019-06-13 16:11:15 +01009078#else
9079void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
9080 void *p_bio )
9081{
9082 ssl->p_bio = p_bio;
9083}
9084#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009085
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02009086#if defined(MBEDTLS_SSL_PROTO_DTLS)
9087void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
9088{
9089 ssl->mtu = mtu;
9090}
9091#endif
9092
Hanno Becker1f835fa2019-06-13 10:14:59 +01009093#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009094void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009095{
9096 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009097}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009098#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009099
Hanno Becker0ae6b242019-06-13 16:45:36 +01009100#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9101 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009102void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9103 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009104 mbedtls_ssl_set_timer_t *f_set_timer,
9105 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009106{
9107 ssl->p_timer = p_timer;
9108 ssl->f_set_timer = f_set_timer;
9109 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009110 /* Make sure we start with no timer running */
9111 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009112}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009113#else
9114void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9115 void *p_timer )
9116{
9117 ssl->p_timer = p_timer;
9118 /* Make sure we start with no timer running */
9119 ssl_set_timer( ssl, 0 );
9120}
9121#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009122
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009123#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009124void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009125 void *p_cache,
9126 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9127 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009128{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009129 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009130 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009131 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009132}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009133#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009134
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009135#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009137{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009138 int ret;
9139
9140 if( ssl == NULL ||
9141 session == NULL ||
9142 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009143 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009146 }
9147
Hanno Becker58fccf22019-02-06 14:30:46 +00009148 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9149 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009150 return( ret );
9151
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009152 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009153
9154 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009155}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009156#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009157
Hanno Becker73f4cb12019-06-27 13:51:07 +01009158#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009159void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009160 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009161{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009162 conf->ciphersuite_list[0] = ciphersuites;
9163 conf->ciphersuite_list[1] = ciphersuites;
9164 conf->ciphersuite_list[2] = ciphersuites;
9165 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009166}
9167
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009168void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009169 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009170 int major, int minor )
9171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009173 return;
9174
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009175 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9176 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9177 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009178 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009179 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009180
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009181 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9182 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009183}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009184#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009186#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009187void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009188 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009189{
9190 conf->cert_profile = profile;
9191}
9192
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009193/* Append a new keycert entry to a (possibly empty) list */
9194static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9195 mbedtls_x509_crt *cert,
9196 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009197{
niisato8ee24222018-06-25 19:05:48 +09009198 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009199
niisato8ee24222018-06-25 19:05:48 +09009200 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9201 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009202 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009203
niisato8ee24222018-06-25 19:05:48 +09009204 new_cert->cert = cert;
9205 new_cert->key = key;
9206 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009207
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009208 /* Update head is the list was null, else add to the end */
9209 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009210 {
niisato8ee24222018-06-25 19:05:48 +09009211 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009212 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009213 else
9214 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009215 mbedtls_ssl_key_cert *cur = *head;
9216 while( cur->next != NULL )
9217 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009218 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009219 }
9220
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009221 return( 0 );
9222}
9223
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009224int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009225 mbedtls_x509_crt *own_cert,
9226 mbedtls_pk_context *pk_key )
9227{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009228 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009229}
9230
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009231void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009232 mbedtls_x509_crt *ca_chain,
9233 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009234{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009235 conf->ca_chain = ca_chain;
9236 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009238#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009239
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009240#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9241int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9242 mbedtls_x509_crt *own_cert,
9243 mbedtls_pk_context *pk_key )
9244{
9245 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9246 own_cert, pk_key ) );
9247}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009248
9249void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9250 mbedtls_x509_crt *ca_chain,
9251 mbedtls_x509_crl *ca_crl )
9252{
9253 ssl->handshake->sni_ca_chain = ca_chain;
9254 ssl->handshake->sni_ca_crl = ca_crl;
9255}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009256
9257void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9258 int authmode )
9259{
9260 ssl->handshake->sni_authmode = authmode;
9261}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009262#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9263
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009264#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009265/*
9266 * Set EC J-PAKE password for current handshake
9267 */
9268int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9269 const unsigned char *pw,
9270 size_t pw_len )
9271{
9272 mbedtls_ecjpake_role role;
9273
Janos Follath8eb64132016-06-03 15:40:57 +01009274 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9276
Hanno Becker2d9623f2019-06-13 12:07:05 +01009277 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009278 role = MBEDTLS_ECJPAKE_SERVER;
9279 else
9280 role = MBEDTLS_ECJPAKE_CLIENT;
9281
9282 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9283 role,
9284 MBEDTLS_MD_SHA256,
9285 MBEDTLS_ECP_DP_SECP256R1,
9286 pw, pw_len ) );
9287}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009288#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009290#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009291int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009292 const unsigned char *psk, size_t psk_len,
9293 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009294{
Paul Bakker6db455e2013-09-18 17:29:31 +02009295 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009300
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009301 /* Identity len will be encoded on two bytes */
9302 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009303 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009304 {
9305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9306 }
9307
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009308 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009309 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009310 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009311
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009312 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009313 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009314 conf->psk_len = 0;
9315 }
9316 if( conf->psk_identity != NULL )
9317 {
9318 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009319 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009320 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009321 }
9322
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009323 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9324 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009325 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009326 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009327 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009328 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009329 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009331 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009332
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009333 conf->psk_len = psk_len;
9334 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009335
Teppo Järvelin91d79382019-10-02 09:09:31 +03009336 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9337 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009338
9339 return( 0 );
9340}
9341
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009342int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9343 const unsigned char *psk, size_t psk_len )
9344{
9345 if( psk == NULL || ssl->handshake == NULL )
9346 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9347
9348 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9350
9351 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009352 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009353 mbedtls_platform_zeroize( ssl->handshake->psk,
9354 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009355 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009356 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009357 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009358
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009359 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009360 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009361
9362 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009363 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009364
9365 return( 0 );
9366}
9367
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009368void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009370 size_t),
9371 void *p_psk )
9372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009373 conf->f_psk = f_psk;
9374 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009375}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009376#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009377
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009378#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009379
9380#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009381int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009382{
9383 int ret;
9384
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009385 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9386 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9387 {
9388 mbedtls_mpi_free( &conf->dhm_P );
9389 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009390 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009392
9393 return( 0 );
9394}
Hanno Becker470a8c42017-10-04 15:28:46 +01009395#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009396
Hanno Beckera90658f2017-10-04 15:29:08 +01009397int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9398 const unsigned char *dhm_P, size_t P_len,
9399 const unsigned char *dhm_G, size_t G_len )
9400{
9401 int ret;
9402
9403 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9404 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9405 {
9406 mbedtls_mpi_free( &conf->dhm_P );
9407 mbedtls_mpi_free( &conf->dhm_G );
9408 return( ret );
9409 }
9410
9411 return( 0 );
9412}
Paul Bakker5121ce52009-01-03 21:22:43 +00009413
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009414int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009415{
9416 int ret;
9417
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009418 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9419 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9420 {
9421 mbedtls_mpi_free( &conf->dhm_P );
9422 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009423 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009424 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009425
9426 return( 0 );
9427}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009428#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009429
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009430#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9431/*
9432 * Set the minimum length for Diffie-Hellman parameters
9433 */
9434void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9435 unsigned int bitlen )
9436{
9437 conf->dhm_min_bitlen = bitlen;
9438}
9439#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9440
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009441#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009442/*
9443 * Set allowed/preferred hashes for handshake signatures
9444 */
9445void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9446 const int *hashes )
9447{
Hanno Becker56595f42019-06-19 16:31:38 +01009448#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009449 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009450#else
9451 ((void) conf);
9452 ((void) hashes);
9453#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009454}
Hanno Becker947194e2017-04-07 13:25:49 +01009455#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009456
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009457#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009458#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009459/*
9460 * Set the allowed elliptic curves
9461 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009462void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009463 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009464{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009465 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009466}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009467#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009468#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009469
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009470#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009471int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009472{
Hanno Becker947194e2017-04-07 13:25:49 +01009473 /* Initialize to suppress unnecessary compiler warning */
9474 size_t hostname_len = 0;
9475
9476 /* Check if new hostname is valid before
9477 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009478 if( hostname != NULL )
9479 {
9480 hostname_len = strlen( hostname );
9481
9482 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9484 }
9485
9486 /* Now it's clear that we will overwrite the old hostname,
9487 * so we can free it safely */
9488
9489 if( ssl->hostname != NULL )
9490 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009491 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009492 mbedtls_free( ssl->hostname );
9493 }
9494
9495 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009496
Paul Bakker5121ce52009-01-03 21:22:43 +00009497 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009498 {
9499 ssl->hostname = NULL;
9500 }
9501 else
9502 {
9503 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009504 if( ssl->hostname == NULL )
9505 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009506
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009507 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9508 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009509
Hanno Becker947194e2017-04-07 13:25:49 +01009510 ssl->hostname[hostname_len] = '\0';
9511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009512
9513 return( 0 );
9514}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009515#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009516
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009517#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009518void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009520 const unsigned char *, size_t),
9521 void *p_sni )
9522{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009523 conf->f_sni = f_sni;
9524 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009529int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009530{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009531 size_t cur_len, tot_len;
9532 const char **p;
9533
9534 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009535 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9536 * MUST NOT be truncated."
9537 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009538 */
9539 tot_len = 0;
9540 for( p = protos; *p != NULL; p++ )
9541 {
9542 cur_len = strlen( *p );
9543 tot_len += cur_len;
9544
9545 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009547 }
9548
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009549 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009550
9551 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009552}
9553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009555{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009556 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009559
Hanno Becker33b9b252019-07-05 11:23:25 +01009560#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9561 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9562void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9563 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009565 conf->max_major_ver = major;
9566 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009567}
Hanno Becker33b9b252019-07-05 11:23:25 +01009568#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9569 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009570
Hanno Becker33b9b252019-07-05 11:23:25 +01009571#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9572 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9573void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9574 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009576 conf->min_major_ver = major;
9577 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009578}
Hanno Becker33b9b252019-07-05 11:23:25 +01009579#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9580 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009583void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009584{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009585 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009586}
9587#endif
9588
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009589#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009590void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9591 char cert_req_ca_list )
9592{
9593 conf->cert_req_ca_list = cert_req_ca_list;
9594}
9595#endif
9596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009598void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009599{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009600 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009601}
9602#endif
9603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009605#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009606void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009608 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009609}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009610#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9611#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009612void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009613 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009614{
9615 conf->enforce_extended_master_secret = ems_enf;
9616}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009617#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009618#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009619
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009620#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009621void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009623 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009624}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009625#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009628int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009629{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009631 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009634 }
9635
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009636 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009637
9638 return( 0 );
9639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009643void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009644{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009645 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009646}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009650void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009651{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009652 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009653}
9654#endif
9655
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009656#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009657void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009659 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009660}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009661#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009664void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009665{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009666 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009667}
9668
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009669void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009670{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009671 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009672}
9673
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009674void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009675 const unsigned char period[8] )
9676{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009677 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009682#if defined(MBEDTLS_SSL_CLI_C)
9683void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009684{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009685 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009686}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009687#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009688
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009689#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009690void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9691 mbedtls_ssl_ticket_write_t *f_ticket_write,
9692 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9693 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009694{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009695 conf->f_ticket_write = f_ticket_write;
9696 conf->f_ticket_parse = f_ticket_parse;
9697 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009698}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009699#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009701
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009702#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9703void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9704 mbedtls_ssl_export_keys_t *f_export_keys,
9705 void *p_export_keys )
9706{
9707 conf->f_export_keys = f_export_keys;
9708 conf->p_export_keys = p_export_keys;
9709}
9710#endif
9711
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009712#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009713void mbedtls_ssl_conf_async_private_cb(
9714 mbedtls_ssl_config *conf,
9715 mbedtls_ssl_async_sign_t *f_async_sign,
9716 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9717 mbedtls_ssl_async_resume_t *f_async_resume,
9718 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009719 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009720{
9721 conf->f_async_sign_start = f_async_sign;
9722 conf->f_async_decrypt_start = f_async_decrypt;
9723 conf->f_async_resume = f_async_resume;
9724 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009725 conf->p_async_config_data = async_config_data;
9726}
9727
Gilles Peskine8f97af72018-04-26 11:46:10 +02009728void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9729{
9730 return( conf->p_async_config_data );
9731}
9732
Gilles Peskine1febfef2018-04-30 11:54:39 +02009733void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009734{
9735 if( ssl->handshake == NULL )
9736 return( NULL );
9737 else
9738 return( ssl->handshake->user_async_ctx );
9739}
9740
Gilles Peskine1febfef2018-04-30 11:54:39 +02009741void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009742 void *ctx )
9743{
9744 if( ssl->handshake != NULL )
9745 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009746}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009747#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009748
Paul Bakker5121ce52009-01-03 21:22:43 +00009749/*
9750 * SSL get accessors
9751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009753{
9754 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9755}
9756
Hanno Becker8b170a02017-10-10 11:51:19 +01009757int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9758{
9759 /*
9760 * Case A: We're currently holding back
9761 * a message for further processing.
9762 */
9763
9764 if( ssl->keep_current_message == 1 )
9765 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009767 return( 1 );
9768 }
9769
9770 /*
9771 * Case B: Further records are pending in the current datagram.
9772 */
9773
9774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009775 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009776 ssl->in_left > ssl->next_record_offset )
9777 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009778 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009779 return( 1 );
9780 }
9781#endif /* MBEDTLS_SSL_PROTO_DTLS */
9782
9783 /*
9784 * Case C: A handshake message is being processed.
9785 */
9786
Hanno Becker8b170a02017-10-10 11:51:19 +01009787 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9788 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009790 return( 1 );
9791 }
9792
9793 /*
9794 * Case D: An application data message is being processed
9795 */
9796 if( ssl->in_offt != NULL )
9797 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009799 return( 1 );
9800 }
9801
9802 /*
9803 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009804 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009805 * we implement support for multiple alerts in single records.
9806 */
9807
9808 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9809 return( 0 );
9810}
9811
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009812uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009813{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009814 if( ssl->session != NULL )
9815 return( ssl->session->verify_result );
9816
9817 if( ssl->session_negotiate != NULL )
9818 return( ssl->session_negotiate->verify_result );
9819
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009820 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009821}
9822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009824{
Hanno Beckere02758c2019-06-26 15:31:31 +01009825 int suite;
9826
Paul Bakker926c8e42013-03-06 10:23:34 +01009827 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009828 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009829
Hanno Beckere02758c2019-06-26 15:31:31 +01009830 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9831 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009832}
9833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009835{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009837 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009838 {
Hanno Becker2881d802019-05-22 14:44:53 +01009839 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009842 return( "DTLSv1.0" );
9843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009845 return( "DTLSv1.2" );
9846
9847 default:
9848 return( "unknown (DTLS)" );
9849 }
9850 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009851 MBEDTLS_SSL_TRANSPORT_ELSE
9852#endif /* MBEDTLS_SSL_PROTO_DTLS */
9853#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009854 {
Hanno Becker2881d802019-05-22 14:44:53 +01009855 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009856 {
9857 case MBEDTLS_SSL_MINOR_VERSION_0:
9858 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009859
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009860 case MBEDTLS_SSL_MINOR_VERSION_1:
9861 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009862
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009863 case MBEDTLS_SSL_MINOR_VERSION_2:
9864 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009865
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009866 case MBEDTLS_SSL_MINOR_VERSION_3:
9867 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009868
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009869 default:
9870 return( "unknown" );
9871 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009872 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009873#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009874}
9875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009876int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009877{
Hanno Becker3136ede2018-08-17 15:28:19 +01009878 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009880
Hanno Becker43395762019-05-03 14:46:38 +01009881 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9882
Hanno Becker78640902018-08-13 16:35:15 +01009883 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009884 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009886#if defined(MBEDTLS_ZLIB_SUPPORT)
9887 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9888 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009889#endif
9890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009892 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009893#if defined(MBEDTLS_GCM_C) || \
9894 defined(MBEDTLS_CCM_C) || \
9895 defined(MBEDTLS_CHACHAPOLY_C)
9896#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009898#endif
9899#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009901#endif
9902#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009903 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009904#endif
9905 transform_expansion =
9906 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009907 break;
9908
Hanno Beckera9d5c452019-07-25 16:47:12 +01009909#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9910 MBEDTLS_CHACHAPOLY_C */
9911
9912#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9913 case MBEDTLS_MODE_STREAM:
9914 transform_expansion = transform->maclen;
9915 break;
9916#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9917
9918#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009920 {
9921 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009922
9923 block_size = mbedtls_cipher_get_block_size(
9924 &transform->cipher_ctx_enc );
9925
Hanno Becker3136ede2018-08-17 15:28:19 +01009926 /* Expansion due to the addition of the MAC. */
9927 transform_expansion += transform->maclen;
9928
9929 /* Expansion due to the addition of CBC padding;
9930 * Theoretically up to 256 bytes, but we never use
9931 * more than the block size of the underlying cipher. */
9932 transform_expansion += block_size;
9933
9934 /* For TLS 1.1 or higher, an explicit IV is added
9935 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009936#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009937 if( mbedtls_ssl_ver_geq(
9938 mbedtls_ssl_get_minor_ver( ssl ),
9939 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9940 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009941 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009942 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009943#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009944
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009945 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009946 }
9947#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009948
9949 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009951 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009952 }
9953
Hanno Beckera5a2b082019-05-15 14:03:01 +01009954#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009955 if( transform->out_cid_len != 0 )
9956 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009957#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009958
Hanno Becker43395762019-05-03 14:46:38 +01009959 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009960}
9961
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009962#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9963size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9964{
9965 size_t max_len;
9966
9967 /*
9968 * Assume mfl_code is correct since it was checked when set
9969 */
Angus Grattond8213d02016-05-25 20:56:48 +10009970 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009971
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009972 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009973 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009974 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009975 {
Angus Grattond8213d02016-05-25 20:56:48 +10009976 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009977 }
9978
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009979 /* During a handshake, use the value being negotiated */
9980 if( ssl->session_negotiate != NULL &&
9981 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9982 {
9983 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9984 }
9985
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009986 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009987}
9988#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9989
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009990#if defined(MBEDTLS_SSL_PROTO_DTLS)
9991static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9992{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009993 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009994 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009995 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9996 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009997 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009998
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009999 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
10000 return( ssl->mtu );
10001
10002 if( ssl->mtu == 0 )
10003 return( ssl->handshake->mtu );
10004
10005 return( ssl->mtu < ssl->handshake->mtu ?
10006 ssl->mtu : ssl->handshake->mtu );
10007}
10008#endif /* MBEDTLS_SSL_PROTO_DTLS */
10009
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010010int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
10011{
10012 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
10013
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +020010014#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
10015 !defined(MBEDTLS_SSL_PROTO_DTLS)
10016 (void) ssl;
10017#endif
10018
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010019#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10020 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
10021
10022 if( max_len > mfl )
10023 max_len = mfl;
10024#endif
10025
10026#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +020010027 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010028 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +020010029 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010030 const int ret = mbedtls_ssl_get_record_expansion( ssl );
10031 const size_t overhead = (size_t) ret;
10032
10033 if( ret < 0 )
10034 return( ret );
10035
10036 if( mtu <= overhead )
10037 {
10038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
10039 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
10040 }
10041
10042 if( max_len > mtu - overhead )
10043 max_len = mtu - overhead;
10044 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +020010045#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010046
Hanno Becker0defedb2018-08-10 12:35:02 +010010047#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
10048 !defined(MBEDTLS_SSL_PROTO_DTLS)
10049 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010050#endif
10051
10052 return( (int) max_len );
10053}
10054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010055#if defined(MBEDTLS_X509_CRT_PARSE_C)
10056const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +000010057{
10058 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020010059 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +000010060
Hanno Beckerbfab9df2019-02-07 13:18:46 +000010061#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +020010062 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +000010063#else
10064 return( NULL );
10065#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +000010066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +000010068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +000010070int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
10071 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010072{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010073 if( ssl == NULL ||
10074 dst == NULL ||
10075 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +010010076 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010079 }
10080
Hanno Becker58fccf22019-02-06 14:30:46 +000010081 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010082}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010084
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +020010085const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
10086{
10087 if( ssl == NULL )
10088 return( NULL );
10089
10090 return( ssl->session );
10091}
10092
Paul Bakker5121ce52009-01-03 21:22:43 +000010093/*
Hanno Beckerb5352f02019-05-16 12:39:07 +010010094 * Define ticket header determining Mbed TLS version
10095 * and structure of the ticket.
10096 */
10097
Hanno Becker41527622019-05-16 12:50:45 +010010098/*
Hanno Becker26829e92019-05-28 14:30:45 +010010099 * Define bitflag determining compile-time settings influencing
10100 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010101 */
10102
Hanno Becker26829e92019-05-28 14:30:45 +010010103#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010104#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010105#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010106#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010107#endif /* MBEDTLS_HAVE_TIME */
10108
10109#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010110#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010111#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010112#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010113#endif /* MBEDTLS_X509_CRT_PARSE_C */
10114
10115#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010116#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010117#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010118#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010119#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10120
10121#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010122#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010123#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010124#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010125#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10126
10127#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010128#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010129#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010130#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010131#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10132
10133#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010134#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010135#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010136#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010137#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10138
Hanno Becker41527622019-05-16 12:50:45 +010010139#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10140#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10141#else
10142#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10143#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10144
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010145#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10146#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10147#else
10148#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10149#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10150
Hanno Becker88440552019-07-03 14:16:13 +010010151#if defined(MBEDTLS_ZLIB_SUPPORT)
10152#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10153#else
10154#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10155#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10156
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010157#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10158#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10159#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10160#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10161#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10162#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10163#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010164#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010165#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010166
Hanno Becker26829e92019-05-28 14:30:45 +010010167#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010168 ( (uint16_t) ( \
10169 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10170 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10171 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10172 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10173 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10174 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010175 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010176 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010177 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010178
Hanno Becker557fe9f2019-05-16 12:41:07 +010010179static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010180 MBEDTLS_VERSION_MAJOR,
10181 MBEDTLS_VERSION_MINOR,
10182 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010183 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10184 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010185};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010186
10187/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010188 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010189 * (in the presentation language of TLS, RFC 8446 section 3)
10190 *
Hanno Becker26829e92019-05-28 14:30:45 +010010191 * opaque mbedtls_version[3]; // major, minor, patch
10192 * opaque session_format[2]; // version-specific 16-bit field determining
10193 * // the format of the remaining
10194 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010195 *
10196 * Note: When updating the format, remember to keep
10197 * these version+format bytes.
10198 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010199 * // In this version, `session_format` determines
10200 * // the setting of those compile-time
10201 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010202 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010203 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010204 * uint8 ciphersuite[2]; // defined by the standard
10205 * uint8 compression; // 0 or 1
10206 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010207 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010208 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010209 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010210 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10211 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10212 * case disabled: uint8_t peer_cert_digest_type;
10213 * opaque peer_cert_digest<0..2^8-1>;
10214 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010215 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010216 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010217 * uint8 mfl_code; // up to 255 according to standard
10218 * uint8 trunc_hmac; // 0 or 1
10219 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010220 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010221 * The order is the same as in the definition of the structure, except
10222 * verify_result is put before peer_cert so that all mandatory fields come
10223 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010224 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010225static int ssl_session_save( const mbedtls_ssl_session *session,
10226 unsigned char omit_header,
10227 unsigned char *buf,
10228 size_t buf_len,
10229 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010230{
10231 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010232 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010233#if defined(MBEDTLS_HAVE_TIME)
10234 uint64_t start;
10235#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010236#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010237#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010238 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010239#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010240#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010241
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010242 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010243 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010244 /*
10245 * Add version identifier
10246 */
10247
10248 used += sizeof( ssl_serialized_session_header );
10249
10250 if( used <= buf_len )
10251 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010252 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010253 sizeof( ssl_serialized_session_header ) );
10254 p += sizeof( ssl_serialized_session_header );
10255 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010256 }
10257
10258 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010259 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010260 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010261#if defined(MBEDTLS_HAVE_TIME)
10262 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010263
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010264 if( used <= buf_len )
10265 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010266 start = (uint64_t) session->start;
10267
10268 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10269 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10270 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10271 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10272 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10273 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10274 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10275 *p++ = (unsigned char)( ( start ) & 0xFF );
10276 }
10277#endif /* MBEDTLS_HAVE_TIME */
10278
10279 /*
10280 * Basic mandatory fields
10281 */
Hanno Becker88440552019-07-03 14:16:13 +010010282 {
10283 size_t const ciphersuite_len = 2;
10284#if defined(MBEDTLS_ZLIB_SUPPORT)
10285 size_t const compression_len = 1;
10286#else
10287 size_t const compression_len = 0;
10288#endif
10289 size_t const id_len_len = 1;
10290 size_t const id_len = 32;
10291 size_t const master_len = 48;
10292 size_t const verif_result_len = 4;
10293
10294 size_t const basic_len =
10295 ciphersuite_len +
10296 compression_len +
10297 id_len_len +
10298 id_len +
10299 master_len +
10300 verif_result_len;
10301
10302 used += basic_len;
10303 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010304
10305 if( used <= buf_len )
10306 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010307 const int ciphersuite =
10308 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010309 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010310
Hanno Becker88440552019-07-03 14:16:13 +010010311#if defined(MBEDTLS_ZLIB_SUPPORT)
10312 *p++ = (unsigned char)(
10313 mbedtls_ssl_session_get_compression( session ) );
10314#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010315
10316 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010317 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10318 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010319 p += 32;
10320
Teppo Järvelin91d79382019-10-02 09:09:31 +030010321 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010322 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010323 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010324 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010325
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010326 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010327 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010328 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010329#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010330#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010331 if( session->peer_cert == NULL )
10332 cert_len = 0;
10333 else
10334 cert_len = session->peer_cert->raw.len;
10335
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010336 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010337
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010338 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010339 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010340 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010341
10342 if( session->peer_cert != NULL )
10343 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010344 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010345 p += cert_len;
10346 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010347 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010348
Hanno Becker5882dd02019-06-06 16:25:57 +010010349#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010350 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010351 if( session->peer_cert_digest != NULL )
10352 {
10353 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10354 if( used <= buf_len )
10355 {
10356 *p++ = (unsigned char) session->peer_cert_digest_type;
10357 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010358 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010359 session->peer_cert_digest_len );
10360 p += session->peer_cert_digest_len;
10361 }
10362 }
10363 else
10364 {
10365 used += 2;
10366 if( used <= buf_len )
10367 {
10368 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10369 *p++ = 0;
10370 }
10371 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010372#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010373#endif /* MBEDTLS_X509_CRT_PARSE_C */
10374
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010375 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010376 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010377 */
10378#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010379 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010380
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010381 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010382 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010383 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010384
10385 if( session->ticket != NULL )
10386 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010387 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010388 p += session->ticket_len;
10389 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010390
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010391 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010392 }
10393#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10394
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010395 /*
10396 * Misc extension-related info
10397 */
10398#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10399 used += 1;
10400
10401 if( used <= buf_len )
10402 *p++ = session->mfl_code;
10403#endif
10404
10405#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10406 used += 1;
10407
10408 if( used <= buf_len )
10409 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10410#endif
10411
10412#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10413 used += 1;
10414
10415 if( used <= buf_len )
10416 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10417#endif
10418
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010419 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010420 *olen = used;
10421
10422 if( used > buf_len )
10423 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010424
10425 return( 0 );
10426}
10427
10428/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010429 * Public wrapper for ssl_session_save()
10430 */
10431int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10432 unsigned char *buf,
10433 size_t buf_len,
10434 size_t *olen )
10435{
10436 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10437}
10438
10439/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010440 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010441 *
10442 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010443 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010444 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010445static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010446 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010447 const unsigned char *buf,
10448 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010449{
10450 const unsigned char *p = buf;
10451 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010452 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010453#if defined(MBEDTLS_HAVE_TIME)
10454 uint64_t start;
10455#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010456#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010457#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010458 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010459#endif
10460#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010461
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010462 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010463 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010464 /*
10465 * Check version identifier
10466 */
10467
10468 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10469 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10470
Teppo Järvelin0efac532019-10-04 13:21:08 +030010471 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010472 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010473 sizeof( ssl_serialized_session_header ) ) != 0 )
10474 {
10475 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10476 }
10477 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010478 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010479
10480 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010481 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010482 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010483#if defined(MBEDTLS_HAVE_TIME)
10484 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010485 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10486
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010487 start = ( (uint64_t) p[0] << 56 ) |
10488 ( (uint64_t) p[1] << 48 ) |
10489 ( (uint64_t) p[2] << 40 ) |
10490 ( (uint64_t) p[3] << 32 ) |
10491 ( (uint64_t) p[4] << 24 ) |
10492 ( (uint64_t) p[5] << 16 ) |
10493 ( (uint64_t) p[6] << 8 ) |
10494 ( (uint64_t) p[7] );
10495 p += 8;
10496
10497 session->start = (time_t) start;
10498#endif /* MBEDTLS_HAVE_TIME */
10499
10500 /*
10501 * Basic mandatory fields
10502 */
Hanno Becker88440552019-07-03 14:16:13 +010010503 {
10504 size_t const ciphersuite_len = 2;
10505#if defined(MBEDTLS_ZLIB_SUPPORT)
10506 size_t const compression_len = 1;
10507#else
10508 size_t const compression_len = 0;
10509#endif
10510 size_t const id_len_len = 1;
10511 size_t const id_len = 32;
10512 size_t const master_len = 48;
10513 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010514
Hanno Becker88440552019-07-03 14:16:13 +010010515 size_t const basic_len =
10516 ciphersuite_len +
10517 compression_len +
10518 id_len_len +
10519 id_len +
10520 master_len +
10521 verif_result_len;
10522
10523 if( basic_len > (size_t)( end - p ) )
10524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10525 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010526
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010527 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010528 p += 2;
10529
Hanno Becker73f4cb12019-06-27 13:51:07 +010010530#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010531 session->ciphersuite = ciphersuite;
10532#else
10533 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010534 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010535 {
10536 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10537 }
10538#endif
10539
Hanno Becker88440552019-07-03 14:16:13 +010010540#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010541 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010542#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010543
10544 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010545 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10546 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010547 p += 32;
10548
Teppo Järvelin91d79382019-10-02 09:09:31 +030010549 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010550 p += 48;
10551
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010552 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010553 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010554
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010555 /* Immediately clear invalid pointer values that have been read, in case
10556 * we exit early before we replaced them with valid ones. */
10557#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010558#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010559 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010560#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010561 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010562#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010563#endif
10564#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10565 session->ticket = NULL;
10566#endif
10567
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010568 /*
10569 * Peer certificate
10570 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010571#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010572#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010573 if( 3 > (size_t)( end - p ) )
10574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10575
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010576 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10577
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010578 p += 3;
10579
10580 if( cert_len == 0 )
10581 {
10582 session->peer_cert = NULL;
10583 }
10584 else
10585 {
10586 int ret;
10587
10588 if( cert_len > (size_t)( end - p ) )
10589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10590
10591 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10592
10593 if( session->peer_cert == NULL )
10594 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10595
10596 mbedtls_x509_crt_init( session->peer_cert );
10597
10598 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10599 p, cert_len ) ) != 0 )
10600 {
10601 mbedtls_x509_crt_free( session->peer_cert );
10602 mbedtls_free( session->peer_cert );
10603 session->peer_cert = NULL;
10604 return( ret );
10605 }
10606
10607 p += cert_len;
10608 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010609#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010610 /* Deserialize CRT digest from the end of the ticket. */
10611 if( 2 > (size_t)( end - p ) )
10612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10613
10614 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10615 session->peer_cert_digest_len = (size_t) *p++;
10616
10617 if( session->peer_cert_digest_len != 0 )
10618 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010619 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010620 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010621 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10623 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10625
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010626 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10628
10629 session->peer_cert_digest =
10630 mbedtls_calloc( 1, session->peer_cert_digest_len );
10631 if( session->peer_cert_digest == NULL )
10632 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10633
Teppo Järvelin91d79382019-10-02 09:09:31 +030010634 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010635 session->peer_cert_digest_len );
10636 p += session->peer_cert_digest_len;
10637 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010638#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010639#endif /* MBEDTLS_X509_CRT_PARSE_C */
10640
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010641 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010642 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010643 */
10644#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10645 if( 3 > (size_t)( end - p ) )
10646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10647
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010648 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010649 p += 3;
10650
10651 if( session->ticket_len != 0 )
10652 {
10653 if( session->ticket_len > (size_t)( end - p ) )
10654 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10655
10656 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10657 if( session->ticket == NULL )
10658 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10659
Teppo Järvelin91d79382019-10-02 09:09:31 +030010660 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010661 p += session->ticket_len;
10662 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010663
10664 if( 4 > (size_t)( end - p ) )
10665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10666
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010667 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010668 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010669#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10670
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010671 /*
10672 * Misc extension-related info
10673 */
10674#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10675 if( 1 > (size_t)( end - p ) )
10676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10677
10678 session->mfl_code = *p++;
10679#endif
10680
10681#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10682 if( 1 > (size_t)( end - p ) )
10683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10684
10685 session->trunc_hmac = *p++;
10686#endif
10687
10688#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10689 if( 1 > (size_t)( end - p ) )
10690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10691
10692 session->encrypt_then_mac = *p++;
10693#endif
10694
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010695 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010696 if( p != end )
10697 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10698
10699 return( 0 );
10700}
10701
10702/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010703 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010704 */
10705int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10706 const unsigned char *buf,
10707 size_t len )
10708{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010709 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010710
10711 if( ret != 0 )
10712 mbedtls_ssl_session_free( session );
10713
10714 return( ret );
10715}
10716
10717/*
Paul Bakker1961b702013-01-25 14:49:24 +010010718 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010721{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010723
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010724 if( ssl == NULL || ssl->conf == NULL )
10725 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010728 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010732 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010734#endif
10735
Hanno Beckerb82350b2019-07-26 07:24:05 +010010736 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010737 return( ret );
10738}
10739
10740/*
10741 * Perform the SSL handshake
10742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010743int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010744{
10745 int ret = 0;
10746
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010747 if( ssl == NULL || ssl->conf == NULL )
10748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010755
10756 if( ret != 0 )
10757 break;
10758 }
10759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010761
10762 return( ret );
10763}
10764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765#if defined(MBEDTLS_SSL_RENEGOTIATION)
10766#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010767/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010768 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010771{
10772 int ret;
10773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010775
10776 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10778 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010779
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010780 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010781 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010783 return( ret );
10784 }
10785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010787
10788 return( 0 );
10789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010791
10792/*
10793 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794 * - any side: calling mbedtls_ssl_renegotiate(),
10795 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10796 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010797 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010798 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010802{
10803 int ret;
10804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010806
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010807 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10808 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010809
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010810 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10811 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010812#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010813 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010815 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010816 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10817 MBEDTLS_SSL_IS_SERVER )
10818 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010819 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010820 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010821 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010822 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010823 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010824 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010825 }
10826#endif
10827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010828 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10829 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010831 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010834 return( ret );
10835 }
10836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010838
10839 return( 0 );
10840}
10841
10842/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010843 * Renegotiate current connection on client,
10844 * or request renegotiation on server
10845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010846int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010847{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010848 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010849
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010850 if( ssl == NULL || ssl->conf == NULL )
10851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010854 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010855 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010860 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010861
10862 /* Did we already try/start sending HelloRequest? */
10863 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010864 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010865
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010866 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010867 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010871 /*
10872 * On client, either start the renegotiation process or,
10873 * if already in progress, continue the handshake
10874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010877 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010879
10880 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010882 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010883 return( ret );
10884 }
10885 }
10886 else
10887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010891 return( ret );
10892 }
10893 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010895
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010896 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010897}
10898
10899/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010900 * Check record counters and renegotiate if they're above the limit.
10901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010903{
Andres AG2196c7f2016-12-15 17:01:16 +000010904 size_t ep_len = ssl_ep_len( ssl );
10905 int in_ctr_cmp;
10906 int out_ctr_cmp;
10907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10909 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010910 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010911 {
10912 return( 0 );
10913 }
10914
Teppo Järvelin0efac532019-10-04 13:21:08 +030010915 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010916 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010917 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010918 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010919 ssl->conf->renego_period + ep_len, 8 - ep_len );
10920
10921 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010922 {
10923 return( 0 );
10924 }
10925
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010928}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010930
10931/*
10932 * Receive application data decrypted from the SSL layer
10933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010934int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010935{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010936 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010937 size_t n;
Andrzej Kurek84bde412020-07-06 15:27:34 -040010938 volatile unsigned char *buf_dup = buf;
10939 volatile size_t len_dup = len;
Paul Bakker5121ce52009-01-03 21:22:43 +000010940
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010941 if( ssl == NULL || ssl->conf == NULL )
10942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010947 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010950 return( ret );
10951
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010952 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010954 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010955 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010956 return( ret );
10957 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010958 }
10959#endif
10960
Hanno Becker4a810fb2017-05-24 16:27:30 +010010961 /*
10962 * Check if renegotiation is necessary and/or handshake is
10963 * in process. If yes, perform/continue, and fall through
10964 * if an unexpected packet is received while the client
10965 * is waiting for the ServerHello.
10966 *
10967 * (There is no equivalent to the last condition on
10968 * the server-side as it is not treated as within
10969 * a handshake while waiting for the ClientHello
10970 * after a renegotiation request.)
10971 */
10972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010973#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010974 ret = ssl_check_ctr_renegotiate( ssl );
10975 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10976 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010978 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010979 return( ret );
10980 }
10981#endif
10982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010983 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010986 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10987 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010990 return( ret );
10991 }
10992 }
10993
Hanno Beckere41158b2017-10-23 13:30:32 +010010994 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010995 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010996 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010997 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010998 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10999 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020011000 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010011001 ssl_set_timer( ssl,
11002 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020011003 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011004
Hanno Becker327c93b2018-08-15 13:56:18 +010011005 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011006 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011007 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
11008 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000011009
Hanno Becker4a810fb2017-05-24 16:27:30 +010011010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
11011 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011012 }
11013
11014 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011016 {
11017 /*
11018 * OpenSSL sends empty messages to randomize the IV
11019 */
Hanno Becker327c93b2018-08-15 13:56:18 +010011020 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000011023 return( 0 );
11024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011026 return( ret );
11027 }
11028 }
11029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011030 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000011031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011033
Hanno Becker4a810fb2017-05-24 16:27:30 +010011034 /*
11035 * - For client-side, expect SERVER_HELLO_REQUEST.
11036 * - For server-side, expect CLIENT_HELLO.
11037 * - Fail (TLS) or silently drop record (DTLS) in other cases.
11038 */
11039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011041 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11042 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010011044 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000011045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011047
11048 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011050 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010011051 {
11052 continue;
11053 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011054 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011055#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011056#if defined(MBEDTLS_SSL_PROTO_TLS)
11057 {
11058 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
11059 }
11060#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011061 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011062#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011063
Hanno Becker4a810fb2017-05-24 16:27:30 +010011064#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011065 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11066 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011067 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011070
11071 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011073 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010011074 {
11075 continue;
11076 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011077 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011078#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011079#if defined(MBEDTLS_SSL_PROTO_TLS)
11080 {
11081 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
11082 }
11083#endif
Paul Bakker48916f92012-09-16 19:57:18 +000011084 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011085#endif /* MBEDTLS_SSL_SRV_C */
11086
Hanno Becker21df7f92017-10-17 11:03:26 +010011087#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010011088 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011089 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
11090 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010011091 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011092 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
11093 {
11094 /*
11095 * Accept renegotiation request
11096 */
Paul Bakker48916f92012-09-16 19:57:18 +000011097
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011098 /* DTLS clients need to know renego is server-initiated */
11099#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011100 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011101 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11102 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011103 {
11104 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11105 }
11106#endif
11107 ret = ssl_start_renegotiation( ssl );
11108 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11109 ret != 0 )
11110 {
11111 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11112 return( ret );
11113 }
11114 }
11115 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011116#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011117 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011118 /*
11119 * Refuse renegotiation
11120 */
11121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011125 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011126 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011127 /* SSLv3 does not have a "no_renegotiation" warning, so
11128 we send a fatal alert and abort the connection. */
11129 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11130 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11131 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011132 }
11133 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011134#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11135#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11136 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011137 if( mbedtls_ssl_ver_geq(
11138 mbedtls_ssl_get_minor_ver( ssl ),
11139 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011140 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011141 ret = mbedtls_ssl_send_alert_message( ssl,
11142 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11143 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11144 if( ret != 0 )
11145 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011146 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11149 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11152 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011153 }
Paul Bakker48916f92012-09-16 19:57:18 +000011154 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011155
Hanno Becker90333da2017-10-10 11:27:13 +010011156 /* At this point, we don't know whether the renegotiation has been
11157 * completed or not. The cases to consider are the following:
11158 * 1) The renegotiation is complete. In this case, no new record
11159 * has been read yet.
11160 * 2) The renegotiation is incomplete because the client received
11161 * an application data record while awaiting the ServerHello.
11162 * 3) The renegotiation is incomplete because the client received
11163 * a non-handshake, non-application data message while awaiting
11164 * the ServerHello.
11165 * In each of these case, looping will be the proper action:
11166 * - For 1), the next iteration will read a new record and check
11167 * if it's application data.
11168 * - For 2), the loop condition isn't satisfied as application data
11169 * is present, hence continue is the same as break
11170 * - For 3), the loop condition is satisfied and read_record
11171 * will re-deliver the message that was held back by the client
11172 * when expecting the ServerHello.
11173 */
11174 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011175 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011176#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011177 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011178 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011179 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011180 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011181 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011184 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011185 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011186 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011187 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011189#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11192 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011195 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011196 }
11197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011198 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11201 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011202 }
11203
11204 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011205
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011206 /* We're going to return something now, cancel timer,
11207 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011208 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011209 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011210
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011212 /* If we requested renego but received AppData, resend HelloRequest.
11213 * Do it now, after setting in_offt, to avoid taking this branch
11214 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011215#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011216 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11217 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011218 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011219 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011220 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011223 return( ret );
11224 }
11225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011226#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011228 }
11229
11230 n = ( len < ssl->in_msglen )
11231 ? len : ssl->in_msglen;
11232
Teppo Järvelin91d79382019-10-02 09:09:31 +030011233 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011234 ssl->in_msglen -= n;
11235
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011236 // clear incoming data after it's copied to buffer
11237 mbedtls_platform_memset(ssl->in_offt, 0, n);
11238
Paul Bakker5121ce52009-01-03 21:22:43 +000011239 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011240 {
11241 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011242 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011243 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011244 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011245 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011246 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011247 /* more data available */
11248 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011249 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011252
Andrzej Kurek84bde412020-07-06 15:27:34 -040011253 /* Secure against buffer substitution */
11254 if( buf_dup == buf && len_dup == len )
11255 {
11256 return( (int) n );
11257 }
11258 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker5121ce52009-01-03 21:22:43 +000011259}
11260
11261/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011262 * Send application data to be encrypted by the SSL layer, taking care of max
11263 * fragment length and buffer size.
11264 *
11265 * According to RFC 5246 Section 6.2.1:
11266 *
11267 * Zero-length fragments of Application data MAY be sent as they are
11268 * potentially useful as a traffic analysis countermeasure.
11269 *
11270 * Therefore, it is possible that the input message length is 0 and the
11271 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011272 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011273static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011274 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011275{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011276 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11277 const size_t max_len = (size_t) ret;
Andrzej Kurek84bde412020-07-06 15:27:34 -040011278 volatile const unsigned char *buf_dup = buf;
11279 volatile size_t len_dup = len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011280
11281 if( ret < 0 )
11282 {
11283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11284 return( ret );
11285 }
11286
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011287 if( len > max_len )
11288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011290 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011293 "maximum fragment length: %d > %d",
11294 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011295 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011296 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011297 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011298#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011299#if defined(MBEDTLS_SSL_PROTO_TLS)
11300 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011301 len = max_len;
Andrzej Kurek84bde412020-07-06 15:27:34 -040011302 len_dup = len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011303 }
11304#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011305 }
Paul Bakker887bd502011-06-08 13:10:54 +000011306
Paul Bakker5121ce52009-01-03 21:22:43 +000011307 if( ssl->out_left != 0 )
11308 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011309 /*
11310 * The user has previously tried to send the data and
11311 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11312 * written. In this case, we expect the high-level write function
11313 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011315 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011318 return( ret );
11319 }
11320 }
Paul Bakker887bd502011-06-08 13:10:54 +000011321 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011322 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011323 /*
11324 * The user is trying to send a message the first time, so we need to
11325 * copy the data into the internal buffers and setup the data structure
11326 * to keep track of partial writes
11327 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011328 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011329 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011330 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011331
Hanno Becker67bc7c32018-08-06 11:33:50 +010011332 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011335 return( ret );
11336 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011337 }
Andrzej Kurek84bde412020-07-06 15:27:34 -040011338 /* Secure against buffer substitution */
11339 if( buf_dup == buf && len_dup == len )
11340 {
11341 return( (int) len );
11342 }
11343 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker5121ce52009-01-03 21:22:43 +000011344}
11345
11346/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011347 * Write application data, doing 1/n-1 splitting if necessary.
11348 *
11349 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011350 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011351 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011353#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011354static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011355 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011356{
11357 int ret;
11358
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011359 if( ssl->conf->cbc_record_splitting ==
11360 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011361 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011362 mbedtls_ssl_ver_gt(
11363 mbedtls_ssl_get_minor_ver( ssl ),
11364 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011365 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11366 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011367 {
11368 return( ssl_write_real( ssl, buf, len ) );
11369 }
11370
11371 if( ssl->split_done == 0 )
11372 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011373 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011374 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011375 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011376 }
11377
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011378 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11379 return( ret );
11380 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011381
11382 return( ret + 1 );
11383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011384#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011385
11386/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011387 * Write application data (public-facing wrapper)
11388 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011389int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011390{
11391 int ret;
Andrzej Kurek84bde412020-07-06 15:27:34 -040011392 volatile const unsigned char *buf_dup = buf;
11393 volatile size_t len_dup = len;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011394
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011396
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011397 if( ssl == NULL || ssl->conf == NULL )
11398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11399
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011400#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011401 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11402 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011403 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011404 return( ret );
11405 }
11406#endif
11407
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011408 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011409 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011410 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011411 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011413 return( ret );
11414 }
11415 }
11416
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011417#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011418 ret = ssl_write_split( ssl, buf, len );
11419#else
11420 ret = ssl_write_real( ssl, buf, len );
11421#endif
11422
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011424
Andrzej Kurek84bde412020-07-06 15:27:34 -040011425 /* Secure against buffer substitution */
11426 if( buf_dup == buf && len_dup == len )
11427 {
11428 return( ret );
11429 }
11430 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011431}
11432
11433/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011434 * Notify the peer that the connection is being closed
11435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011436int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011437{
11438 int ret;
11439
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011440 if( ssl == NULL || ssl->conf == NULL )
11441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011444
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011445 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011446 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011448 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011450 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11451 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11452 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011455 return( ret );
11456 }
11457 }
11458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011460
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011461 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011462}
11463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011464void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011465{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011466 if( transform == NULL )
11467 return;
11468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011469#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011470 deflateEnd( &transform->ctx_deflate );
11471 inflateEnd( &transform->ctx_inflate );
11472#endif
11473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011474 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11475 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011476
Hanno Becker92231322018-01-03 15:32:51 +000011477#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011478 mbedtls_md_free( &transform->md_ctx_enc );
11479 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011480#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011481
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011482 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011483}
11484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011485#if defined(MBEDTLS_X509_CRT_PARSE_C)
11486static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011487{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011488 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011489
11490 while( cur != NULL )
11491 {
11492 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011493 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011494 cur = next;
11495 }
11496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011497#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011498
Hanno Becker0271f962018-08-16 13:23:47 +010011499#if defined(MBEDTLS_SSL_PROTO_DTLS)
11500
11501static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11502{
11503 unsigned offset;
11504 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11505
11506 if( hs == NULL )
11507 return;
11508
Hanno Becker283f5ef2018-08-24 09:34:47 +010011509 ssl_free_buffered_record( ssl );
11510
Hanno Becker0271f962018-08-16 13:23:47 +010011511 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011512 ssl_buffering_free_slot( ssl, offset );
11513}
11514
11515static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11516 uint8_t slot )
11517{
11518 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11519 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011520
11521 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11522 return;
11523
Hanno Beckere605b192018-08-21 15:59:07 +010011524 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011525 {
Hanno Beckere605b192018-08-21 15:59:07 +010011526 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011527 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011528 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011529 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011530 }
11531}
11532
11533#endif /* MBEDTLS_SSL_PROTO_DTLS */
11534
Gilles Peskine9b562d52018-04-25 20:32:43 +020011535void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011536{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011537 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11538
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011539 if( handshake == NULL )
11540 return;
11541
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011542#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11543 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11544 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011545 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011546 handshake->async_in_progress = 0;
11547 }
11548#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11549
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011550#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11551 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11552 mbedtls_md5_free( &handshake->fin_md5 );
11553 mbedtls_sha1_free( &handshake->fin_sha1 );
11554#endif
11555#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11556#if defined(MBEDTLS_SHA256_C)
11557 mbedtls_sha256_free( &handshake->fin_sha256 );
11558#endif
11559#if defined(MBEDTLS_SHA512_C)
11560 mbedtls_sha512_free( &handshake->fin_sha512 );
11561#endif
11562#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011564#if defined(MBEDTLS_DHM_C)
11565 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011566#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011567#if defined(MBEDTLS_ECDH_C)
11568 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011569#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011570#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011571 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011572#if defined(MBEDTLS_SSL_CLI_C)
11573 mbedtls_free( handshake->ecjpake_cache );
11574 handshake->ecjpake_cache = NULL;
11575 handshake->ecjpake_cache_len = 0;
11576#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011577#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011578
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011579#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11580 if( handshake->psk != NULL )
11581 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011582 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011583 mbedtls_free( handshake->psk );
11584 }
11585#endif
11586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011587#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11588 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011589 /*
11590 * Free only the linked list wrapper, not the keys themselves
11591 * since the belong to the SNI callback
11592 */
11593 if( handshake->sni_key_cert != NULL )
11594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011595 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011596
11597 while( cur != NULL )
11598 {
11599 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011600 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011601 cur = next;
11602 }
11603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011604#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011605
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011606#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011607 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011608 if( handshake->ecrs_peer_cert != NULL )
11609 {
11610 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11611 mbedtls_free( handshake->ecrs_peer_cert );
11612 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011613#endif
11614
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011615#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11616 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11617 mbedtls_pk_free( &handshake->peer_pubkey );
11618#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011620#if defined(MBEDTLS_SSL_PROTO_DTLS)
11621 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011622 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011623 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011624#endif
11625
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011626 mbedtls_platform_zeroize( handshake,
11627 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011628}
11629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011630void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011631{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011632 if( session == NULL )
11633 return;
11634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011635#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011636 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011637#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011638
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011639#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011640 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011641#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011642
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011643 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011644}
11645
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011646#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011647
11648#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11649#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11650#else
11651#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11652#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11653
11654#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11655#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11656#else
11657#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11658#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11659
11660#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11661#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11662#else
11663#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11664#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11665
11666#if defined(MBEDTLS_SSL_ALPN)
11667#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11668#else
11669#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11670#endif /* MBEDTLS_SSL_ALPN */
11671
11672#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11673#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11674#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11675#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11676
11677#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11678 ( (uint32_t) ( \
11679 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11680 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11681 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11682 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11683 0u ) )
11684
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011685static unsigned char ssl_serialized_context_header[] = {
11686 MBEDTLS_VERSION_MAJOR,
11687 MBEDTLS_VERSION_MINOR,
11688 MBEDTLS_VERSION_PATCH,
11689 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11690 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011691 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11692 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11693 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011694};
11695
Paul Bakker5121ce52009-01-03 21:22:43 +000011696/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011697 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011698 *
11699 * The format of the serialized data is:
11700 * (in the presentation language of TLS, RFC 8446 section 3)
11701 *
11702 * // header
11703 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011704 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011705 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011706 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011707 * Note: When updating the format, remember to keep these
11708 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011709 *
11710 * // session sub-structure
11711 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11712 * // transform sub-structure
11713 * uint8 random[64]; // ServerHello.random+ClientHello.random
11714 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11715 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11716 * // fields from ssl_context
11717 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11718 * uint64 in_window_top; // DTLS: last validated record seq_num
11719 * uint64 in_window; // DTLS: bitmask for replay protection
11720 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11721 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11722 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11723 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11724 *
11725 * Note that many fields of the ssl_context or sub-structures are not
11726 * serialized, as they fall in one of the following categories:
11727 *
11728 * 1. forced value (eg in_left must be 0)
11729 * 2. pointer to dynamically-allocated memory (eg session, transform)
11730 * 3. value can be re-derived from other data (eg session keys from MS)
11731 * 4. value was temporary (eg content of input buffer)
11732 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011733 */
11734int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11735 unsigned char *buf,
11736 size_t buf_len,
11737 size_t *olen )
11738{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011739 unsigned char *p = buf;
11740 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011741 size_t session_len;
11742 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011743
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011744 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011745 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11746 * this function's documentation.
11747 *
11748 * These are due to assumptions/limitations in the implementation. Some of
11749 * them are likely to stay (no handshake in progress) some might go away
11750 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011751 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011752 /* The initial handshake must be over */
11753 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011754 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011755 if( ssl->handshake != NULL )
11756 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11757 /* Double-check that sub-structures are indeed ready */
11758 if( ssl->transform == NULL || ssl->session == NULL )
11759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11760 /* There must be no pending incoming or outgoing data */
11761 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11763 if( ssl->out_left != 0 )
11764 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11765 /* Protocol must be DLTS, not TLS */
11766 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11768 /* Version must be 1.2 */
11769 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11771 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11773 /* We must be using an AEAD ciphersuite */
11774 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11776 /* Renegotiation must not be enabled */
11777 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011779
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011780 /*
11781 * Version and format identifier
11782 */
11783 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011784
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011785 if( used <= buf_len )
11786 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011787 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011788 sizeof( ssl_serialized_context_header ) );
11789 p += sizeof( ssl_serialized_context_header );
11790 }
11791
11792 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011793 * Session (length + data)
11794 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011795 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011796 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11797 return( ret );
11798
11799 used += 4 + session_len;
11800 if( used <= buf_len )
11801 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011802 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011803
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011804 ret = ssl_session_save( ssl->session, 1,
11805 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011806 if( ret != 0 )
11807 return( ret );
11808
11809 p += session_len;
11810 }
11811
11812 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011813 * Transform
11814 */
11815 used += sizeof( ssl->transform->randbytes );
11816 if( used <= buf_len )
11817 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011818 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011819 sizeof( ssl->transform->randbytes ) );
11820 p += sizeof( ssl->transform->randbytes );
11821 }
11822
11823#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11824 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11825 if( used <= buf_len )
11826 {
11827 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011828 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11829 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011830 p += ssl->transform->in_cid_len;
11831
11832 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011833 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11834 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011835 p += ssl->transform->out_cid_len;
11836 }
11837#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11838
11839 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011840 * Saved fields from top-level ssl_context structure
11841 */
11842#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11843 used += 4;
11844 if( used <= buf_len )
11845 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011846 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011847 }
11848#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11849
11850#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11851 used += 16;
11852 if( used <= buf_len )
11853 {
11854 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11855 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11856 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11857 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11858 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11859 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11860 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11861 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11862
11863 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11864 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11865 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11866 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11867 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11868 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11869 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11870 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11871 }
11872#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11873
11874#if defined(MBEDTLS_SSL_PROTO_DTLS)
11875 used += 1;
11876 if( used <= buf_len )
11877 {
11878 *p++ = ssl->disable_datagram_packing;
11879 }
11880#endif /* MBEDTLS_SSL_PROTO_DTLS */
11881
11882 used += 8;
11883 if( used <= buf_len )
11884 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011885 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011886 p += 8;
11887 }
11888
11889#if defined(MBEDTLS_SSL_PROTO_DTLS)
11890 used += 2;
11891 if( used <= buf_len )
11892 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011893 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011894 }
11895#endif /* MBEDTLS_SSL_PROTO_DTLS */
11896
11897#if defined(MBEDTLS_SSL_ALPN)
11898 {
11899 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011900 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011901 : 0;
11902
11903 used += 1 + alpn_len;
11904 if( used <= buf_len )
11905 {
11906 *p++ = alpn_len;
11907
11908 if( ssl->alpn_chosen != NULL )
11909 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011910 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011911 p += alpn_len;
11912 }
11913 }
11914 }
11915#endif /* MBEDTLS_SSL_ALPN */
11916
11917 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011918 * Done
11919 */
11920 *olen = used;
11921
11922 if( used > buf_len )
11923 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011924
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011925 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11926
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011927 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011928}
11929
11930/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011931 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011932 *
11933 * This internal version is wrapped by a public function that cleans up in
11934 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011935 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011936static int ssl_context_load( mbedtls_ssl_context *ssl,
11937 const unsigned char *buf,
11938 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011939{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011940 const unsigned char *p = buf;
11941 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011942 size_t session_len;
11943 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011944
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011945 /*
11946 * The context should have been freshly setup or reset.
11947 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011948 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011949 * renegotiating, or if the user mistakenly loaded a session first.)
11950 */
11951 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11952 ssl->session != NULL )
11953 {
11954 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11955 }
11956
11957 /*
11958 * We can't check that the config matches the initial one, but we can at
11959 * least check it matches the requirements for serializing.
11960 */
11961 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011962 mbedtls_ssl_ver_lt(
11963 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11964 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11965 mbedtls_ssl_ver_gt(
11966 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11967 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11968 mbedtls_ssl_ver_lt(
11969 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11970 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11971 mbedtls_ssl_ver_gt(
11972 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11973 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011974 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011975 {
11976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11977 }
11978
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011979 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11980
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011981 /*
11982 * Check version identifier
11983 */
11984 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11986
Teppo Järvelin650343c2019-10-03 15:36:59 +030011987 // use regular memcmp as header is not that critical
11988 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011989 sizeof( ssl_serialized_context_header ) ) != 0 )
11990 {
11991 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11992 }
11993 p += sizeof( ssl_serialized_context_header );
11994
11995 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011996 * Session
11997 */
11998 if( (size_t)( end - p ) < 4 )
11999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12000
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030012001 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012002 p += 4;
12003
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020012004 /* This has been allocated by ssl_handshake_init(), called by
12005 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
12006 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012007 ssl->session_in = ssl->session;
12008 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020012009 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012010
12011 if( (size_t)( end - p ) < session_len )
12012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12013
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020012014 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012015 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020012016 {
12017 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012018 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020012019 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012020
12021 p += session_len;
12022
12023 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020012024 * Transform
12025 */
12026
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020012027 /* This has been allocated by ssl_handshake_init(), called by
12028 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
12029 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020012030 ssl->transform_in = ssl->transform;
12031 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020012032 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020012033
12034 /* Read random bytes and populate structure */
12035 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
12036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12037
12038 ret = ssl_populate_transform( ssl->transform,
12039 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
12040 ssl->session->master,
12041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
12042#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12043 ssl->session->encrypt_then_mac,
12044#endif
12045#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
12046 ssl->session->trunc_hmac,
12047#endif
12048#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
12049#if defined(MBEDTLS_ZLIB_SUPPORT)
12050 ssl->session->compression,
12051#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020012052 p, /* currently pointing to randbytes */
12053 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
12054 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
12055 ssl );
12056 if( ret != 0 )
12057 return( ret );
12058
12059 p += sizeof( ssl->transform->randbytes );
12060
12061#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
12062 /* Read connection IDs and store them */
12063 if( (size_t)( end - p ) < 1 )
12064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12065
12066 ssl->transform->in_cid_len = *p++;
12067
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020012068 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020012069 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12070
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030012071 /* Not using more secure mbedtls_platform_memcpy as cid is public */
12072 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020012073 p += ssl->transform->in_cid_len;
12074
12075 ssl->transform->out_cid_len = *p++;
12076
12077 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
12078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12079
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030012080 /* Not using more secure mbedtls_platform_memcpy as cid is public */
12081 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020012082 p += ssl->transform->out_cid_len;
12083#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
12084
12085 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012086 * Saved fields from top-level ssl_context structure
12087 */
12088#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
12089 if( (size_t)( end - p ) < 4 )
12090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12091
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012092 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012093 p += 4;
12094#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
12095
12096#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
12097 if( (size_t)( end - p ) < 16 )
12098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12099
12100 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
12101 ( (uint64_t) p[1] << 48 ) |
12102 ( (uint64_t) p[2] << 40 ) |
12103 ( (uint64_t) p[3] << 32 ) |
12104 ( (uint64_t) p[4] << 24 ) |
12105 ( (uint64_t) p[5] << 16 ) |
12106 ( (uint64_t) p[6] << 8 ) |
12107 ( (uint64_t) p[7] );
12108 p += 8;
12109
12110 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
12111 ( (uint64_t) p[1] << 48 ) |
12112 ( (uint64_t) p[2] << 40 ) |
12113 ( (uint64_t) p[3] << 32 ) |
12114 ( (uint64_t) p[4] << 24 ) |
12115 ( (uint64_t) p[5] << 16 ) |
12116 ( (uint64_t) p[6] << 8 ) |
12117 ( (uint64_t) p[7] );
12118 p += 8;
12119#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12120
12121#if defined(MBEDTLS_SSL_PROTO_DTLS)
12122 if( (size_t)( end - p ) < 1 )
12123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12124
12125 ssl->disable_datagram_packing = *p++;
12126#endif /* MBEDTLS_SSL_PROTO_DTLS */
12127
12128 if( (size_t)( end - p ) < 8 )
12129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12130
Teppo Järvelin91d79382019-10-02 09:09:31 +030012131 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012132 p += 8;
12133
12134#if defined(MBEDTLS_SSL_PROTO_DTLS)
12135 if( (size_t)( end - p ) < 2 )
12136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012137 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012138 p += 2;
12139#endif /* MBEDTLS_SSL_PROTO_DTLS */
12140
12141#if defined(MBEDTLS_SSL_ALPN)
12142 {
12143 uint8_t alpn_len;
12144 const char **cur;
12145
12146 if( (size_t)( end - p ) < 1 )
12147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12148
12149 alpn_len = *p++;
12150
12151 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12152 {
12153 /* alpn_chosen should point to an item in the configured list */
12154 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12155 {
12156 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012157 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012158 {
12159 ssl->alpn_chosen = *cur;
12160 break;
12161 }
12162 }
12163 }
12164
12165 /* can only happen on conf mismatch */
12166 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12168
12169 p += alpn_len;
12170 }
12171#endif /* MBEDTLS_SSL_ALPN */
12172
12173 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012174 * Forced fields from top-level ssl_context structure
12175 *
12176 * Most of them already set to the correct value by mbedtls_ssl_init() and
12177 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12178 */
12179 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12180
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012181#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012182 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012183#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12184#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012185 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012186#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012187
Hanno Becker83985822019-08-30 10:42:49 +010012188 /* Adjust pointers for header fields of outgoing records to
12189 * the given transform, accounting for explicit IV and CID. */
12190 ssl_update_out_pointers( ssl, ssl->transform );
12191
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012192#if defined(MBEDTLS_SSL_PROTO_DTLS)
12193 ssl->in_epoch = 1;
12194#endif
12195
12196 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12197 * which we don't want - otherwise we'd end up freeing the wrong transform
12198 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12199 if( ssl->handshake != NULL )
12200 {
12201 mbedtls_ssl_handshake_free( ssl );
12202 mbedtls_free( ssl->handshake );
12203 ssl->handshake = NULL;
12204 }
12205
12206 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012207 * Done - should have consumed entire buffer
12208 */
12209 if( p != end )
12210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012211
12212 return( 0 );
12213}
12214
12215/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012216 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012217 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012218int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012219 const unsigned char *buf,
12220 size_t len )
12221{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012222 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012223
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012224 if( ret != 0 )
12225 mbedtls_ssl_free( context );
12226
12227 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012228}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012229#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012230
12231/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012232 * Free an SSL context
12233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012234void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012235{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012236 if( ssl == NULL )
12237 return;
12238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012240
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012241 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012242 {
Angus Grattond8213d02016-05-25 20:56:48 +100012243 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012244 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012245 }
12246
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012247 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012248 {
Angus Grattond8213d02016-05-25 20:56:48 +100012249 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012250 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012251 }
12252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012253#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012254 if( ssl->compress_buf != NULL )
12255 {
Angus Grattond8213d02016-05-25 20:56:48 +100012256 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012257 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012258 }
12259#endif
12260
Paul Bakker48916f92012-09-16 19:57:18 +000012261 if( ssl->transform )
12262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012263 mbedtls_ssl_transform_free( ssl->transform );
12264 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012265 }
12266
12267 if( ssl->handshake )
12268 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012269 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012270 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12271 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012273 mbedtls_free( ssl->handshake );
12274 mbedtls_free( ssl->transform_negotiate );
12275 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012276 }
12277
Paul Bakkerc0463502013-02-14 11:19:38 +010012278 if( ssl->session )
12279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012280 mbedtls_ssl_session_free( ssl->session );
12281 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012282 }
12283
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012284#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012285 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012286 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012287 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012288 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012289 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012292#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12293 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12296 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012297 }
12298#endif
12299
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012300#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012301 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012302#endif
12303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012305
Paul Bakker86f04f42013-02-14 11:20:09 +010012306 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012307 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012308}
12309
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012310/*
12311 * Initialze mbedtls_ssl_config
12312 */
12313void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12314{
12315 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012316
12317#if !defined(MBEDTLS_SSL_PROTO_TLS)
12318 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12319#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012320}
12321
Simon Butcherc97b6972015-12-27 23:48:17 +000012322#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012323#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012324static int ssl_preset_default_hashes[] = {
12325#if defined(MBEDTLS_SHA512_C)
12326 MBEDTLS_MD_SHA512,
12327 MBEDTLS_MD_SHA384,
12328#endif
12329#if defined(MBEDTLS_SHA256_C)
12330 MBEDTLS_MD_SHA256,
12331 MBEDTLS_MD_SHA224,
12332#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012333#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012334 MBEDTLS_MD_SHA1,
12335#endif
12336 MBEDTLS_MD_NONE
12337};
Simon Butcherc97b6972015-12-27 23:48:17 +000012338#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012339#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012340
Hanno Becker73f4cb12019-06-27 13:51:07 +010012341#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012342static int ssl_preset_suiteb_ciphersuites[] = {
12343 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12344 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12345 0
12346};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012347#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012348
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012349#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012350#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012351static int ssl_preset_suiteb_hashes[] = {
12352 MBEDTLS_MD_SHA256,
12353 MBEDTLS_MD_SHA384,
12354 MBEDTLS_MD_NONE
12355};
12356#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012357#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012358
Hanno Beckerc1096e72019-06-19 12:30:41 +010012359#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012360static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012361#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012362 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012363#endif
12364#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012365 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012366#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012367 MBEDTLS_ECP_DP_NONE
12368};
12369#endif
12370
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012371/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012372 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012373 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012374int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012375 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012376{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012377#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012378 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012379#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012380
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012381 /* Use the functions here so that they are covered in tests,
12382 * but otherwise access member directly for efficiency */
12383 mbedtls_ssl_conf_endpoint( conf, endpoint );
12384 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012385
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012386 /*
12387 * Things that are common to all presets
12388 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012389#if defined(MBEDTLS_SSL_CLI_C)
12390 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12391 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012392#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012393 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012394#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012395#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12396 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12397#endif
12398 }
12399#endif
12400
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012401#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012402 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012403#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012404
12405#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12406 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12407#endif
12408
12409#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012410#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012411 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012412#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12413#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012414 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012415 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012416#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012417#endif
12418
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012419#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12420 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12421#endif
12422
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012423#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012424 conf->f_cookie_write = ssl_cookie_write_dummy;
12425 conf->f_cookie_check = ssl_cookie_check_dummy;
12426#endif
12427
Hanno Becker7f376f42019-06-12 16:20:48 +010012428#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12429 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012430 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12431#endif
12432
Janos Follath088ce432017-04-10 12:42:31 +010012433#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012434#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012435 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012436#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12437#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012438
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012439#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012440#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012441 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012442#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12443#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012444 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012445#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12446#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012447
12448#if defined(MBEDTLS_SSL_RENEGOTIATION)
12449 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012450 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12451 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012452#endif
12453
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012454#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12455 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12456 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012457 const unsigned char dhm_p[] =
12458 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12459 const unsigned char dhm_g[] =
12460 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12461
Hanno Beckera90658f2017-10-04 15:29:08 +010012462 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12463 dhm_p, sizeof( dhm_p ),
12464 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012465 {
12466 return( ret );
12467 }
12468 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012469#endif
12470
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012471 /*
12472 * Preset-specific defaults
12473 */
12474 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012475 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012476 /*
12477 * NSA Suite B
12478 */
12479 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012480#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012481 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012482#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12483#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012484 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012485#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12486#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012487 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012488#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12489#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012490 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012491#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012492
Hanno Becker73f4cb12019-06-27 13:51:07 +010012493#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012494 conf->ciphersuite_list[0] =
12495 conf->ciphersuite_list[1] =
12496 conf->ciphersuite_list[2] =
12497 conf->ciphersuite_list[3] =
12498 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012499#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012500
12501#if defined(MBEDTLS_X509_CRT_PARSE_C)
12502 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012503#endif
12504
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012505#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012506#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012507 conf->sig_hashes = ssl_preset_suiteb_hashes;
12508#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012509#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012510
12511#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012512#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012513 conf->curve_list = ssl_preset_suiteb_curves;
12514#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012515#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012516 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012517
12518 /*
12519 * Default
12520 */
12521 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012522#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012523 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12524 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12525 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12526 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012527#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12528#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012529 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12530 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12531 MBEDTLS_SSL_MIN_MINOR_VERSION :
12532 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012534 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012535 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12536#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012537#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12538#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12539 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12540#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12541#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12542 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12543#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012544
Hanno Becker73f4cb12019-06-27 13:51:07 +010012545#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012546 conf->ciphersuite_list[0] =
12547 conf->ciphersuite_list[1] =
12548 conf->ciphersuite_list[2] =
12549 conf->ciphersuite_list[3] =
12550 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012551#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012552
12553#if defined(MBEDTLS_X509_CRT_PARSE_C)
12554 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12555#endif
12556
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012557#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012558#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012559 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012560#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012561#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012562
12563#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012564#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012565 conf->curve_list = mbedtls_ecp_grp_id_list();
12566#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012567#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012568
12569#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12570 conf->dhm_min_bitlen = 1024;
12571#endif
12572 }
12573
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012574 return( 0 );
12575}
12576
12577/*
12578 * Free mbedtls_ssl_config
12579 */
12580void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12581{
12582#if defined(MBEDTLS_DHM_C)
12583 mbedtls_mpi_free( &conf->dhm_P );
12584 mbedtls_mpi_free( &conf->dhm_G );
12585#endif
12586
12587#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12588 if( conf->psk != NULL )
12589 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012590 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012591 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012592 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012593 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012594 }
12595
12596 if( conf->psk_identity != NULL )
12597 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012598 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012599 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012600 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012601 conf->psk_identity_len = 0;
12602 }
12603#endif
12604
12605#if defined(MBEDTLS_X509_CRT_PARSE_C)
12606 ssl_key_cert_free( conf->key_cert );
12607#endif
12608
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012609 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012610}
12611
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012612#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012613 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12614 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012615/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012616 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012618unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012620#if defined(MBEDTLS_RSA_C)
12621 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12622 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012623#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012624#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012625 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12626 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012628 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012629}
12630
Hanno Becker7e5437a2017-04-28 17:15:26 +010012631unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12632{
12633 switch( type ) {
12634 case MBEDTLS_PK_RSA:
12635 return( MBEDTLS_SSL_SIG_RSA );
12636 case MBEDTLS_PK_ECDSA:
12637 case MBEDTLS_PK_ECKEY:
12638 return( MBEDTLS_SSL_SIG_ECDSA );
12639 default:
12640 return( MBEDTLS_SSL_SIG_ANON );
12641 }
12642}
12643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012644mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012645{
12646 switch( sig )
12647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012648#if defined(MBEDTLS_RSA_C)
12649 case MBEDTLS_SSL_SIG_RSA:
12650 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012651#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012652#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012653 case MBEDTLS_SSL_SIG_ECDSA:
12654 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012655#endif
12656 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012657 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012658 }
12659}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012660#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012661
Hanno Becker7e5437a2017-04-28 17:15:26 +010012662#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12663 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12664
12665/* Find an entry in a signature-hash set matching a given hash algorithm. */
12666mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12667 mbedtls_pk_type_t sig_alg )
12668{
12669 switch( sig_alg )
12670 {
12671 case MBEDTLS_PK_RSA:
12672 return( set->rsa );
12673 case MBEDTLS_PK_ECDSA:
12674 return( set->ecdsa );
12675 default:
12676 return( MBEDTLS_MD_NONE );
12677 }
12678}
12679
12680/* Add a signature-hash-pair to a signature-hash set */
12681void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12682 mbedtls_pk_type_t sig_alg,
12683 mbedtls_md_type_t md_alg )
12684{
12685 switch( sig_alg )
12686 {
12687 case MBEDTLS_PK_RSA:
12688 if( set->rsa == MBEDTLS_MD_NONE )
12689 set->rsa = md_alg;
12690 break;
12691
12692 case MBEDTLS_PK_ECDSA:
12693 if( set->ecdsa == MBEDTLS_MD_NONE )
12694 set->ecdsa = md_alg;
12695 break;
12696
12697 default:
12698 break;
12699 }
12700}
12701
12702/* Allow exactly one hash algorithm for each signature. */
12703void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12704 mbedtls_md_type_t md_alg )
12705{
12706 set->rsa = md_alg;
12707 set->ecdsa = md_alg;
12708}
12709
12710#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12711 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12712
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012713/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012714 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012716mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012717{
12718 switch( hash )
12719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012720#if defined(MBEDTLS_MD5_C)
12721 case MBEDTLS_SSL_HASH_MD5:
12722 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012724#if defined(MBEDTLS_SHA1_C)
12725 case MBEDTLS_SSL_HASH_SHA1:
12726 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012728#if defined(MBEDTLS_SHA256_C)
12729 case MBEDTLS_SSL_HASH_SHA224:
12730 return( MBEDTLS_MD_SHA224 );
12731 case MBEDTLS_SSL_HASH_SHA256:
12732 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012734#if defined(MBEDTLS_SHA512_C)
12735 case MBEDTLS_SSL_HASH_SHA384:
12736 return( MBEDTLS_MD_SHA384 );
12737 case MBEDTLS_SSL_HASH_SHA512:
12738 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012739#endif
12740 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012741 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012742 }
12743}
12744
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012745/*
12746 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12747 */
12748unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12749{
12750 switch( md )
12751 {
12752#if defined(MBEDTLS_MD5_C)
12753 case MBEDTLS_MD_MD5:
12754 return( MBEDTLS_SSL_HASH_MD5 );
12755#endif
12756#if defined(MBEDTLS_SHA1_C)
12757 case MBEDTLS_MD_SHA1:
12758 return( MBEDTLS_SSL_HASH_SHA1 );
12759#endif
12760#if defined(MBEDTLS_SHA256_C)
12761 case MBEDTLS_MD_SHA224:
12762 return( MBEDTLS_SSL_HASH_SHA224 );
12763 case MBEDTLS_MD_SHA256:
12764 return( MBEDTLS_SSL_HASH_SHA256 );
12765#endif
12766#if defined(MBEDTLS_SHA512_C)
12767 case MBEDTLS_MD_SHA384:
12768 return( MBEDTLS_SSL_HASH_SHA384 );
12769 case MBEDTLS_MD_SHA512:
12770 return( MBEDTLS_SSL_HASH_SHA512 );
12771#endif
12772 default:
12773 return( MBEDTLS_SSL_HASH_NONE );
12774 }
12775}
12776
Hanno Beckeree902df2019-08-23 13:47:47 +010012777#if defined(MBEDTLS_USE_TINYCRYPT)
12778/*
12779 * Check if a curve proposed by the peer is in our list.
12780 * Return 0 if we're willing to use it, -1 otherwise.
12781 */
12782int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12783 mbedtls_uecc_group_id grp_id )
12784{
12785 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12786 if( own_ec_id == grp_id )
12787 return( 0 );
12788 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12789
12790 return( -1 );
12791}
12792#endif /* MBEDTLS_USE_TINYCRYPT */
12793
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012794#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012795/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012796 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012797 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012798 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012799int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12800 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012801{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012802 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12803 if( own_ec_id == grp_id )
12804 return( 0 );
12805 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012806
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012807 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012808}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012809#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012810
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012811#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012812/*
12813 * Check if a hash proposed by the peer is in our list.
12814 * Return 0 if we're willing to use it, -1 otherwise.
12815 */
12816int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12817 mbedtls_md_type_t md )
12818{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012819 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12820 if( md_alg == md )
12821 return( 0 );
12822 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012823
12824 return( -1 );
12825}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012826#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012828#if defined(MBEDTLS_X509_CRT_PARSE_C)
12829int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012830 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012831 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012832 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012833{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012834 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012835#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012836 int usage = 0;
12837#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012838#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012839 const char *ext_oid;
12840 size_t ext_len;
12841#endif
12842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012843#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12844 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012845 ((void) cert);
12846 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012847 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012848#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012850#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12851 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012852 {
12853 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012854 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012856 case MBEDTLS_KEY_EXCHANGE_RSA:
12857 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012858 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012859 break;
12860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012861 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12862 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12863 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12864 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012865 break;
12866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012867 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12868 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012869 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012870 break;
12871
12872 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012873 case MBEDTLS_KEY_EXCHANGE_NONE:
12874 case MBEDTLS_KEY_EXCHANGE_PSK:
12875 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12876 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012877 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012878 usage = 0;
12879 }
12880 }
12881 else
12882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012883 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12884 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012885 }
12886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012887 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012888 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012889 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012890 ret = -1;
12891 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012892#else
12893 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012894#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012896#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12897 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012899 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12900 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012901 }
12902 else
12903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012904 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12905 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012906 }
12907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012908 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012909 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012910 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012911 ret = -1;
12912 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012913#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012914
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012915 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012916}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012917#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012918
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012919#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12920 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12921int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12922 unsigned char *output,
12923 unsigned char *data, size_t data_len )
12924{
12925 int ret = 0;
12926 mbedtls_md5_context mbedtls_md5;
12927 mbedtls_sha1_context mbedtls_sha1;
12928
12929 mbedtls_md5_init( &mbedtls_md5 );
12930 mbedtls_sha1_init( &mbedtls_sha1 );
12931
12932 /*
12933 * digitally-signed struct {
12934 * opaque md5_hash[16];
12935 * opaque sha_hash[20];
12936 * };
12937 *
12938 * md5_hash
12939 * MD5(ClientHello.random + ServerHello.random
12940 * + ServerParams);
12941 * sha_hash
12942 * SHA(ClientHello.random + ServerHello.random
12943 * + ServerParams);
12944 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012945 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012946 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012947 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012948 goto exit;
12949 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012950 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012951 ssl->handshake->randbytes, 64 ) ) != 0 )
12952 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012953 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012954 goto exit;
12955 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012956 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012957 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012959 goto exit;
12960 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012961 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012962 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012964 goto exit;
12965 }
12966
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012967 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012968 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012970 goto exit;
12971 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012972 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012973 ssl->handshake->randbytes, 64 ) ) != 0 )
12974 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012976 goto exit;
12977 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012978 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012979 data_len ) ) != 0 )
12980 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012982 goto exit;
12983 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012984 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012985 output + 16 ) ) != 0 )
12986 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012988 goto exit;
12989 }
12990
12991exit:
12992 mbedtls_md5_free( &mbedtls_md5 );
12993 mbedtls_sha1_free( &mbedtls_sha1 );
12994
12995 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012996 mbedtls_ssl_pend_fatal_alert( ssl,
12997 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012998
12999 return( ret );
13000
13001}
13002#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
13003 MBEDTLS_SSL_PROTO_TLS1_1 */
13004
13005#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
13006 defined(MBEDTLS_SSL_PROTO_TLS1_2)
13007int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020013008 unsigned char *hash, size_t *hashlen,
13009 unsigned char *data, size_t data_len,
13010 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010013011{
13012 int ret = 0;
13013 mbedtls_md_context_t ctx;
Andrzej Kurek84bde412020-07-06 15:27:34 -040013014 volatile unsigned char* hash_dup = hash;
13015 volatile size_t *hashlen_dup = hashlen;
13016 volatile unsigned char* data_dup = data;
13017 volatile size_t data_len_dup = data_len;
13018
Hanno Beckera5cedbc2019-07-17 11:21:02 +010013019 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020013020 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010013021
13022 mbedtls_md_init( &ctx );
13023
13024 /*
13025 * digitally-signed struct {
13026 * opaque client_random[32];
13027 * opaque server_random[32];
13028 * ServerDHParams params;
13029 * };
13030 */
13031 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
13032 {
13033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
13034 goto exit;
13035 }
13036 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
13037 {
13038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
13039 goto exit;
13040 }
13041 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
13042 {
13043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
13044 goto exit;
13045 }
13046 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
13047 {
13048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
13049 goto exit;
13050 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020013051 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010013052 {
13053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
13054 goto exit;
13055 }
13056
13057exit:
13058 mbedtls_md_free( &ctx );
13059
13060 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010013061 mbedtls_ssl_pend_fatal_alert( ssl,
13062 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010013063
Andrzej Kurek84bde412020-07-06 15:27:34 -040013064 /* Secure against buffer substitution */
13065 if( hash_dup == hash && hashlen_dup == hashlen &&
13066 data_dup == data && data_len_dup == data_len )
13067 {
13068 return( ret );
13069 }
13070 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010013071}
13072#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
13073 MBEDTLS_SSL_PROTO_TLS1_2 */
13074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013075#endif /* MBEDTLS_SSL_TLS_C */