blob: ad2dc9809b0f5b2c53d94897043fe81e8633cab4 [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 {
235 return MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
236 }
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{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200291 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
293
Hanno Becker07489862019-04-25 16:01:49 +0100294 ssl->negotiate_cid = enable;
295 if( enable == MBEDTLS_SSL_CID_DISABLED )
296 {
297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
298 return( 0 );
299 }
300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100301 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100302
Hanno Beckere0200da2019-06-13 09:23:43 +0100303 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100304 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
306 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100307 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
309 }
310
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300311 /* Not using more secure mbedtls_platform_memcpy as cid is public */
312 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100313 /* Truncation is not an issue here because
314 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
315 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100316
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100317 return( 0 );
318}
319
320int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
321 int *enabled,
322 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
323 size_t *peer_cid_len )
324{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100325 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100326
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200327 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100328 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
329 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100331 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100332
Hanno Beckercb063f52019-05-03 12:54:52 +0100333 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
334 * were used, but client and server requested the empty CID.
335 * This is indistinguishable from not using the CID extension
336 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100337 if( ssl->transform_in->in_cid_len == 0 &&
338 ssl->transform_in->out_cid_len == 0 )
339 {
340 return( 0 );
341 }
342
Hanno Becker633d6042019-05-22 16:50:35 +0100343 if( peer_cid_len != NULL )
344 {
345 *peer_cid_len = ssl->transform_in->out_cid_len;
346 if( peer_cid != NULL )
347 {
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300348 /* Not using more secure mbedtls_platform_memcpy as cid is public */
349 memcpy( peer_cid, ssl->transform_in->out_cid,
Hanno Becker633d6042019-05-22 16:50:35 +0100350 ssl->transform_in->out_cid_len );
351 }
352 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100353
354 *enabled = MBEDTLS_SSL_CID_ENABLED;
355
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100356 return( 0 );
357}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100358#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100359
Hanno Beckerd5847772018-08-28 10:09:23 +0100360/* Forward declarations for functions related to message buffering. */
361static void ssl_buffering_free( mbedtls_ssl_context *ssl );
362static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
363 uint8_t slot );
364static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
365static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
366static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
367static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100368static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
369 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100370static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100371
Hanno Beckera67dee22018-08-22 10:05:20 +0100372static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100373static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100374{
Hanno Becker11682cc2018-08-22 14:41:02 +0100375 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100376
377 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100378 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100379
380 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
381}
382
Hanno Becker67bc7c32018-08-06 11:33:50 +0100383static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
384{
Hanno Becker11682cc2018-08-22 14:41:02 +0100385 size_t const bytes_written = ssl->out_left;
386 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100387
388 /* Double-check that the write-index hasn't gone
389 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100390 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100391 {
392 /* Should never happen... */
393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
394 }
395
396 return( (int) ( mtu - bytes_written ) );
397}
398
399static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
400{
401 int ret;
402 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400403 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100404
405#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
406 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
407
408 if( max_len > mfl )
409 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100410
411 /* By the standard (RFC 6066 Sect. 4), the MFL extension
412 * only limits the maximum record payload size, so in theory
413 * we would be allowed to pack multiple records of payload size
414 * MFL into a single datagram. However, this would mean that there's
415 * no way to explicitly communicate MTU restrictions to the peer.
416 *
417 * The following reduction of max_len makes sure that we never
418 * write datagrams larger than MFL + Record Expansion Overhead.
419 */
420 if( max_len <= ssl->out_left )
421 return( 0 );
422
423 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100424#endif
425
426 ret = ssl_get_remaining_space_in_datagram( ssl );
427 if( ret < 0 )
428 return( ret );
429 remaining = (size_t) ret;
430
431 ret = mbedtls_ssl_get_record_expansion( ssl );
432 if( ret < 0 )
433 return( ret );
434 expansion = (size_t) ret;
435
436 if( remaining <= expansion )
437 return( 0 );
438
439 remaining -= expansion;
440 if( remaining >= max_len )
441 remaining = max_len;
442
443 return( (int) remaining );
444}
445
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446/*
447 * Double the retransmit timeout value, within the allowed range,
448 * returning -1 if the maximum value has already been reached.
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200451{
452 uint32_t new_timeout;
453
Hanno Becker1f835fa2019-06-13 10:14:59 +0100454 if( ssl->handshake->retransmit_timeout >=
455 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
456 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200457 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100458 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200459
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200460 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
461 * in the following way: after the initial transmission and a first
462 * retransmission, back off to a temporary estimated MTU of 508 bytes.
463 * This value is guaranteed to be deliverable (if not guaranteed to be
464 * delivered) of any compliant IPv4 (and IPv6) network, and should work
465 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 if( ssl->handshake->retransmit_timeout !=
467 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400468 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200469 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
471 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200472
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 new_timeout = 2 * ssl->handshake->retransmit_timeout;
474
475 /* Avoid arithmetic overflow and range overflow */
476 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100477 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200478 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100479 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200480 }
481
482 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200484 ssl->handshake->retransmit_timeout ) );
485
486 return( 0 );
487}
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200490{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100491 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200493 ssl->handshake->retransmit_timeout ) );
494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200498/*
499 * Convert max_fragment_length codes to length.
500 * RFC 6066 says:
501 * enum{
502 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
503 * } MaxFragmentLength;
504 * and we add 0 -> extension unused
505 */
Angus Grattond8213d02016-05-25 20:56:48 +1000506static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200507{
Angus Grattond8213d02016-05-25 20:56:48 +1000508 switch( mfl )
509 {
510 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300511 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000512 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
513 return 512;
514 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
515 return 1024;
516 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
517 return 2048;
518 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
519 return 4096;
520 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300521 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000522 }
523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200525
Hanno Becker58fccf22019-02-06 14:30:46 +0000526int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
527 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_ssl_session_free( dst );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300530 mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000533
534#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200535 if( src->peer_cert != NULL )
536 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200537 int ret;
538
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200539 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200540 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200541 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200546 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200549 dst->peer_cert = NULL;
550 return( ret );
551 }
552 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100553#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 if( src->peer_cert_digest != NULL )
555 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000557 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000558 if( dst->peer_cert_digest == NULL )
559 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
560
Teppo Järvelin91d79382019-10-02 09:09:31 +0300561 mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest,
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000562 src->peer_cert_digest_len );
563 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000564 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000565 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100566#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200570#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200571 if( src->ticket != NULL )
572 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200573 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200574 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200575 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200576
Teppo Järvelin91d79382019-10-02 09:09:31 +0300577 mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200578 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200579#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200580
581 return( 0 );
582}
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
585int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200586 const unsigned char *key_enc, const unsigned char *key_dec,
587 size_t keylen,
588 const unsigned char *iv_enc, const unsigned char *iv_dec,
589 size_t ivlen,
590 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200591 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
593int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
594int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
595int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
596int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
597#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000598
Paul Bakker5121ce52009-01-03 21:22:43 +0000599/*
600 * Key material generation
601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100603MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200604 const char *label,
605 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000606 unsigned char *dstbuf, size_t dlen )
607{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100608 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000609 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200610 mbedtls_md5_context md5;
611 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000612 unsigned char padding[16];
613 unsigned char sha1sum[20];
614 ((void)label);
615
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200616 mbedtls_md5_init( &md5 );
617 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200618
Paul Bakker5f70b252012-09-13 14:23:06 +0000619 /*
620 * SSLv3:
621 * block =
622 * MD5( secret + SHA1( 'A' + secret + random ) ) +
623 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
624 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
625 * ...
626 */
627 for( i = 0; i < dlen / 16; i++ )
628 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200629 mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100639 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100640 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000641
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100642 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100643 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100644 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100645 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100646 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100647 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100648 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100649 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000650 }
651
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100652exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200653 mbedtls_md5_free( &md5 );
654 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000655
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500656 mbedtls_platform_zeroize( padding, sizeof( padding ) );
657 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000658
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100659 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100664MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200665 const char *label,
666 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000667 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000668{
Paul Bakker23986e52011-04-24 08:57:21 +0000669 size_t nb, hs;
670 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200671 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 unsigned char tmp[128];
673 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100674 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100676 int ret;
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000679
680 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
683 hs = ( slen + 1 ) / 2;
684 S1 = secret;
685 S2 = secret + slen - hs;
686
687 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300688 mbedtls_platform_memcpy( tmp + 20, label, nb );
689 mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 nb += rlen;
691
692 /*
693 * First compute P_md5(secret,label+random)[0..dlen]
694 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100695 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
696 MBEDTLS_MD_INVALID_HANDLE )
697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100699 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100702 return( ret );
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
705 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
706 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707
708 for( i = 0; i < dlen; i += 16 )
709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_hmac_reset ( &md_ctx );
711 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
712 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_md_hmac_reset ( &md_ctx );
715 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
716 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
718 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
719
720 for( j = 0; j < k; j++ )
721 dstbuf[i + j] = h_i[j];
722 }
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100725
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 /*
727 * XOR out with P_sha1(secret,label+random)[0..dlen]
728 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100729 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
730 MBEDTLS_MD_INVALID_HANDLE )
731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100733 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100736 return( ret );
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
739 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
740 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000741
742 for( i = 0; i < dlen; i += 20 )
743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_md_hmac_reset ( &md_ctx );
745 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
746 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_md_hmac_reset ( &md_ctx );
749 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
750 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
753
754 for( j = 0; j < k; j++ )
755 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
756 }
757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100759
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500760 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
761 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763 return( 0 );
764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100768#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
769MBEDTLS_ALWAYS_INLINE static inline
770#else
771static
772#endif
773int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774 const unsigned char *secret, size_t slen,
775 const char *label,
776 const unsigned char *random, size_t rlen,
777 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000778{
779 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100780 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100783 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100785 int ret;
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000788
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100789 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
790 MBEDTLS_MD_INVALID_HANDLE )
791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100793 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100796
797 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000799
800 nb = strlen( label );
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200801 (void)mbedtls_platform_memcpy( tmp + md_len, label, nb );
802 (void)mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000803 nb += rlen;
804
805 /*
806 * Compute P_<hash>(secret, label + random)[0..dlen]
807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100809 return( ret );
810
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200811 if ( ( ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ) ) != 0 )
812 return( ret );
813 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ) ) != 0 )
814 return( ret );
815 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
816 return( ret );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100817
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000819 {
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200820 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
821 return( ret );
822 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ) ) != 0 )
823 return( ret );
824 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, h_i ) ) != 0 )
825 return( ret );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100826
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200827 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
828 return( ret );
829 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ) ) != 0 )
830 return( ret );
831 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
832 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000833
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100834 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000835
836 for( j = 0; j < k; j++ )
837 dstbuf[i + j] = h_i[j];
838 }
839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100841
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200842 (void)mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
843 (void)mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000844
845 return( 0 );
846}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100849MBEDTLS_NO_INLINE static int tls_prf_sha256(
850 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100851 const char *label,
852 const unsigned char *random, size_t rlen,
853 unsigned char *dstbuf, size_t dlen )
854{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100856 label, random, rlen, dstbuf, dlen ) );
857}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100861MBEDTLS_NO_INLINE static int tls_prf_sha384(
862 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200863 const char *label,
864 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000865 unsigned char *dstbuf, size_t dlen )
866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100868 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#endif /* MBEDTLS_SHA512_C */
871#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000872
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100873/*
874 * Call the appropriate PRF function
875 */
Hanno Becker2793f742019-08-16 14:28:43 +0100876MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100877 mbedtls_md_type_t hash,
878 const unsigned char *secret, size_t slen,
879 const char *label,
880 const unsigned char *random, size_t rlen,
881 unsigned char *dstbuf, size_t dlen )
882{
883#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
884 (void) hash;
885#endif
886
887#if defined(MBEDTLS_SSL_PROTO_SSL3)
888 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
889 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
890 else
891#endif
892#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100893 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100894 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
895 else
896#endif
897#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
898#if defined(MBEDTLS_SHA512_C)
899 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
900 hash == MBEDTLS_MD_SHA384 )
901 {
902 return( tls_prf_sha384( secret, slen, label, random, rlen,
903 dstbuf, dlen ) );
904 }
905 else
906#endif
907#if defined(MBEDTLS_SHA256_C)
908 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
909 {
910 return( tls_prf_sha256( secret, slen, label, random, rlen,
911 dstbuf, dlen ) );
912 }
913#endif
914#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
915
916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
917}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200918
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100919#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100920MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100921 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
922{
923 const char *sender;
924 mbedtls_md5_context md5;
925 mbedtls_sha1_context sha1;
926
927 unsigned char padbuf[48];
928 unsigned char md5sum[16];
929 unsigned char sha1sum[20];
930
931 mbedtls_ssl_session *session = ssl->session_negotiate;
932 if( !session )
933 session = ssl->session;
934
935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
936
937 mbedtls_md5_init( &md5 );
938 mbedtls_sha1_init( &sha1 );
939
940 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
941 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
942
943 /*
944 * SSLv3:
945 * hash =
946 * MD5( master + pad2 +
947 * MD5( handshake + sender + master + pad1 ) )
948 * + SHA1( master + pad2 +
949 * SHA1( handshake + sender + master + pad1 ) )
950 */
951
952#if !defined(MBEDTLS_MD5_ALT)
953 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
954 md5.state, sizeof( md5.state ) );
955#endif
956
957#if !defined(MBEDTLS_SHA1_ALT)
958 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
959 sha1.state, sizeof( sha1.state ) );
960#endif
961
962 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
963 : "SRVR";
964
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200965 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100966
967 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
968 mbedtls_md5_update_ret( &md5, session->master, 48 );
969 mbedtls_md5_update_ret( &md5, padbuf, 48 );
970 mbedtls_md5_finish_ret( &md5, md5sum );
971
972 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
973 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
974 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
975 mbedtls_sha1_finish_ret( &sha1, sha1sum );
976
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200977 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100978
979 mbedtls_md5_starts_ret( &md5 );
980 mbedtls_md5_update_ret( &md5, session->master, 48 );
981 mbedtls_md5_update_ret( &md5, padbuf, 48 );
982 mbedtls_md5_update_ret( &md5, md5sum, 16 );
983 mbedtls_md5_finish_ret( &md5, buf );
984
985 mbedtls_sha1_starts_ret( &sha1 );
986 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
987 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
988 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
989 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
990
991 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
992
993 mbedtls_md5_free( &md5 );
994 mbedtls_sha1_free( &sha1 );
995
996 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
997 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
998 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
999
1000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1001}
1002#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1003
1004#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +01001005MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001006 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1007{
1008 int len = 12;
1009 const char *sender;
1010 mbedtls_md5_context md5;
1011 mbedtls_sha1_context sha1;
1012 unsigned char padbuf[36];
1013
1014 mbedtls_ssl_session *session = ssl->session_negotiate;
1015 if( !session )
1016 session = ssl->session;
1017
1018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1019
1020 mbedtls_md5_init( &md5 );
1021 mbedtls_sha1_init( &sha1 );
1022
1023 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1024 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1025
1026 /*
1027 * TLSv1:
1028 * hash = PRF( master, finished_label,
1029 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1030 */
1031
1032#if !defined(MBEDTLS_MD5_ALT)
1033 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1034 md5.state, sizeof( md5.state ) );
1035#endif
1036
1037#if !defined(MBEDTLS_SHA1_ALT)
1038 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1039 sha1.state, sizeof( sha1.state ) );
1040#endif
1041
1042 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1043 ? "client finished"
1044 : "server finished";
1045
1046 mbedtls_md5_finish_ret( &md5, padbuf );
1047 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1048
1049 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1050 mbedtls_ssl_suite_get_mac(
1051 mbedtls_ssl_ciphersuite_from_id(
1052 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1053 session->master, 48, sender,
1054 padbuf, 36, buf, len );
1055
1056 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1057
1058 mbedtls_md5_free( &md5 );
1059 mbedtls_sha1_free( &sha1 );
1060
1061 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1062
1063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1064}
1065#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1066
1067#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1068#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001069MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001070 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1071{
1072 int len = 12;
1073 const char *sender;
1074 mbedtls_sha256_context sha256;
1075 unsigned char padbuf[32];
1076
1077 mbedtls_ssl_session *session = ssl->session_negotiate;
1078 if( !session )
1079 session = ssl->session;
1080
1081 mbedtls_sha256_init( &sha256 );
1082
1083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1084
1085 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1086
1087 /*
1088 * TLSv1.2:
1089 * hash = PRF( master, finished_label,
1090 * Hash( handshake ) )[0.11]
1091 */
1092
1093#if !defined(MBEDTLS_SHA256_ALT)
1094 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1095 sha256.state, sizeof( sha256.state ) );
1096#endif
1097
1098 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1099 ? "client finished"
1100 : "server finished";
1101
1102 mbedtls_sha256_finish_ret( &sha256, padbuf );
1103
1104 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1105 mbedtls_ssl_suite_get_mac(
1106 mbedtls_ssl_ciphersuite_from_id(
1107 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1108 session->master, 48, sender,
1109 padbuf, 32, buf, len );
1110
1111 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1112
1113 mbedtls_sha256_free( &sha256 );
1114
1115 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1116
1117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1118}
1119#endif /* MBEDTLS_SHA256_C */
1120
1121#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001122MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001123 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1124{
1125 int len = 12;
1126 const char *sender;
1127 mbedtls_sha512_context sha512;
1128 unsigned char padbuf[48];
1129
1130 mbedtls_ssl_session *session = ssl->session_negotiate;
1131 if( !session )
1132 session = ssl->session;
1133
1134 mbedtls_sha512_init( &sha512 );
1135
1136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1137
1138 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1139
1140 /*
1141 * TLSv1.2:
1142 * hash = PRF( master, finished_label,
1143 * Hash( handshake ) )[0.11]
1144 */
1145
1146#if !defined(MBEDTLS_SHA512_ALT)
1147 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1148 sha512.state, sizeof( sha512.state ) );
1149#endif
1150
1151 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1152 ? "client finished"
1153 : "server finished";
1154
1155 mbedtls_sha512_finish_ret( &sha512, padbuf );
1156
1157 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1158 mbedtls_ssl_suite_get_mac(
1159 mbedtls_ssl_ciphersuite_from_id(
1160 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1161 session->master, 48, sender,
1162 padbuf, 48, buf, len );
1163
1164 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1165
1166 mbedtls_sha512_free( &sha512 );
1167
1168 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1169
1170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1171}
1172#endif /* MBEDTLS_SHA512_C */
1173#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1174
Hanno Becker2793f742019-08-16 14:28:43 +01001175MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1176 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001177 mbedtls_md_type_t hash,
1178 mbedtls_ssl_context *ssl,
1179 unsigned char *buf,
1180 int from )
1181{
1182#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1183 (void) hash;
1184#endif
1185
1186#if defined(MBEDTLS_SSL_PROTO_SSL3)
1187 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1188 ssl_calc_finished_ssl( ssl, buf, from );
1189 else
1190#endif
1191#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001192 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001193 ssl_calc_finished_tls( ssl, buf, from );
1194 else
1195#endif
1196#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1197#if defined(MBEDTLS_SHA512_C)
1198 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1199 hash == MBEDTLS_MD_SHA384 )
1200 {
1201 ssl_calc_finished_tls_sha384( ssl, buf, from );
1202 }
1203 else
1204#endif
1205#if defined(MBEDTLS_SHA256_C)
1206 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1207 ssl_calc_finished_tls_sha256( ssl, buf, from );
1208 else
1209#endif
1210#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1211 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1212
1213 return( 0 );
1214}
1215
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001216/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001217 * Populate a transform structure with session keys and all the other
1218 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001219 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001220 * Parameters:
1221 * - [in/out]: transform: structure to populate
1222 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001223 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001224 * - [in] ciphersuite
1225 * - [in] master
1226 * - [in] encrypt_then_mac
1227 * - [in] trunc_hmac
1228 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001229 * - [in] tls_prf: pointer to PRF to use for key derivation
1230 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001231 * - [in] minor_ver: SSL/TLS minor version
1232 * - [in] endpoint: client or server
1233 * - [in] ssl: optionally used for:
1234 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1235 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1236 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001237 */
Hanno Becker298a4702019-08-16 10:21:32 +01001238/* Force compilers to inline this function if it's used only
1239 * from one place, because at least ARMC5 doesn't do that
1240 * automatically. */
1241#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1242MBEDTLS_ALWAYS_INLINE static inline
1243#else
1244static
1245#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1246int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001247 int ciphersuite,
1248 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001249#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001250#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1251 int encrypt_then_mac,
1252#endif
1253#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1254 int trunc_hmac,
1255#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001256#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001257#if defined(MBEDTLS_ZLIB_SUPPORT)
1258 int compression,
1259#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001260 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001261 int minor_ver,
1262 unsigned endpoint,
Manuel Pégourié-Gonnard13bebd02020-03-13 11:28:19 +01001263#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1264 const
1265#endif
1266 mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001267{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001268 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 unsigned char keyblk[256];
1270 unsigned char *key1;
1271 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001272 unsigned char *mac_enc;
1273 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001274 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001275 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001276 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001277 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001279 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001280
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001281#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1282 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1283 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001284 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001285 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001286#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001287
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001288 /*
1289 * Some data just needs copying into the structure
1290 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001291#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1292 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001293 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001294#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001295
1296#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001297 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001298#else
1299 ((void) minor_ver);
1300#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001301
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001302#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001303 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001304#endif
1305
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001306 /*
1307 * Get various info structures
1308 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001309 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001310 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001311 {
1312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001313 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1315 }
1316
Hanno Becker473f98f2019-06-26 10:27:32 +01001317 cipher_info = mbedtls_cipher_info_from_type(
1318 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001319 if( cipher_info == NULL )
1320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001322 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001324 }
1325
Hanno Becker473f98f2019-06-26 10:27:32 +01001326 md_info = mbedtls_md_info_from_type(
1327 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001328 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001331 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001333 }
1334
Hanno Beckera5a2b082019-05-15 14:03:01 +01001335#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001336 /* Copy own and peer's CID if the use of the CID
1337 * extension has been negotiated. */
1338 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1339 {
1340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001341
Hanno Becker4932f9f2019-05-03 15:23:51 +01001342 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001343 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1344 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001345 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001346 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001347
1348 transform->out_cid_len = ssl->handshake->peer_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001349 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1350 memcpy( transform->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001351 ssl->handshake->peer_cid_len );
1352 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1353 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001354 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001355#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001356
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001358 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001359 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001360 ret = ssl_prf( minor_ver,
1361 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1362 master, 48, "key expansion", randbytes, 64,
1363 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001364 if( ret != 0 )
1365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001367 return( ret );
1368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001371 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001372 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001373 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 /*
1377 * Determine the appropriate key, IV and MAC length.
1378 */
Paul Bakker68884e32013-01-07 18:20:04 +01001379
Hanno Beckere7f2df02017-12-27 08:17:40 +00001380 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381
Hanno Beckerf1229442018-01-03 15:32:31 +00001382#if defined(MBEDTLS_GCM_C) || \
1383 defined(MBEDTLS_CCM_C) || \
1384 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001386 cipher_info->mode == MBEDTLS_MODE_CCM ||
1387 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001389 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001390 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001391 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1392 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001393
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001394 /* All modes haves 96-bit IVs;
1395 * GCM and CCM has 4 implicit and 8 explicit bytes
1396 * ChachaPoly has all 12 bytes implicit
1397 */
Paul Bakker68884e32013-01-07 18:20:04 +01001398 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001399 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1400 transform->fixed_ivlen = 12;
1401 else
1402 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001403 }
1404 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001405#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1406#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1407 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1408 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001409 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001410 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1412 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001415 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001418 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001419 mac_key_len = mbedtls_md_get_size( md_info );
1420 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001423 /*
1424 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1425 * (rfc 6066 page 13 or rfc 2104 section 4),
1426 * so we only need to adjust the length here.
1427 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001428 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001431
1432#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1433 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001434 * HMAC implementation which also truncates the key
1435 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001436 mac_key_len = transform->maclen;
1437#endif
1438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001440
1441 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001442 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001444 else
1445#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1446 {
1447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1448 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Hanno Beckera9d5c452019-07-25 16:47:12 +01001451 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001452 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001453 (unsigned) transform->ivlen,
1454 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
1456 /*
1457 * Finally setup the cipher contexts, IVs and MAC secrets.
1458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001460 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001461 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001462 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001463 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001464
Paul Bakker68884e32013-01-07 18:20:04 +01001465 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001466 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001468 /*
1469 * This is not used in TLS v1.1.
1470 */
Paul Bakker48916f92012-09-16 19:57:18 +00001471 iv_copy_len = ( transform->fixed_ivlen ) ?
1472 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001473 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1474 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001475 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 }
1477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_SSL_CLI_C */
1479#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001480 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001482 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001483 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001484
Hanno Becker81c7b182017-11-09 18:39:33 +00001485 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001486 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001487
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001488 /*
1489 * This is not used in TLS v1.1.
1490 */
Paul Bakker48916f92012-09-16 19:57:18 +00001491 iv_copy_len = ( transform->fixed_ivlen ) ?
1492 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001493 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1494 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001495 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001496 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001497 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Hanno Becker92231322018-01-03 15:32:51 +00001504#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001506 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001507 {
Hanno Becker92231322018-01-03 15:32:51 +00001508 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1511 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001512 }
1513
Teppo Järvelin91d79382019-10-02 09:09:31 +03001514 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1515 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001516 }
1517 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1519#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1520 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001521 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001522 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001523 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1524 For AEAD-based ciphersuites, there is nothing to do here. */
1525 if( mac_key_len != 0 )
1526 {
1527 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1528 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1529 }
Paul Bakker68884e32013-01-07 18:20:04 +01001530 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001531 else
1532#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001536 }
Hanno Becker92231322018-01-03 15:32:51 +00001537#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1540 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001543
Hanno Beckere7f2df02017-12-27 08:17:40 +00001544 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001545 transform->iv_enc, transform->iv_dec,
1546 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001547 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001548 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1551 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001552 }
1553 }
Hanno Becker92231322018-01-03 15:32:51 +00001554#else
1555 ((void) mac_dec);
1556 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001558
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001559#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1560 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001561 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001562 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001563 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001564 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001565 iv_copy_len );
1566 }
1567#endif
1568
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001569 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001570 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001573 return( ret );
1574 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001575
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001576 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001577 cipher_info ) ) != 0 )
1578 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001580 return( ret );
1581 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001584 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 return( ret );
1589 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001592 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001596 return( ret );
1597 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#if defined(MBEDTLS_CIPHER_MODE_CBC)
1600 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1603 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001606 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001607 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1610 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001613 return( ret );
1614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001617
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001618 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001619
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001620 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001622 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001625
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001626 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1627 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001628
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001629 if( deflateInit( &transform->ctx_deflate,
1630 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001631 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1634 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001635 }
1636 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001638
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 return( 0 );
1640}
1641
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001642#if defined(MBEDTLS_SSL_PROTO_SSL3)
1643static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1644 unsigned char hash[36],
1645 size_t *hlen )
1646{
1647 mbedtls_md5_context md5;
1648 mbedtls_sha1_context sha1;
1649 unsigned char pad_1[48];
1650 unsigned char pad_2[48];
1651
1652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1653
1654 mbedtls_md5_init( &md5 );
1655 mbedtls_sha1_init( &sha1 );
1656
1657 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1658 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1659
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001660 mbedtls_platform_memset( pad_1, 0x36, 48 );
1661 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001662
1663 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1664 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1665 mbedtls_md5_finish_ret( &md5, hash );
1666
1667 mbedtls_md5_starts_ret( &md5 );
1668 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1669 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1670 mbedtls_md5_update_ret( &md5, hash, 16 );
1671 mbedtls_md5_finish_ret( &md5, hash );
1672
1673 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1674 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1675 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1676
1677 mbedtls_sha1_starts_ret( &sha1 );
1678 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1679 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1680 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1681 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1682
1683 *hlen = 36;
1684
1685 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1687
1688 mbedtls_md5_free( &md5 );
1689 mbedtls_sha1_free( &sha1 );
1690
1691 return;
1692}
1693#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1694
1695#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1696static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1697 unsigned char hash[36],
1698 size_t *hlen )
1699{
1700 mbedtls_md5_context md5;
1701 mbedtls_sha1_context sha1;
1702
1703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1704
1705 mbedtls_md5_init( &md5 );
1706 mbedtls_sha1_init( &sha1 );
1707
1708 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1709 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1710
1711 mbedtls_md5_finish_ret( &md5, hash );
1712 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1713
1714 *hlen = 36;
1715
1716 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1718
1719 mbedtls_md5_free( &md5 );
1720 mbedtls_sha1_free( &sha1 );
1721
1722 return;
1723}
1724#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1725
1726#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1727#if defined(MBEDTLS_SHA256_C)
1728static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1729 unsigned char hash[32],
1730 size_t *hlen )
1731{
1732 mbedtls_sha256_context sha256;
1733
1734 mbedtls_sha256_init( &sha256 );
1735
1736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1737
1738 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1739 mbedtls_sha256_finish_ret( &sha256, hash );
1740
1741 *hlen = 32;
1742
1743 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1745
1746 mbedtls_sha256_free( &sha256 );
1747
1748 return;
1749}
1750#endif /* MBEDTLS_SHA256_C */
1751
1752#if defined(MBEDTLS_SHA512_C)
1753static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1754 unsigned char hash[48],
1755 size_t *hlen )
1756{
1757 mbedtls_sha512_context sha512;
1758
1759 mbedtls_sha512_init( &sha512 );
1760
1761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1762
1763 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1764 mbedtls_sha512_finish_ret( &sha512, hash );
1765
1766 *hlen = 48;
1767
1768 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1770
1771 mbedtls_sha512_free( &sha512 );
1772
1773 return;
1774}
1775#endif /* MBEDTLS_SHA512_C */
1776#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1777
Hanno Becker2f41b242019-08-15 17:29:43 +01001778int mbedtls_ssl_calc_verify( int minor_ver,
1779 mbedtls_md_type_t hash,
1780 mbedtls_ssl_context const *ssl,
1781 unsigned char *dst,
1782 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001783{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001784#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1785 (void) hash;
1786#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001787
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001788#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001789 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001790 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001791 else
1792#endif
1793#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001794 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001795 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001796 else
1797#endif
1798#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1799#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001800 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1801 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001802 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001803 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001804 }
1805 else
1806#endif
1807#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001808 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001809 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001810 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001811 }
1812 else
1813#endif
1814#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1815 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001816 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1817 }
1818
1819 return( 0 );
1820}
1821
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001822/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001823 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001824 *
1825 * Parameters:
1826 * [in/out] handshake
1827 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1828 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001829 * [out] master
1830 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001832static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001833 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001834 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001835{
1836 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001837
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001838/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1839/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1840/* (void) ssl; */
1841/* #endif */
1842
1843 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1844 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001845
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001846#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001847 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001848 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1850 return( 0 );
1851 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001852#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001853
1854 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1855 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001856
1857#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001858 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1859 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001860 {
1861 unsigned char session_hash[48];
1862 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001863
Hanno Becker2f41b242019-08-15 17:29:43 +01001864 mbedtls_ssl_calc_verify(
1865 mbedtls_ssl_get_minor_ver( ssl ),
1866 mbedtls_ssl_suite_get_mac( ciphersuite ),
1867 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001868
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001869 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1870 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001871
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001872 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1873 mbedtls_ssl_suite_get_mac( ciphersuite ),
1874 handshake->premaster, handshake->pmslen,
1875 "extended master secret",
1876 session_hash, hash_len,
1877 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001878 }
1879 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001880#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001881 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001882 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1883 mbedtls_ssl_suite_get_mac( ciphersuite ),
1884 handshake->premaster, handshake->pmslen,
1885 "master secret",
1886 handshake->randbytes, 64,
1887 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001888 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001889 if( ret != 0 )
1890 {
1891 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1892 return( ret );
1893 }
1894
1895 mbedtls_platform_zeroize( handshake->premaster,
1896 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001897
1898 return( 0 );
1899}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001900
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001901int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1902{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001903 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001904
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001906 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001907 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001908 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001909 ssl->session_negotiate->master,
1910 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001911 if( ret != 0 )
1912 {
1913 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1914 return( ret );
1915 }
1916
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001917 /* Swap the client and server random values:
1918 * - MS derivation wanted client+server (RFC 5246 8.1)
1919 * - key derivation wants server+client (RFC 5246 6.3) */
1920 {
1921 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001922 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1923 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1924 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001925 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1926 }
1927
1928 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001929 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001930 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1931 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001932#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001933#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001934 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001935#endif
1936#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001937 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001938#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001939#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001940#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001941 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001942#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001943 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001944 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001945 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1946 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001947 if( ret == 0 )
1948 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001949 mbedtls_platform_random_delay();
Jarno Lamsa4031a452019-12-19 08:11:12 +02001950 if( ret == 0 )
1951 {
1952 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1953 }
1954 else
1955 {
1956 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1957 }
1958 }
1959 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001960 {
1961 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1962 return( ret );
1963 }
1964
1965 /* We no longer need Server/ClientHello.random values */
1966 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1967 sizeof( ssl->handshake->randbytes ) );
1968
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001969 /* Allocate compression buffer */
1970#if defined(MBEDTLS_ZLIB_SUPPORT)
jiblime92af9a92019-12-18 21:40:01 -08001971 if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001972 ssl->compress_buf == NULL )
1973 {
1974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1975 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1976 if( ssl->compress_buf == NULL )
1977 {
1978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001979 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001980 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1981 }
1982 }
1983#endif
1984
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1986
1987 return( 0 );
1988}
1989
Hanno Becker09d23642019-07-22 17:18:18 +01001990int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1991{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001992 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Becker09d23642019-07-22 17:18:18 +01001993
1994 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1995 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001996#if defined(MBEDTLS_USE_TINYCRYPT)
1997 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001998 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001999 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01002000 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2001 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2002 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2003 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2004 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002005 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002006 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2007 ssl->handshake->ecdh_privkey,
2008 ssl->handshake->premaster );
2009 if( ret == UECC_FAULT_DETECTED )
2010 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2011 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002012 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002013 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01002014
2015 ssl->handshake->pmslen = NUM_ECC_BYTES;
2016 }
2017 else
2018#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002019#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2020 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2021 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2022 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002023 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002024 ssl->handshake->premaster,
2025 MBEDTLS_PREMASTER_SIZE,
2026 &ssl->handshake->pmslen,
2027 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002028 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2029 if( ret == 0 )
2030 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002031 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002032 if( ret == 0 )
2033 {
2034 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2035 }
2036 else
2037 {
Piotr Nowickie048b912020-06-05 17:59:28 +02002038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret",
2039 MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2040 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002041 }
2042 }
2043 else
Hanno Becker09d23642019-07-22 17:18:18 +01002044 {
2045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2046 return( ret );
2047 }
2048
2049 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2050 }
2051 else
2052#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002053#if defined(MBEDTLS_ECDH_C) && \
2054 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2055 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2056 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2057 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002058 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2059 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2060 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2061 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2062 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2063 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2064 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2065 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2066 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002067 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002068 &ssl->handshake->pmslen,
2069 ssl->handshake->premaster,
2070 MBEDTLS_MPI_MAX_SIZE,
2071 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002072 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2073 if( ret == 0 )
2074 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002075 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002076 if( ret == 0 )
2077 {
2078 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2079 }
2080 else
2081 {
2082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002083 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002084 }
2085 }
2086 else
Hanno Becker09d23642019-07-22 17:18:18 +01002087 {
2088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2089 return( ret );
2090 }
2091
2092 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2093 }
2094 else
2095#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2096 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2097 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2098 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2099#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2100 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2101 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002102 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2103 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2104 if( ret == 0 )
2105 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002106 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002107 if( ret == 0 )
2108 {
2109 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2110 }
2111 else
2112 {
2113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002114 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002115 }
2116 }
2117 else
Hanno Becker09d23642019-07-22 17:18:18 +01002118 {
2119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2120 return( ret );
2121 }
2122 }
2123 else
2124#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2125#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2126 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2127 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2128 {
2129 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2130 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2131 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002132 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002133 if( ret == 0 )
2134 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002135 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002136 if( ret == 0 )
2137 {
2138 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2139 }
2140 else
2141 {
2142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002143 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002144 }
2145 }
2146 else
Hanno Becker09d23642019-07-22 17:18:18 +01002147 {
2148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2149 return( ret );
2150 }
2151 }
2152 else
2153#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2154#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2155 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2156 == MBEDTLS_KEY_EXCHANGE_RSA )
2157 {
2158 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002159 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002160 * ssl_rsa_generate_partial_pms(). Only the
2161 * PMS length needs to be set. */
2162 ssl->handshake->pmslen = 48;
2163 }
2164 else
2165#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2166 {
2167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2169 }
2170
2171 return( 0 );
2172}
2173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2175int 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 +02002176{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002177 unsigned char *p = ssl->handshake->premaster;
2178 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002179 const unsigned char *psk = ssl->conf->psk;
2180 size_t psk_len = ssl->conf->psk_len;
2181
2182 /* If the psk callback was called, use its result */
2183 if( ssl->handshake->psk != NULL )
2184 {
2185 psk = ssl->handshake->psk;
2186 psk_len = ssl->handshake->psk_len;
2187 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002188
2189 /*
2190 * PMS = struct {
2191 * opaque other_secret<0..2^16-1>;
2192 * opaque psk<0..2^16-1>;
2193 * };
2194 * with "other_secret" depending on the particular key exchange
2195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2197 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002198 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002199 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002201
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002202 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002203
2204 if( end < p || (size_t)( end - p ) < psk_len )
2205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2206
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002207 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002208 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002209 }
2210 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2212#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2213 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002214 {
2215 /*
2216 * other_secret already set by the ClientKeyExchange message,
2217 * and is 48 bytes long
2218 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002219 if( end - p < 2 )
2220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2221
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002222 *p++ = 0;
2223 *p++ = 48;
2224 p += 48;
2225 }
2226 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2228#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2229 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002230 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002231 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002232 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002233
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002234 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002236 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002237 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002238 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002241 return( ret );
2242 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002243 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002244 p += len;
2245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002247 }
2248 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2250#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2251 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002252 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002253 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002254 size_t zlen;
2255
Hanno Becker982da7e2019-09-02 09:47:39 +01002256#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002257 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2258 ssl->handshake->ecdh_privkey,
2259 p + 2 );
2260 if( ret == UECC_FAULT_DETECTED )
2261 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2262 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002263 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002264
2265 zlen = NUM_ECC_BYTES;
2266#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002268 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002269 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002270 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002273 return( ret );
2274 }
2275
Hanno Becker982da7e2019-09-02 09:47:39 +01002276 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2277 MBEDTLS_DEBUG_ECDH_Z );
2278#endif /* MBEDTLS_USE_TINYCRYPT */
2279
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002280 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002281 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002282 }
2283 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002288 }
2289
2290 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002291 if( end - p < 2 )
2292 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002293
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002294 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002295
2296 if( end < p || (size_t)( end - p ) < psk_len )
2297 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2298
Teppo Järvelin91d79382019-10-02 09:09:31 +03002299 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002300 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002301
2302 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2303
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002304 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2305
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002306 return( 0 );
2307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002311/*
2312 * SSLv3.0 MAC functions
2313 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002314#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002315static void ssl_mac( mbedtls_md_context_t *md_ctx,
2316 const unsigned char *secret,
2317 const unsigned char *buf, size_t len,
2318 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002319 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002320{
2321 unsigned char header[11];
2322 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002323 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2325 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002326
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002327 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002329 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002330 else
Paul Bakker68884e32013-01-07 18:20:04 +01002331 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002332
Teppo Järvelin91d79382019-10-02 09:09:31 +03002333 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002334 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002335 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002337 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 mbedtls_md_starts( md_ctx );
2339 mbedtls_md_update( md_ctx, secret, md_size );
2340 mbedtls_md_update( md_ctx, padding, padlen );
2341 mbedtls_md_update( md_ctx, header, 11 );
2342 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002343 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002344
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002345 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 mbedtls_md_starts( md_ctx );
2347 mbedtls_md_update( md_ctx, secret, md_size );
2348 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002349 mbedtls_md_update( md_ctx, out, md_size );
2350 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002351}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002353
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002354/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002355 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002356#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002357 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2358 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2359 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2360/* This function makes sure every byte in the memory region is accessed
2361 * (in ascending addresses order) */
2362static void ssl_read_memory( unsigned char *p, size_t len )
2363{
2364 unsigned char acc = 0;
2365 volatile unsigned char force;
2366
2367 for( ; len != 0; p++, len-- )
2368 acc ^= *p;
2369
2370 force = acc;
2371 (void) force;
2372}
2373#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2374
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002375/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002376 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002377 */
Hanno Becker3307b532017-12-27 21:37:21 +00002378
Hanno Beckera5a2b082019-05-15 14:03:01 +01002379#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002380/* This functions transforms a DTLS plaintext fragment and a record content
2381 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002382 *
2383 * struct {
2384 * opaque content[DTLSPlaintext.length];
2385 * ContentType real_type;
2386 * uint8 zeros[length_of_padding];
2387 * } DTLSInnerPlaintext;
2388 *
2389 * Input:
2390 * - `content`: The beginning of the buffer holding the
2391 * plaintext to be wrapped.
2392 * - `*content_size`: The length of the plaintext in Bytes.
2393 * - `max_len`: The number of Bytes available starting from
2394 * `content`. This must be `>= *content_size`.
2395 * - `rec_type`: The desired record content type.
2396 *
2397 * Output:
2398 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2399 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2400 *
2401 * Returns:
2402 * - `0` on success.
2403 * - A negative error code if `max_len` didn't offer enough space
2404 * for the expansion.
2405 */
2406static int ssl_cid_build_inner_plaintext( unsigned char *content,
2407 size_t *content_size,
2408 size_t remaining,
2409 uint8_t rec_type )
2410{
2411 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002412 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2413 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2414 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002415
2416 /* Write real content type */
2417 if( remaining == 0 )
2418 return( -1 );
2419 content[ len ] = rec_type;
2420 len++;
2421 remaining--;
2422
2423 if( remaining < pad )
2424 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002425 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002426 len += pad;
2427 remaining -= pad;
2428
2429 *content_size = len;
2430 return( 0 );
2431}
2432
Hanno Becker7dc25772019-05-20 15:08:01 +01002433/* This function parses a DTLSInnerPlaintext structure.
2434 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002435static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2436 size_t *content_size,
2437 uint8_t *rec_type )
2438{
2439 size_t remaining = *content_size;
2440
2441 /* Determine length of padding by skipping zeroes from the back. */
2442 do
2443 {
2444 if( remaining == 0 )
2445 return( -1 );
2446 remaining--;
2447 } while( content[ remaining ] == 0 );
2448
2449 *content_size = remaining;
2450 *rec_type = content[ remaining ];
2451
2452 return( 0 );
2453}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002454#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002455
Hanno Becker99abf512019-05-20 14:50:53 +01002456/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002457 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002458static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002459 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002460 mbedtls_record *rec )
2461{
Hanno Becker99abf512019-05-20 14:50:53 +01002462 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002463 *
2464 * additional_data = seq_num + TLSCompressed.type +
2465 * TLSCompressed.version + TLSCompressed.length;
2466 *
Hanno Becker99abf512019-05-20 14:50:53 +01002467 * For the CID extension, this is extended as follows
2468 * (quoting draft-ietf-tls-dtls-connection-id-05,
2469 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002470 *
2471 * additional_data = seq_num + DTLSPlaintext.type +
2472 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002473 * cid +
2474 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002475 * length_of_DTLSInnerPlaintext;
2476 */
2477
Teppo Järvelin91d79382019-10-02 09:09:31 +03002478 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002479 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002480 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002481
Hanno Beckera5a2b082019-05-15 14:03:01 +01002482#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002483 if( rec->cid_len != 0 )
2484 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002485 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002486 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002487 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2488 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002489 *add_data_len = 13 + 1 + rec->cid_len;
2490 }
2491 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002492#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002493 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002494 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002495 *add_data_len = 13;
2496 }
Hanno Becker3307b532017-12-27 21:37:21 +00002497}
2498
Hanno Becker611a83b2018-01-03 14:27:32 +00002499int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2500 mbedtls_ssl_transform *transform,
2501 mbedtls_record *rec,
2502 int (*f_rng)(void *, unsigned char *, size_t),
2503 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002507 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002508 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002509 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002510 size_t post_avail;
2511
2512 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002513#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002514 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002515 ((void) ssl);
2516#endif
2517
2518 /* The PRNG is used for dynamic IV generation that's used
2519 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2520#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2521 ( defined(MBEDTLS_AES_C) || \
2522 defined(MBEDTLS_ARIA_C) || \
2523 defined(MBEDTLS_CAMELLIA_C) ) && \
2524 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2525 ((void) f_rng);
2526 ((void) p_rng);
2527#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Hanno Becker3307b532017-12-27 21:37:21 +00002531 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002532 {
Hanno Becker3307b532017-12-27 21:37:21 +00002533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2535 }
Hanno Becker505089d2019-05-01 09:45:57 +01002536 if( rec == NULL
2537 || rec->buf == NULL
2538 || rec->buf_len < rec->data_offset
2539 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002540#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002541 || rec->cid_len != 0
2542#endif
2543 )
Hanno Becker3307b532017-12-27 21:37:21 +00002544 {
2545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002547 }
2548
Hanno Becker3307b532017-12-27 21:37:21 +00002549 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002550 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002552 data, rec->data_len );
2553
2554 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2555
2556 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2557 {
2558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2559 (unsigned) rec->data_len,
2560 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2562 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002563
Hanno Beckera5a2b082019-05-15 14:03:01 +01002564#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002565 /*
2566 * Add CID information
2567 */
2568 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002569 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2570 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002571 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002572
2573 if( rec->cid_len != 0 )
2574 {
2575 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002576 * Wrap plaintext into DTLSInnerPlaintext structure.
2577 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002578 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002579 * Note that this changes `rec->data_len`, and hence
2580 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002581 */
2582 if( ssl_cid_build_inner_plaintext( data,
2583 &rec->data_len,
2584 post_avail,
2585 rec->type ) != 0 )
2586 {
2587 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2588 }
2589
2590 rec->type = MBEDTLS_SSL_MSG_CID;
2591 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002592#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002593
Hanno Becker92c930f2019-04-29 17:31:37 +01002594 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2595
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002597 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002599#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 if( mode == MBEDTLS_MODE_STREAM ||
2601 ( mode == MBEDTLS_MODE_CBC
2602#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002603 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002604#endif
2605 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 {
Hanno Becker3307b532017-12-27 21:37:21 +00002607 if( post_avail < transform->maclen )
2608 {
2609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2610 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2611 }
2612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002614 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2615 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002616 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002617 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002618 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2619 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002620 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002621 }
2622 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002623#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2625 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002626 if( mbedtls_ssl_ver_geq(
2627 mbedtls_ssl_transform_get_minor_ver( transform ),
2628 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002629 {
Hanno Becker992b6872017-11-09 18:57:39 +00002630 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2631
Hanno Beckere83efe62019-04-29 13:52:53 +01002632 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002633
Hanno Becker3307b532017-12-27 21:37:21 +00002634 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002635 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002636 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2637 data, rec->data_len );
2638 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2639 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2640
Teppo Järvelin91d79382019-10-02 09:09:31 +03002641 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002642 }
2643 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002644#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002648 }
2649
Hanno Becker3307b532017-12-27 21:37:21 +00002650 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2651 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002652
Hanno Becker3307b532017-12-27 21:37:21 +00002653 rec->data_len += transform->maclen;
2654 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002655 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002656 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002657#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002659 /*
2660 * Encrypt
2661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2663 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002665 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002666 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002668 "including %d bytes of padding",
2669 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670
Hanno Becker3307b532017-12-27 21:37:21 +00002671 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2672 transform->iv_enc, transform->ivlen,
2673 data, rec->data_len,
2674 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002677 return( ret );
2678 }
2679
Hanno Becker3307b532017-12-27 21:37:21 +00002680 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
Paul Bakker68884e32013-01-07 18:20:04 +01002686 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002688
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002689#if defined(MBEDTLS_GCM_C) || \
2690 defined(MBEDTLS_CCM_C) || \
2691 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002693 mode == MBEDTLS_MODE_CCM ||
2694 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002695 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002696 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002697 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002698 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002699
Hanno Becker3307b532017-12-27 21:37:21 +00002700 /* Check that there's space for both the authentication tag
2701 * and the explicit IV before and after the record content. */
2702 if( post_avail < transform->taglen ||
2703 rec->data_offset < explicit_iv_len )
2704 {
2705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2706 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2707 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002708
Paul Bakker68884e32013-01-07 18:20:04 +01002709 /*
2710 * Generate IV
2711 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002712 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2713 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002714 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002715 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2716 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002717 explicit_iv_len );
2718 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002719 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002720 }
2721 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2722 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002723 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002724 unsigned char i;
2725
Teppo Järvelin91d79382019-10-02 09:09:31 +03002726 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002727
2728 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002729 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002730 }
2731 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002732 {
2733 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002736 }
2737
Hanno Beckere83efe62019-04-29 13:52:53 +01002738 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002739
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002740 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2741 iv, transform->ivlen );
2742 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002743 data - explicit_iv_len, explicit_iv_len );
2744 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002745 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002747 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002748 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002749
Paul Bakker68884e32013-01-07 18:20:04 +01002750 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002751 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002752 */
Hanno Becker3307b532017-12-27 21:37:21 +00002753
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002754 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002755 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002756 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002757 data, rec->data_len, /* source */
2758 data, &rec->data_len, /* destination */
2759 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002762 return( ret );
2763 }
2764
Hanno Becker3307b532017-12-27 21:37:21 +00002765 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2766 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002767
Hanno Becker3307b532017-12-27 21:37:21 +00002768 rec->data_len += transform->taglen + explicit_iv_len;
2769 rec->data_offset -= explicit_iv_len;
2770 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002771 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002772 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2775#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002776 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002779 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002780 size_t padlen, i;
2781 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002782
Hanno Becker3307b532017-12-27 21:37:21 +00002783 /* Currently we're always using minimal padding
2784 * (up to 255 bytes would be allowed). */
2785 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2786 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 padlen = 0;
2788
Hanno Becker3307b532017-12-27 21:37:21 +00002789 /* Check there's enough space in the buffer for the padding. */
2790 if( post_avail < padlen + 1 )
2791 {
2792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2793 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2794 }
2795
Paul Bakker5121ce52009-01-03 21:22:43 +00002796 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002797 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002798
Hanno Becker3307b532017-12-27 21:37:21 +00002799 rec->data_len += padlen + 1;
2800 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002804 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2805 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002806 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002807 if( mbedtls_ssl_ver_geq(
2808 mbedtls_ssl_transform_get_minor_ver( transform ),
2809 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002810 {
Hanno Becker3307b532017-12-27 21:37:21 +00002811 if( f_rng == NULL )
2812 {
2813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2815 }
2816
2817 if( rec->data_offset < transform->ivlen )
2818 {
2819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2820 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2821 }
2822
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002823 /*
2824 * Generate IV
2825 */
Hanno Becker3307b532017-12-27 21:37:21 +00002826 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002827 if( ret != 0 )
2828 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002829
Teppo Järvelin91d79382019-10-02 09:09:31 +03002830 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002831 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002832
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002837 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002838 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002839 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
Hanno Becker3307b532017-12-27 21:37:21 +00002841 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2842 transform->iv_enc,
2843 transform->ivlen,
2844 data, rec->data_len,
2845 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002848 return( ret );
2849 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002850
Hanno Becker3307b532017-12-27 21:37:21 +00002851 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002855 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002858 if( mbedtls_ssl_ver_lt(
2859 mbedtls_ssl_transform_get_minor_ver( transform ),
2860 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002861 {
2862 /*
2863 * Save IV in SSL3 and TLS1
2864 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002865 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002866 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 }
Hanno Becker3307b532017-12-27 21:37:21 +00002868 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002869#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002870 {
2871 data -= transform->ivlen;
2872 rec->data_offset -= transform->ivlen;
2873 rec->data_len += transform->ivlen;
2874 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002877 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002878 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002879 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2880
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002881 /*
2882 * MAC(MAC_write_key, seq_num +
2883 * TLSCipherText.type +
2884 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002885 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002886 * IV + // except for TLS 1.0
2887 * ENC(content + padding + padding_length));
2888 */
Hanno Becker3307b532017-12-27 21:37:21 +00002889
2890 if( post_avail < transform->maclen)
2891 {
2892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2893 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2894 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002895
Hanno Beckere83efe62019-04-29 13:52:53 +01002896 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002899 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002900 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002901
Hanno Becker3307b532017-12-27 21:37:21 +00002902 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002903 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002904 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2905 data, rec->data_len );
2906 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2907 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002908
Teppo Järvelin91d79382019-10-02 09:09:31 +03002909 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002910
Hanno Becker3307b532017-12-27 21:37:21 +00002911 rec->data_len += transform->maclen;
2912 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002913 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002914 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002917 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002919 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002923 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002925 /* Make extra sure authentication was performed, exactly once */
2926 if( auth_done != 1 )
2927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002930 }
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
2934 return( 0 );
2935}
2936
Hanno Becker40478be2019-07-12 08:23:59 +01002937int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002938 mbedtls_ssl_transform *transform,
2939 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002940{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002941 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002943 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002944#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002945 size_t padlen = 0, correct = 1;
2946#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002947 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002948 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002949 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002950
Hanno Becker611a83b2018-01-03 14:27:32 +00002951#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002952 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002953 ((void) ssl);
2954#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002957 if( rec == NULL ||
2958 rec->buf == NULL ||
2959 rec->buf_len < rec->data_offset ||
2960 rec->buf_len - rec->data_offset < rec->data_len )
2961 {
2962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002964 }
2965
Hanno Becker4c6876b2017-12-27 21:28:58 +00002966 data = rec->buf + rec->data_offset;
2967 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Hanno Beckera5a2b082019-05-15 14:03:01 +01002969#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002970 /*
2971 * Match record's CID with incoming CID.
2972 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002973 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002974 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002975 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002976 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002977 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002978#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2981 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002982 {
2983 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002984 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2985 transform->iv_dec,
2986 transform->ivlen,
2987 data, rec->data_len,
2988 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002991 return( ret );
2992 }
2993
Hanno Becker4c6876b2017-12-27 21:28:58 +00002994 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002998 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002999 }
Paul Bakker68884e32013-01-07 18:20:04 +01003000 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003001#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003002#if defined(MBEDTLS_GCM_C) || \
3003 defined(MBEDTLS_CCM_C) || \
3004 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003005 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003006 mode == MBEDTLS_MODE_CCM ||
3007 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003008 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003009 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003010 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003011
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003012 /*
3013 * Prepare IV from explicit and implicit data.
3014 */
3015
3016 /* Check that there's enough space for the explicit IV
3017 * (at the beginning of the record) and the MAC (at the
3018 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003019 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003022 "+ taglen (%d)", rec->data_len,
3023 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003024 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003025 }
Paul Bakker68884e32013-01-07 18:20:04 +01003026
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003027#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003028 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3029 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003030 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003031
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003032 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003033 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003034 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003035 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003036 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003037 else
3038#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3039#if defined(MBEDTLS_CHACHAPOLY_C)
3040 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003041 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003042 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003043 unsigned char i;
3044
Teppo Järvelin91d79382019-10-02 09:09:31 +03003045 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003046
3047 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003048 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003049 }
3050 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003051#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003052 {
3053 /* Reminder if we ever add an AEAD mode with a different size */
3054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3056 }
3057
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003058 /* Group changes to data, data_len, and add_data, because
3059 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003060 data += explicit_iv_len;
3061 rec->data_offset += explicit_iv_len;
3062 rec->data_len -= explicit_iv_len + transform->taglen;
3063
Hanno Beckere83efe62019-04-29 13:52:53 +01003064 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003065 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003066 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003067
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003068 /* Because of the check above, we know that there are
3069 * explicit_iv_len Bytes preceeding data, and taglen
3070 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003071 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003072 * mbedtls_cipher_auth_decrypt() below. */
3073
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003074 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003075 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003076 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003077
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003078 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003079 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003080 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003081 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3082 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003083 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003084 data, rec->data_len,
3085 data, &olen,
3086 data + rec->data_len,
3087 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3092 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003093
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003094 return( ret );
3095 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003096 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003097
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003098 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003099 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003103 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003104 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3107#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003108 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003110 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003111 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003112
Paul Bakker5121ce52009-01-03 21:22:43 +00003113 /*
Paul Bakker45829992013-01-03 14:52:21 +01003114 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003117 if( mbedtls_ssl_ver_geq(
3118 mbedtls_ssl_transform_get_minor_ver( transform ),
3119 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003120 {
3121 /* The ciphertext is prefixed with the CBC IV. */
3122 minlen += transform->ivlen;
3123 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003124#endif
Paul Bakker45829992013-01-03 14:52:21 +01003125
Hanno Becker4c6876b2017-12-27 21:28:58 +00003126 /* Size considerations:
3127 *
3128 * - The CBC cipher text must not be empty and hence
3129 * at least of size transform->ivlen.
3130 *
3131 * Together with the potential IV-prefix, this explains
3132 * the first of the two checks below.
3133 *
3134 * - The record must contain a MAC, either in plain or
3135 * encrypted, depending on whether Encrypt-then-MAC
3136 * is used or not.
3137 * - If it is, the message contains the IV-prefix,
3138 * the CBC ciphertext, and the MAC.
3139 * - If it is not, the padded plaintext, and hence
3140 * the CBC ciphertext, has at least length maclen + 1
3141 * because there is at least the padding length byte.
3142 *
3143 * As the CBC ciphertext is not empty, both cases give the
3144 * lower bound minlen + maclen + 1 on the record size, which
3145 * we test for in the second check below.
3146 */
3147 if( rec->data_len < minlen + transform->ivlen ||
3148 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003151 "+ 1 ) ( + expl IV )", rec->data_len,
3152 transform->ivlen,
3153 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003155 }
3156
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003157 /*
3158 * Authenticate before decrypt if enabled
3159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003161 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003162 {
Hanno Becker992b6872017-11-09 18:57:39 +00003163 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003166
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003167 /* Update data_len in tandem with add_data.
3168 *
3169 * The subtraction is safe because of the previous check
3170 * data_len >= minlen + maclen + 1.
3171 *
3172 * Afterwards, we know that data + data_len is followed by at
3173 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003174 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003175 *
3176 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003177 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003178 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003179
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003180 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003181 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3182 add_data_len );
3183 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3184 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003185 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3186 data, rec->data_len );
3187 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3188 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003189
Hanno Becker4c6876b2017-12-27 21:28:58 +00003190 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3191 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003192 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003193 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003194
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003195 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003196 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003197 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003201 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003202 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003203 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003205
3206 /*
3207 * Check length sanity
3208 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003209
3210 /* We know from above that data_len > minlen >= 0,
3211 * so the following check in particular implies that
3212 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003213 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003216 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003218 }
3219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003221 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003222 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003223 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003224 if( mbedtls_ssl_ver_geq(
3225 mbedtls_ssl_transform_get_minor_ver( transform ),
3226 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003227 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003228 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003229 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003230
Hanno Becker4c6876b2017-12-27 21:28:58 +00003231 data += transform->ivlen;
3232 rec->data_offset += transform->ivlen;
3233 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003234 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003236
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003237 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3238
Hanno Becker4c6876b2017-12-27 21:28:58 +00003239 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3240 transform->iv_dec, transform->ivlen,
3241 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003244 return( ret );
3245 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003246
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003247 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003248 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003252 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003255 if( mbedtls_ssl_ver_lt(
3256 mbedtls_ssl_transform_get_minor_ver( transform ),
3257 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003258 {
3259 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003260 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3261 * records is equivalent to CBC decryption of the concatenation
3262 * of the records; in other words, IVs are maintained across
3263 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003264 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003265 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003266 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003268#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003269
Hanno Becker4c6876b2017-12-27 21:28:58 +00003270 /* Safe since data_len >= minlen + maclen + 1, so after having
3271 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003272 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3273 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003274 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003275
Hanno Becker4c6876b2017-12-27 21:28:58 +00003276 if( auth_done == 1 )
3277 {
3278 correct *= ( rec->data_len >= padlen + 1 );
3279 padlen *= ( rec->data_len >= padlen + 1 );
3280 }
3281 else
Paul Bakker45829992013-01-03 14:52:21 +01003282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003284 if( rec->data_len < transform->maclen + padlen + 1 )
3285 {
3286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3287 rec->data_len,
3288 transform->maclen,
3289 padlen + 1 ) );
3290 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003291#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003292
3293 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3294 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Hanno Becker4c6876b2017-12-27 21:28:58 +00003297 padlen++;
3298
3299 /* Regardless of the validity of the padding,
3300 * we have data_len >= padlen here. */
3301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003303 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3304 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003306 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#if defined(MBEDTLS_SSL_DEBUG_ALL)
3309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003310 "should be no more than %d",
3311 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003312#endif
Paul Bakker45829992013-01-03 14:52:21 +01003313 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 }
3315 }
3316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3318#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3319 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003320 if( mbedtls_ssl_ver_gt(
3321 mbedtls_ssl_transform_get_minor_ver( transform ),
3322 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003324 /* The padding check involves a series of up to 256
3325 * consecutive memory reads at the end of the record
3326 * plaintext buffer. In order to hide the length and
3327 * validity of the padding, always perform exactly
3328 * `min(256,plaintext_len)` reads (but take into account
3329 * only the last `padlen` bytes for the padding check). */
3330 size_t pad_count = 0;
3331 size_t real_count = 0;
3332 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003333
Hanno Becker4c6876b2017-12-27 21:28:58 +00003334 /* Index of first padding byte; it has been ensured above
3335 * that the subtraction is safe. */
3336 size_t const padding_idx = rec->data_len - padlen;
3337 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3338 size_t const start_idx = rec->data_len - num_checks;
3339 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003340
Hanno Becker4c6876b2017-12-27 21:28:58 +00003341 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003342 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003343 real_count |= ( idx >= padding_idx );
3344 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003345 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003346 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003349 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003351#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003352 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3356 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003360 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003361
Hanno Becker4c6876b2017-12-27 21:28:58 +00003362 /* If the padding was found to be invalid, padlen == 0
3363 * and the subtraction is safe. If the padding was found valid,
3364 * padlen hasn't been changed and the previous assertion
3365 * data_len >= padlen still holds. */
3366 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003367 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003368 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003370 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003374 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003376#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003378 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
3381 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003382 * Authenticate if not done yet.
3383 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003384 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003385#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003386 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003387 {
Hanno Becker992b6872017-11-09 18:57:39 +00003388 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003389
Hanno Becker4c6876b2017-12-27 21:28:58 +00003390 /* If the initial value of padlen was such that
3391 * data_len < maclen + padlen + 1, then padlen
3392 * got reset to 1, and the initial check
3393 * data_len >= minlen + maclen + 1
3394 * guarantees that at this point we still
3395 * have at least data_len >= maclen.
3396 *
3397 * If the initial value of padlen was such that
3398 * data_len >= maclen + padlen + 1, then we have
3399 * subtracted either padlen + 1 (if the padding was correct)
3400 * or 0 (if the padding was incorrect) since then,
3401 * hence data_len >= maclen in any case.
3402 */
3403 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003404 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003407 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3408 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003409 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003410 ssl_mac( &transform->md_ctx_dec,
3411 transform->mac_dec,
3412 data, rec->data_len,
3413 rec->ctr, rec->type,
3414 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003415 }
3416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3419 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003420 if( mbedtls_ssl_ver_gt(
3421 mbedtls_ssl_transform_get_minor_ver( transform ),
3422 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003423 {
3424 /*
3425 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003426 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003427 *
3428 * Known timing attacks:
3429 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3430 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003431 * To compensate for different timings for the MAC calculation
3432 * depending on how much padding was removed (which is determined
3433 * by padlen), process extra_run more blocks through the hash
3434 * function.
3435 *
3436 * The formula in the paper is
3437 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3438 * where L1 is the size of the header plus the decrypted message
3439 * plus CBC padding and L2 is the size of the header plus the
3440 * decrypted message. This is for an underlying hash function
3441 * with 64-byte blocks.
3442 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3443 * correctly. We round down instead of up, so -56 is the correct
3444 * value for our calculations instead of -55.
3445 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003446 * Repeat the formula rather than defining a block_size variable.
3447 * This avoids requiring division by a variable at runtime
3448 * (which would be marginally less efficient and would require
3449 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003450 */
3451 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003452 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003453
3454 /*
3455 * The next two sizes are the minimum and maximum values of
3456 * in_msglen over all padlen values.
3457 *
3458 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003459 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003460 *
3461 * Note that max_len + maclen is never more than the buffer
3462 * length, as we previously did in_msglen -= maclen too.
3463 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003464 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003465 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3466
Hanno Becker4c6876b2017-12-27 21:28:58 +00003467 memset( tmp, 0, sizeof( tmp ) );
3468
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003469 switch( mbedtls_md_get_type(
3470 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003471 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003472#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3473 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003474 case MBEDTLS_MD_MD5:
3475 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003476 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003477 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003478 extra_run =
3479 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3480 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003481 break;
3482#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003483#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003484 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003485 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003486 extra_run =
3487 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3488 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003489 break;
3490#endif
3491 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003493 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3494 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003495
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003496 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003497
Hanno Beckere83efe62019-04-29 13:52:53 +01003498 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3499 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003500 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3501 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003502 /* Make sure we access everything even when padlen > 0. This
3503 * makes the synchronisation requirements for just-in-time
3504 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003505 ssl_read_memory( data + rec->data_len, padlen );
3506 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003507
3508 /* Call mbedtls_md_process at least once due to cache attacks
3509 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003510 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003511 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003512
Hanno Becker4c6876b2017-12-27 21:28:58 +00003513 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003514
3515 /* Make sure we access all the memory that could contain the MAC,
3516 * before we check it in the next code block. This makes the
3517 * synchronisation requirements for just-in-time Prime+Probe
3518 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003519 ssl_read_memory( data + min_len,
3520 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003521 }
3522 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3524 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003530#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003531 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3532 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003533#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003535 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003536 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538#if defined(MBEDTLS_SSL_DEBUG_ALL)
3539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003540#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003541 correct = 0;
3542 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003543 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003544 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003545
3546 /*
3547 * Finally check the correct flag
3548 */
3549 if( correct == 0 )
3550 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003551#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003552
3553 /* Make extra sure authentication was performed, exactly once */
3554 if( auth_done != 1 )
3555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003558 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003559
Hanno Beckera5a2b082019-05-15 14:03:01 +01003560#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003561 if( rec->cid_len != 0 )
3562 {
3563 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3564 &rec->type );
3565 if( ret != 0 )
3566 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3567 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003568#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003571
3572 return( 0 );
3573}
3574
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003575#undef MAC_NONE
3576#undef MAC_PLAINTEXT
3577#undef MAC_CIPHERTEXT
3578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003580/*
3581 * Compression/decompression functions
3582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003583static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003584{
3585 int ret;
3586 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003587 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003588 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003589 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003592
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003593 if( len_pre == 0 )
3594 return( 0 );
3595
Teppo Järvelin91d79382019-10-02 09:09:31 +03003596 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003599 ssl->out_msglen ) );
3600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003602 ssl->out_msg, ssl->out_msglen );
3603
Paul Bakker48916f92012-09-16 19:57:18 +00003604 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3605 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3606 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003607 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003608
Paul Bakker48916f92012-09-16 19:57:18 +00003609 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003610 if( ret != Z_OK )
3611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3613 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003614 }
3615
Angus Grattond8213d02016-05-25 20:56:48 +10003616 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003617 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003620 ssl->out_msglen ) );
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003623 ssl->out_msg, ssl->out_msglen );
3624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003626
3627 return( 0 );
3628}
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003631{
3632 int ret;
3633 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003634 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003635 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003636 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003639
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003640 if( len_pre == 0 )
3641 return( 0 );
3642
Teppo Järvelin91d79382019-10-02 09:09:31 +03003643 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003646 ssl->in_msglen ) );
3647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003649 ssl->in_msg, ssl->in_msglen );
3650
Paul Bakker48916f92012-09-16 19:57:18 +00003651 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3652 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3653 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003654 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003655 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656
Paul Bakker48916f92012-09-16 19:57:18 +00003657 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003658 if( ret != Z_OK )
3659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3661 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003662 }
3663
Angus Grattond8213d02016-05-25 20:56:48 +10003664 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003665 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003668 ssl->in_msglen ) );
3669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003671 ssl->in_msg, ssl->in_msglen );
3672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003674
3675 return( 0 );
3676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3680static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682#if defined(MBEDTLS_SSL_PROTO_DTLS)
3683static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003684{
3685 /* If renegotiation is not enforced, retransmit until we would reach max
3686 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003687 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003688 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003689 uint32_t ratio =
3690 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3691 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003692 unsigned char doublings = 1;
3693
3694 while( ratio != 0 )
3695 {
3696 ++doublings;
3697 ratio >>= 1;
3698 }
3699
3700 if( ++ssl->renego_records_seen > doublings )
3701 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003703 return( 0 );
3704 }
3705 }
3706
3707 return( ssl_write_hello_request( ssl ) );
3708}
3709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003710#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003711
Paul Bakker5121ce52009-01-03 21:22:43 +00003712/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003713 * Fill the input message buffer by appending data to it.
3714 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003715 *
3716 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3717 * available (from this read and/or a previous one). Otherwise, an error code
3718 * is returned (possibly EOF or WANT_READ).
3719 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003720 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3721 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3722 * since we always read a whole datagram at once.
3723 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003724 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003725 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003728{
Paul Bakker23986e52011-04-24 08:57:21 +00003729 int ret;
3730 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733
Hanno Beckera58a8962019-06-13 16:11:15 +01003734 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3735 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003738 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003740 }
3741
Angus Grattond8213d02016-05-25 20:56:48 +10003742 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003746 }
3747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003749 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003750 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003751 uint32_t timeout;
3752
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003753 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003754 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3755 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003756 {
3757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3758 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3760 }
3761
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003762 /*
3763 * The point is, we need to always read a full datagram at once, so we
3764 * sometimes read more then requested, and handle the additional data.
3765 * It could be the rest of the current record (while fetching the
3766 * header) and/or some other records in the same datagram.
3767 */
3768
3769 /*
3770 * Move to the next record in the already read datagram if applicable
3771 */
3772 if( ssl->next_record_offset != 0 )
3773 {
3774 if( ssl->in_left < ssl->next_record_offset )
3775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003778 }
3779
3780 ssl->in_left -= ssl->next_record_offset;
3781
3782 if( ssl->in_left != 0 )
3783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003785 ssl->next_record_offset ) );
3786 memmove( ssl->in_hdr,
3787 ssl->in_hdr + ssl->next_record_offset,
3788 ssl->in_left );
3789 }
3790
3791 ssl->next_record_offset = 0;
3792 }
3793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003796
3797 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003798 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003799 */
3800 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003803 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003804 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003805
3806 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003807 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003808 * are not at the beginning of a new record, the caller did something
3809 * wrong.
3810 */
3811 if( ssl->in_left != 0 )
3812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003815 }
3816
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003817 /*
3818 * Don't even try to read if time's out already.
3819 * This avoids by-passing the timer when repeatedly receiving messages
3820 * that will end up being dropped.
3821 */
3822 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003823 {
3824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003825 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003826 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003827 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003828 {
Angus Grattond8213d02016-05-25 20:56:48 +10003829 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003832 timeout = ssl->handshake->retransmit_timeout;
3833 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003834 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003837
Hanno Beckera58a8962019-06-13 16:11:15 +01003838 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3839 {
3840 ret = mbedtls_ssl_get_recv_timeout( ssl )
3841 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3842 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003843 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003844 {
3845 ret = mbedtls_ssl_get_recv( ssl )
3846 ( ssl->p_bio, ssl->in_hdr, len );
3847 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003850
3851 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003853 }
3854
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003855 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003858 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003861 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003862 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003865 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003866 }
3867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003871 return( ret );
3872 }
3873
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003874 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003875 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003877 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3878 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003880 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003881 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003884 return( ret );
3885 }
3886
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003887 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003888 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003890 }
3891
Paul Bakker5121ce52009-01-03 21:22:43 +00003892 if( ret < 0 )
3893 return( ret );
3894
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003895 ssl->in_left = ret;
3896 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003897 MBEDTLS_SSL_TRANSPORT_ELSE
3898#endif /* MBEDTLS_SSL_PROTO_DTLS */
3899#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003902 ssl->in_left, nb_want ) );
3903
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003904 while( ssl->in_left < nb_want )
3905 {
3906 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003907
3908 if( ssl_check_timer( ssl ) != 0 )
3909 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3910 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003911 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003912 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003913 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003914 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3915 ssl->in_hdr + ssl->in_left, len,
3916 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003917 }
3918 else
3919 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003920 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003921 ssl->in_hdr + ssl->in_left, len );
3922 }
3923 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003926 ssl->in_left, nb_want ) );
3927 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003928
3929 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003931
3932 if( ret < 0 )
3933 return( ret );
3934
mohammad160352aecb92018-03-28 23:41:40 -07003935 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003936 {
Darryl Green11999bb2018-03-13 15:22:58 +00003937 MBEDTLS_SSL_DEBUG_MSG( 1,
3938 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003939 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003940 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3941 }
3942
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003943 ssl->in_left += ret;
3944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003945 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003946#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003949
3950 return( 0 );
3951}
3952
3953/*
3954 * Flush any data not yet written
3955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003957{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003958 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003959 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003962
Hanno Beckera58a8962019-06-13 16:11:15 +01003963 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003966 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003968 }
3969
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003970 /* Avoid incrementing counter if data is flushed */
3971 if( ssl->out_left == 0 )
3972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003974 return( 0 );
3975 }
3976
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 while( ssl->out_left > 0 )
3978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003980 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003981
Hanno Becker2b1e3542018-08-06 11:19:13 +01003982 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003983 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003986
3987 if( ret <= 0 )
3988 return( ret );
3989
mohammad160352aecb92018-03-28 23:41:40 -07003990 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003991 {
Darryl Green11999bb2018-03-13 15:22:58 +00003992 MBEDTLS_SSL_DEBUG_MSG( 1,
3993 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003994 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3996 }
3997
Paul Bakker5121ce52009-01-03 21:22:43 +00003998 ssl->out_left -= ret;
3999 }
4000
Hanno Becker2b1e3542018-08-06 11:19:13 +01004001#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004002 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004003 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004004 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004005 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004006 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01004007#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004008#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01004009 {
4010 ssl->out_hdr = ssl->out_buf + 8;
4011 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004012#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004013 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004016
4017 return( 0 );
4018}
4019
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004020/*
4021 * Functions to handle the DTLS retransmission state machine
4022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004024/*
4025 * Append current handshake message to current outgoing flight
4026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004027static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4031 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4032 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033
4034 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004035 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004036 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004039 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004040 }
4041
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004042 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004043 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004046 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004047 }
4048
4049 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004050 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004051 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004052 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004053 msg->next = NULL;
4054
4055 /* Append to the current flight */
4056 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004057 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004058 else
4059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004061 while( cur->next != NULL )
4062 cur = cur->next;
4063 cur->next = msg;
4064 }
4065
Hanno Becker3b235902018-08-06 09:54:53 +01004066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004067 return( 0 );
4068}
4069
4070/*
4071 * Free the current flight of handshake messages
4072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004073static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 mbedtls_ssl_flight_item *cur = flight;
4076 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004077
4078 while( cur != NULL )
4079 {
4080 next = cur->next;
4081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082 mbedtls_free( cur->p );
4083 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004084
4085 cur = next;
4086 }
4087}
4088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4090static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004091#endif
4092
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004093/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004094 * Swap transform_out and out_ctr with the alternative ones
4095 */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004096static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004099 unsigned char tmp_out_ctr[8];
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004100#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4101 int ret;
4102#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004103
4104 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004107 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004108 }
4109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004111
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004112 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004113 tmp_transform = ssl->transform_out;
4114 ssl->transform_out = ssl->handshake->alt_transform_out;
4115 ssl->handshake->alt_transform_out = tmp_transform;
4116
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004117 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004118 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4119 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4120 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004121
4122 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004123 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4126 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4131 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004132 }
4133 }
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004134#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
4135
4136 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004137}
4138
4139/*
4140 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004141 */
4142int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4143{
4144 int ret = 0;
4145
4146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4147
4148 ret = mbedtls_ssl_flight_transmit( ssl );
4149
4150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4151
4152 return( ret );
4153}
4154
4155/*
4156 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004157 *
4158 * Need to remember the current message in case flush_output returns
4159 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004160 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004161 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004162int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004163{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004164 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004168 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004170
4171 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004172 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004173 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4174 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004177 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004178
4179 while( ssl->handshake->cur_msg != NULL )
4180 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004181 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004182 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004183
Hanno Beckere1dcb032018-08-17 16:47:58 +01004184 int const is_finished =
4185 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4186 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4187
Hanno Becker04da1892018-08-14 13:22:10 +01004188 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4189 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4190
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004191 /* Swap epochs before sending Finished: we can't do it after
4192 * sending ChangeCipherSpec, in case write returns WANT_READ.
4193 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004194 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004195 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004197 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4198 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004199 }
4200
Hanno Becker67bc7c32018-08-06 11:33:50 +01004201 ret = ssl_get_remaining_payload_in_datagram( ssl );
4202 if( ret < 0 )
4203 return( ret );
4204 max_frag_len = (size_t) ret;
4205
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004206 /* CCS is copied as is, while HS messages may need fragmentation */
4207 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4208 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004209 if( max_frag_len == 0 )
4210 {
4211 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4212 return( ret );
4213
4214 continue;
4215 }
4216
Teppo Järvelin91d79382019-10-02 09:09:31 +03004217 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004218 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004219 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004220
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004221 /* Update position inside current message */
4222 ssl->handshake->cur_msg_p += cur->len;
4223 }
4224 else
4225 {
4226 const unsigned char * const p = ssl->handshake->cur_msg_p;
4227 const size_t hs_len = cur->len - 12;
4228 const size_t frag_off = p - ( cur->p + 12 );
4229 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004230 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004231
Hanno Beckere1dcb032018-08-17 16:47:58 +01004232 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004233 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004234 if( is_finished )
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004235 {
4236 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4237 return( ret );
4238 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004239
Hanno Becker67bc7c32018-08-06 11:33:50 +01004240 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4241 return( ret );
4242
4243 continue;
4244 }
4245 max_hs_frag_len = max_frag_len - 12;
4246
4247 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4248 max_hs_frag_len : rem_len;
4249
4250 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004251 {
4252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004253 (unsigned) cur_hs_frag_len,
4254 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004255 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004256
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004257 /* Messages are stored with handshake headers as if not fragmented,
4258 * copy beginning of headers then fill fragmentation fields.
4259 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004260 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004261
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004262 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4263 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4264 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004265
4266 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4267
Hanno Becker3f7b9732018-08-28 09:53:25 +01004268 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004269 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004270 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004271 ssl->out_msgtype = cur->type;
4272
4273 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004274 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004275 }
4276
4277 /* If done with the current message move to the next one if any */
4278 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4279 {
4280 if( cur->next != NULL )
4281 {
4282 ssl->handshake->cur_msg = cur->next;
4283 ssl->handshake->cur_msg_p = cur->next->p + 12;
4284 }
4285 else
4286 {
4287 ssl->handshake->cur_msg = NULL;
4288 ssl->handshake->cur_msg_p = NULL;
4289 }
4290 }
4291
4292 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004293 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004295 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004296 return( ret );
4297 }
4298 }
4299
Hanno Becker67bc7c32018-08-06 11:33:50 +01004300 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4301 return( ret );
4302
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004303 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4305 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004306 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004308 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004309 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4310 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004311
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004313
4314 return( 0 );
4315}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004316
4317/*
4318 * To be called when the last message of an incoming flight is received.
4319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004321{
4322 /* We won't need to resend that one any more */
4323 ssl_flight_free( ssl->handshake->flight );
4324 ssl->handshake->flight = NULL;
4325 ssl->handshake->cur_msg = NULL;
4326
4327 /* The next incoming flight will start with this msg_seq */
4328 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4329
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004330 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004331 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004332
Hanno Becker0271f962018-08-16 13:23:47 +01004333 /* Clear future message buffering structure. */
4334 ssl_buffering_free( ssl );
4335
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004336 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004337 ssl_set_timer( ssl, 0 );
4338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4340 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004342 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004343 }
4344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004346}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004347
4348/*
4349 * To be called when the last message of an outgoing flight is send.
4350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004351void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004352{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004353 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004354 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4357 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004360 }
4361 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004363}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004365
Paul Bakker5121ce52009-01-03 21:22:43 +00004366/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004367 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004369
4370/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004371 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004372 *
4373 * - fill in handshake headers
4374 * - update handshake checksum
4375 * - DTLS: save message for resending
4376 * - then pass to the record layer
4377 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004378 * DTLS: except for HelloRequest, messages are only queued, and will only be
4379 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004380 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004381 * Inputs:
4382 * - ssl->out_msglen: 4 + actual handshake message len
4383 * (4 is the size of handshake headers for TLS)
4384 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4385 * - ssl->out_msg + 4: the handshake message body
4386 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004387 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004388 * - ssl->out_msglen: the length of the record contents
4389 * (including handshake headers but excluding record headers)
4390 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004391 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004392int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004393{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004394 int ret;
4395 const size_t hs_len = ssl->out_msglen - 4;
4396 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004397
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4399
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004400 /*
4401 * Sanity checks
4402 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004403 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004404 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4405 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004406 /* In SSLv3, the client might send a NoCertificate alert. */
4407#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004408 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004409 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004410 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4411 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004412#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4413 {
4414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4416 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004418
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004419 /* Whenever we send anything different from a
4420 * HelloRequest we should be in a handshake - double check. */
4421 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4422 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004423 ssl->handshake == NULL )
4424 {
4425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4426 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004430 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004431 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004433 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4435 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004436 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004437#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004438
Hanno Beckerb50a2532018-08-06 11:52:54 +01004439 /* Double-check that we did not exceed the bounds
4440 * of the outgoing record buffer.
4441 * This should never fail as the various message
4442 * writing functions must obey the bounds of the
4443 * outgoing record buffer, but better be safe.
4444 *
4445 * Note: We deliberately do not check for the MTU or MFL here.
4446 */
4447 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4448 {
4449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4450 "size %u, maximum %u",
4451 (unsigned) ssl->out_msglen,
4452 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4454 }
4455
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004456 /*
4457 * Fill handshake headers
4458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004461 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004462
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004463 /*
4464 * DTLS has additional fields in the Handshake layer,
4465 * between the length field and the actual payload:
4466 * uint16 message_seq;
4467 * uint24 fragment_offset;
4468 * uint24 fragment_length;
4469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004471 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004472 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004473 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004474 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004475 {
4476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4477 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004478 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004479 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4481 }
4482
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004483 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004484 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004485
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004486 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004487 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004488 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004489 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4490 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004491 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004492 }
4493 else
4494 {
4495 ssl->out_msg[4] = 0;
4496 ssl->out_msg[5] = 0;
4497 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004498
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004499 /* Handshake hashes are computed without fragmentation,
4500 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004501 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004502 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004505
Hanno Becker0207e532018-08-28 10:28:28 +01004506 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004507 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004508 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004509 }
4510
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004511 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004513 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004514 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4515 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004516 {
4517 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004519 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004520 return( ret );
4521 }
4522 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004523 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004524#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004525 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004526 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004527 {
4528 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4529 return( ret );
4530 }
4531 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004532
4533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4534
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004535 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004536}
4537
4538/*
4539 * Record layer functions
4540 */
4541
4542/*
4543 * Write current record.
4544 *
4545 * Uses:
4546 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4547 * - ssl->out_msglen: length of the record content (excl headers)
4548 * - ssl->out_msg: record content
4549 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004550int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004551{
4552 int ret, done = 0;
4553 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004554 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004555
4556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004559 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004560 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004561 {
4562 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004565 return( ret );
4566 }
4567
4568 len = ssl->out_msglen;
4569 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4573 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 ret = mbedtls_ssl_hw_record_write( ssl );
4578 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4581 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004582 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004583
4584 if( ret == 0 )
4585 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004588 if( !done )
4589 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004590 unsigned i;
4591 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004592 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004593
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004594 /* Skip writing the record content type to after the encryption,
4595 * as it may change when using the CID extension. */
4596
Hanno Becker2881d802019-05-22 14:44:53 +01004597 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4598 mbedtls_ssl_get_minor_ver( ssl ),
4599 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004600
Teppo Järvelin91d79382019-10-02 09:09:31 +03004601 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004602 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004603
Paul Bakker48916f92012-09-16 19:57:18 +00004604 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004605 {
Hanno Becker3307b532017-12-27 21:37:21 +00004606 mbedtls_record rec;
4607
4608 rec.buf = ssl->out_iv;
4609 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4610 ( ssl->out_iv - ssl->out_buf );
4611 rec.data_len = ssl->out_msglen;
4612 rec.data_offset = ssl->out_msg - rec.buf;
4613
Teppo Järvelin91d79382019-10-02 09:09:31 +03004614 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004615 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4616 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004617 ssl->conf->transport, rec.ver );
4618 rec.type = ssl->out_msgtype;
4619
Hanno Beckera5a2b082019-05-15 14:03:01 +01004620#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004621 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004622 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004623#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004624
Hanno Becker611a83b2018-01-03 14:27:32 +00004625 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004626 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004627 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004630 return( ret );
4631 }
4632
Hanno Becker3307b532017-12-27 21:37:21 +00004633 if( rec.data_offset != 0 )
4634 {
4635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4636 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4637 }
4638
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004639 /* Update the record content type and CID. */
4640 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004641#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004642 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4643 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004644#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004645 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004646 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004647 encrypted_fi = 1;
4648 }
4649
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004650 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004651 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4652 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004653 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004654 }
4655
Hanno Becker43395762019-05-03 14:46:38 +01004656 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004657
4658#if defined(MBEDTLS_SSL_PROTO_DTLS)
4659 /* In case of DTLS, double-check that we don't exceed
4660 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004661 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004662 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004663 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004664 if( ret < 0 )
4665 return( ret );
4666
4667 if( protected_record_size > (size_t) ret )
4668 {
4669 /* Should never happen */
4670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4671 }
4672 }
4673#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004674
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004675 /* Now write the potentially updated record content type. */
4676 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004679 "version = [%d:%d], msglen = %d",
4680 ssl->out_hdr[0], ssl->out_hdr[1],
4681 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004684 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004685
4686 ssl->out_left += protected_record_size;
4687 ssl->out_hdr += protected_record_size;
4688 ssl_update_out_pointers( ssl, ssl->transform_out );
4689
Hanno Becker04484622018-08-06 09:49:38 +01004690 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4691 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4692 break;
4693
4694 /* The loop goes to its end iff the counter is wrapping */
4695 if( i == ssl_ep_len( ssl ) )
4696 {
4697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4698 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4699 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004700 }
4701
Hanno Becker67bc7c32018-08-06 11:33:50 +01004702#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004703 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004704 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004705 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004706 size_t remaining;
4707 ret = ssl_get_remaining_payload_in_datagram( ssl );
4708 if( ret < 0 )
4709 {
4710 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4711 ret );
4712 return( ret );
4713 }
4714
4715 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004716 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004717 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004718 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004719 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004720 else
4721 {
Hanno Becker513815a2018-08-20 11:56:09 +01004722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004723 }
4724 }
4725#endif /* MBEDTLS_SSL_PROTO_DTLS */
4726
4727 if( ( flush == SSL_FORCE_FLUSH ) &&
4728 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004731 return( ret );
4732 }
4733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004735
4736 return( 0 );
4737}
4738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004740
4741static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4742{
4743 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004744 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4745 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004746 {
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04004747 return( PROPER_HS_FRAGMENT );
Hanno Beckere25e3b72018-08-16 09:30:53 +01004748 }
4749 return( 0 );
4750}
Hanno Becker44650b72018-08-16 12:51:11 +01004751
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004752static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004753{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004754 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004755}
4756
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004757static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004758{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004759 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004760}
4761
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004762static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004763{
4764 uint32_t msg_len, frag_off, frag_len;
4765
4766 msg_len = ssl_get_hs_total_len( ssl );
4767 frag_off = ssl_get_hs_frag_off( ssl );
4768 frag_len = ssl_get_hs_frag_len( ssl );
4769
4770 if( frag_off > msg_len )
4771 return( -1 );
4772
4773 if( frag_len > msg_len - frag_off )
4774 return( -1 );
4775
4776 if( frag_len + 12 > ssl->in_msglen )
4777 return( -1 );
4778
4779 return( 0 );
4780}
4781
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004782/*
4783 * Mark bits in bitmask (used for DTLS HS reassembly)
4784 */
4785static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4786{
4787 unsigned int start_bits, end_bits;
4788
4789 start_bits = 8 - ( offset % 8 );
4790 if( start_bits != 8 )
4791 {
4792 size_t first_byte_idx = offset / 8;
4793
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004794 /* Special case */
4795 if( len <= start_bits )
4796 {
4797 for( ; len != 0; len-- )
4798 mask[first_byte_idx] |= 1 << ( start_bits - len );
4799
4800 /* Avoid potential issues with offset or len becoming invalid */
4801 return;
4802 }
4803
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004804 offset += start_bits; /* Now offset % 8 == 0 */
4805 len -= start_bits;
4806
4807 for( ; start_bits != 0; start_bits-- )
4808 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4809 }
4810
4811 end_bits = len % 8;
4812 if( end_bits != 0 )
4813 {
4814 size_t last_byte_idx = ( offset + len ) / 8;
4815
4816 len -= end_bits; /* Now len % 8 == 0 */
4817
4818 for( ; end_bits != 0; end_bits-- )
4819 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4820 }
4821
4822 memset( mask + offset / 8, 0xFF, len / 8 );
4823}
4824
4825/*
4826 * Check that bitmask is full
4827 */
4828static int ssl_bitmask_check( unsigned char *mask, size_t len )
4829{
4830 size_t i;
4831
4832 for( i = 0; i < len / 8; i++ )
4833 if( mask[i] != 0xFF )
4834 return( -1 );
4835
4836 for( i = 0; i < len % 8; i++ )
4837 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4838 return( -1 );
4839
4840 return( 0 );
4841}
4842
Hanno Becker56e205e2018-08-16 09:06:12 +01004843/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004844static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004845 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004846{
Hanno Becker56e205e2018-08-16 09:06:12 +01004847 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004848
Hanno Becker56e205e2018-08-16 09:06:12 +01004849 alloc_len = 12; /* Handshake header */
4850 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004851
Hanno Beckerd07df862018-08-16 09:14:58 +01004852 if( add_bitmap )
4853 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004854
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004855 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004856}
Hanno Becker56e205e2018-08-16 09:06:12 +01004857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004859
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004860static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004861{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004862 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004863}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004864
Simon Butcher99000142016-10-13 17:21:01 +01004865int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004866{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004870 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004872 }
4873
Hanno Becker12555c62018-08-16 12:47:53 +01004874 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004876 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004877 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004878 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004881 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004882 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004883 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004884 unsigned int recv_msg_seq = (unsigned int)
4885 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004886
Hanno Becker44650b72018-08-16 12:51:11 +01004887 if( ssl_check_hs_header( ssl ) != 0 )
4888 {
4889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4890 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4891 }
4892
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004893 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004894 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4895 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4896 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4897 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004898 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004899 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4900 {
4901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4902 recv_msg_seq,
4903 ssl->handshake->in_msg_seq ) );
4904 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4905 }
4906
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004907 /* Retransmit only on last message from previous flight, to avoid
4908 * too many retransmissions.
4909 * Besides, No sane server ever retransmits HelloVerifyRequest */
4910 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004914 "message_seq = %d, start_of_flight = %d",
4915 recv_msg_seq,
4916 ssl->handshake->in_flight_start_seq ) );
4917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004921 return( ret );
4922 }
4923 }
4924 else
4925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004927 "message_seq = %d, expected = %d",
4928 recv_msg_seq,
4929 ssl->handshake->in_msg_seq ) );
4930 }
4931
Hanno Becker90333da2017-10-10 11:27:13 +01004932 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004933 }
4934 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004935
Hanno Becker6d97ef52018-08-16 13:09:04 +01004936 /* Message reassembly is handled alongside buffering of future
4937 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004938 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004939 * handshake logic layer. */
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04004940 if( ssl_hs_is_proper_fragment( ssl ) == PROPER_HS_FRAGMENT )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004943 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004944 }
4945 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004946 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004948#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004949 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004950 /* With TLS we don't handle fragmentation (for now) */
4951 if( ssl->in_msglen < ssl->in_hslen )
4952 {
4953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4954 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4955 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004956 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004957#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004958
Simon Butcher99000142016-10-13 17:21:01 +01004959 return( 0 );
4960}
4961
4962void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4963{
Hanno Becker0271f962018-08-16 13:23:47 +01004964 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004965
Hanno Becker0271f962018-08-16 13:23:47 +01004966 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004967 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004968
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004969 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004971 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004972 ssl->handshake != NULL )
4973 {
Hanno Becker0271f962018-08-16 13:23:47 +01004974 unsigned offset;
4975 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004976
Hanno Becker0271f962018-08-16 13:23:47 +01004977 /* Increment handshake sequence number */
4978 hs->in_msg_seq++;
4979
4980 /*
4981 * Clear up handshake buffering and reassembly structure.
4982 */
4983
4984 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004985 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004986
4987 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004988 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4989 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004990 offset++, hs_buf++ )
4991 {
4992 *hs_buf = *(hs_buf + 1);
4993 }
4994
4995 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004996 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004997 }
4998#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004999}
5000
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005001/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005002 * DTLS anti-replay: RFC 6347 4.1.2.6
5003 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005004 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
5005 * Bit n is set iff record number in_window_top - n has been seen.
5006 *
5007 * Usually, in_window_top is the last record number seen and the lsb of
5008 * in_window is set. The only exception is the initial state (record number 0
5009 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5012static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005013{
5014 ssl->in_window_top = 0;
5015 ssl->in_window = 0;
5016}
5017
5018static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5019{
5020 return( ( (uint64_t) buf[0] << 40 ) |
5021 ( (uint64_t) buf[1] << 32 ) |
5022 ( (uint64_t) buf[2] << 24 ) |
5023 ( (uint64_t) buf[3] << 16 ) |
5024 ( (uint64_t) buf[4] << 8 ) |
5025 ( (uint64_t) buf[5] ) );
5026}
5027
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005028static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5029{
5030 int ret;
5031 unsigned char *original_in_ctr;
5032
5033 // save original in_ctr
5034 original_in_ctr = ssl->in_ctr;
5035
5036 // use counter from record
5037 ssl->in_ctr = record_in_ctr;
5038
5039 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5040
5041 // restore the counter
5042 ssl->in_ctr = original_in_ctr;
5043
5044 return ret;
5045}
5046
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005047/*
5048 * Return 0 if sequence number is acceptable, -1 otherwise
5049 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005050int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005051{
5052 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5053 uint64_t bit;
5054
Hanno Becker7f376f42019-06-12 16:20:48 +01005055 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5056 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5057 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005058 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005059 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005060
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005061 if( rec_seqnum > ssl->in_window_top )
5062 return( 0 );
5063
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005064 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005065
5066 if( bit >= 64 )
5067 return( -1 );
5068
5069 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5070 return( -1 );
5071
5072 return( 0 );
5073}
5074
5075/*
5076 * Update replay window on new validated record
5077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005079{
5080 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5081
Hanno Becker7f376f42019-06-12 16:20:48 +01005082 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5083 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5084 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005085 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005086 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005087
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005088 if( rec_seqnum > ssl->in_window_top )
5089 {
5090 /* Update window_top and the contents of the window */
5091 uint64_t shift = rec_seqnum - ssl->in_window_top;
5092
5093 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005094 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005095 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005096 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005097 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005098 ssl->in_window |= 1;
5099 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005100
5101 ssl->in_window_top = rec_seqnum;
5102 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005103 else
5104 {
5105 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005106 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005107
5108 if( bit < 64 ) /* Always true, but be extra sure */
5109 ssl->in_window |= (uint64_t) 1 << bit;
5110 }
5111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005113
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005114#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005115/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005116static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5117
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005118/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005119 * Without any SSL context, check if a datagram looks like a ClientHello with
5120 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005121 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005122 *
5123 * - if cookie is valid, return 0
5124 * - if ClientHello looks superficially valid but cookie is not,
5125 * fill obuf and set olen, then
5126 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5127 * - otherwise return a specific error code
5128 */
5129static int ssl_check_dtls_clihlo_cookie(
5130 mbedtls_ssl_cookie_write_t *f_cookie_write,
5131 mbedtls_ssl_cookie_check_t *f_cookie_check,
5132 void *p_cookie,
5133 const unsigned char *cli_id, size_t cli_id_len,
5134 const unsigned char *in, size_t in_len,
5135 unsigned char *obuf, size_t buf_len, size_t *olen )
5136{
5137 size_t sid_len, cookie_len;
5138 unsigned char *p;
5139
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005140 /*
5141 * Structure of ClientHello with record and handshake headers,
5142 * and expected values. We don't need to check a lot, more checks will be
5143 * done when actually parsing the ClientHello - skipping those checks
5144 * avoids code duplication and does not make cookie forging any easier.
5145 *
5146 * 0-0 ContentType type; copied, must be handshake
5147 * 1-2 ProtocolVersion version; copied
5148 * 3-4 uint16 epoch; copied, must be 0
5149 * 5-10 uint48 sequence_number; copied
5150 * 11-12 uint16 length; (ignored)
5151 *
5152 * 13-13 HandshakeType msg_type; (ignored)
5153 * 14-16 uint24 length; (ignored)
5154 * 17-18 uint16 message_seq; copied
5155 * 19-21 uint24 fragment_offset; copied, must be 0
5156 * 22-24 uint24 fragment_length; (ignored)
5157 *
5158 * 25-26 ProtocolVersion client_version; (ignored)
5159 * 27-58 Random random; (ignored)
5160 * 59-xx SessionID session_id; 1 byte len + sid_len content
5161 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5162 * ...
5163 *
5164 * Minimum length is 61 bytes.
5165 */
5166 if( in_len < 61 ||
5167 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5168 in[3] != 0 || in[4] != 0 ||
5169 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5170 {
5171 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5172 }
5173
5174 sid_len = in[59];
5175 if( sid_len > in_len - 61 )
5176 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5177
5178 cookie_len = in[60 + sid_len];
5179 if( cookie_len > in_len - 60 )
5180 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5181
5182 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5183 cli_id, cli_id_len ) == 0 )
5184 {
5185 /* Valid cookie */
5186 return( 0 );
5187 }
5188
5189 /*
5190 * If we get here, we've got an invalid cookie, let's prepare HVR.
5191 *
5192 * 0-0 ContentType type; copied
5193 * 1-2 ProtocolVersion version; copied
5194 * 3-4 uint16 epoch; copied
5195 * 5-10 uint48 sequence_number; copied
5196 * 11-12 uint16 length; olen - 13
5197 *
5198 * 13-13 HandshakeType msg_type; hello_verify_request
5199 * 14-16 uint24 length; olen - 25
5200 * 17-18 uint16 message_seq; copied
5201 * 19-21 uint24 fragment_offset; copied
5202 * 22-24 uint24 fragment_length; olen - 25
5203 *
5204 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5205 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5206 *
5207 * Minimum length is 28.
5208 */
5209 if( buf_len < 28 )
5210 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5211
5212 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005213 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005214 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5215 obuf[25] = 0xfe;
5216 obuf[26] = 0xff;
5217
5218 /* Generate and write actual cookie */
5219 p = obuf + 28;
5220 if( f_cookie_write( p_cookie,
5221 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5222 {
5223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5224 }
5225
5226 *olen = p - obuf;
5227
5228 /* Go back and fill length fields */
5229 obuf[27] = (unsigned char)( *olen - 28 );
5230
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005231 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005232 obuf[22] = obuf[14];
5233 obuf[23] = obuf[15];
5234 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005235
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005236 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005237
5238 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5239}
5240
5241/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005242 * Handle possible client reconnect with the same UDP quadruplet
5243 * (RFC 6347 Section 4.2.8).
5244 *
5245 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5246 * that looks like a ClientHello.
5247 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005248 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005249 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005250 * - if the input looks like a ClientHello with a valid cookie,
5251 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005252 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005253 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005254 *
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005255 * This function is called (through ssl_check_client_reconnect()) when an
5256 * unexpected record is found in ssl_get_next_record(), which will discard the
5257 * record if we return 0, and bubble up the return value otherwise (this
5258 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
5259 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005260 */
5261static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5262{
5263 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005264 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005265
Hanno Becker87b56262019-07-10 14:37:41 +01005266 if( ssl->conf->f_cookie_write == NULL ||
5267 ssl->conf->f_cookie_check == NULL )
5268 {
5269 /* If we can't use cookies to verify reachability of the peer,
5270 * drop the record. */
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
5272 "can't check reconnect validity" ) );
Hanno Becker87b56262019-07-10 14:37:41 +01005273 return( 0 );
5274 }
5275
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005276 ret = ssl_check_dtls_clihlo_cookie(
5277 ssl->conf->f_cookie_write,
5278 ssl->conf->f_cookie_check,
5279 ssl->conf->p_cookie,
5280 ssl->cli_id, ssl->cli_id_len,
5281 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005282 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005283
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005284 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5285
5286 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005287 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005288 int send_ret;
5289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
5290 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
5291 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08005292 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005293 * If the error is permanent we'll catch it later,
5294 * if it's not, then hopefully it'll work next time. */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005295 send_ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
5296 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_get_send", send_ret );
5297 (void) send_ret;
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005298 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005299 }
5300
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005301 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005302 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005304 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5305 {
5306 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5307 return( ret );
5308 }
5309
5310 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005311 }
5312
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005313 return( ret );
5314}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005315#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005316
Hanno Becker46483f12019-05-03 13:25:54 +01005317static int ssl_check_record_type( uint8_t record_type )
5318{
5319 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5320 record_type != MBEDTLS_SSL_MSG_ALERT &&
5321 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5322 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5323 {
5324 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5325 }
5326
5327 return( 0 );
5328}
5329
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005330/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005331 * ContentType type;
5332 * ProtocolVersion version;
5333 * uint16 epoch; // DTLS only
5334 * uint48 sequence_number; // DTLS only
5335 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005336 *
5337 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005338 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005339 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5340 *
5341 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005342 * 1. proceed with the record if this function returns 0
5343 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5344 * 3. return CLIENT_RECONNECT if this function return that value
5345 * 4. drop the whole datagram if this function returns anything else.
5346 * Point 2 is needed when the peer is resending, and we have already received
5347 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005348 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005349static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005350 unsigned char *buf,
5351 size_t len,
5352 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005353{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005354 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005355
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005356 size_t const rec_hdr_type_offset = 0;
5357 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005358
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005359 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5360 rec_hdr_type_len;
5361 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005362
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005363 size_t const rec_hdr_ctr_len = 8;
5364#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005365 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005366 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5367 rec_hdr_version_len;
5368
Hanno Beckera5a2b082019-05-15 14:03:01 +01005369#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005370 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5371 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005372 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005373#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5374#endif /* MBEDTLS_SSL_PROTO_DTLS */
5375
5376 size_t rec_hdr_len_offset; /* To be determined */
5377 size_t const rec_hdr_len_len = 2;
5378
5379 /*
5380 * Check minimum lengths for record header.
5381 */
5382
5383#if defined(MBEDTLS_SSL_PROTO_DTLS)
5384 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5385 {
5386 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5387 }
5388 MBEDTLS_SSL_TRANSPORT_ELSE
5389#endif /* MBEDTLS_SSL_PROTO_DTLS */
5390#if defined(MBEDTLS_SSL_PROTO_TLS)
5391 {
5392 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5393 }
5394#endif /* MBEDTLS_SSL_PROTO_DTLS */
5395
5396 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5397 {
5398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5399 (unsigned) len,
5400 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5401 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5402 }
5403
5404 /*
5405 * Parse and validate record content type
5406 */
5407
5408 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005409
5410 /* Check record content type */
5411#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5412 rec->cid_len = 0;
5413
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005414 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005415 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5416 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005417 {
5418 /* Shift pointers to account for record header including CID
5419 * struct {
5420 * ContentType special_type = tls12_cid;
5421 * ProtocolVersion version;
5422 * uint16 epoch;
5423 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005424 * opaque cid[cid_length]; // Additional field compared to
5425 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005426 * uint16 length;
5427 * opaque enc_content[DTLSCiphertext.length];
5428 * } DTLSCiphertext;
5429 */
5430
5431 /* So far, we only support static CID lengths
5432 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005433 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5434 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005435
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005436 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005437 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5439 (unsigned) len,
5440 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005442 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005443
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005444 /* configured CID len is guaranteed at most 255, see
5445 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5446 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005447 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5448 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005449 }
5450 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005451#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005452 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005453 if( ssl_check_record_type( rec->type ) )
5454 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5456 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005457 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5458 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005459 }
5460
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005461 /*
5462 * Parse and validate record version
5463 */
5464
Hanno Becker8061c6e2019-07-26 08:07:03 +01005465 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5466 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005467 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5468 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005469 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005470
Hanno Becker2881d802019-05-22 14:44:53 +01005471 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5474 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 }
5476
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005477 if( mbedtls_ssl_ver_gt( minor_ver,
5478 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5481 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005482 }
5483
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005484 /*
5485 * Parse/Copy record sequence number.
5486 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005487
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005488#if defined(MBEDTLS_SSL_PROTO_DTLS)
5489 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5490 {
5491 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005492 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5493 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005494 rec_hdr_ctr_len );
5495 }
5496 MBEDTLS_SSL_TRANSPORT_ELSE
5497#endif /* MBEDTLS_SSL_PROTO_DTLS */
5498#if defined(MBEDTLS_SSL_PROTO_TLS)
5499 {
5500 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005501 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5502 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005503 }
5504#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005505
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005506 /*
5507 * Parse record length.
5508 */
5509
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005510 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005511 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005512 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5513
Hanno Becker8b09b732019-05-08 12:03:28 +01005514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005515 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005516 rec->type,
5517 major_ver, minor_ver, rec->data_len ) );
5518
5519 rec->buf = buf;
5520 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005521
Hanno Beckerec014082019-07-26 08:20:27 +01005522 if( rec->data_len == 0 )
5523 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5524
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005525 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005526 * DTLS-related tests.
5527 * Check epoch before checking length constraint because
5528 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5529 * message gets duplicated before the corresponding Finished message,
5530 * the second ChangeCipherSpec should be discarded because it belongs
5531 * to an old epoch, but not because its length is shorter than
5532 * the minimum record length for packets using the new record transform.
5533 * Note that these two kinds of failures are handled differently,
5534 * as an unexpected record is silently skipped but an invalid
5535 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005536 */
5537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005538 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005539 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005540 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005541
Hanno Beckere0452772019-07-10 17:12:07 +01005542 /* Check that the datagram is large enough to contain a record
5543 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005544 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005545 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5547 (unsigned) len,
5548 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005549 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5550 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005551
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005552 /* Records from other, non-matching epochs are silently discarded.
5553 * (The case of same-port Client reconnects must be considered in
5554 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005555 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005556 {
5557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5558 "expected %d, received %d",
5559 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005560
5561 /* Records from the next epoch are considered for buffering
5562 * (concretely: early Finished messages). */
5563 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5564 {
5565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5566 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5567 }
5568
Hanno Becker87b56262019-07-10 14:37:41 +01005569 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005570 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005571#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005572 /* For records from the correct epoch, check whether their
5573 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005574 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5575 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005576 {
5577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5578 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5579 }
5580#endif
5581 }
5582#endif /* MBEDTLS_SSL_PROTO_DTLS */
5583
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005584 return( 0 );
5585}
Paul Bakker5121ce52009-01-03 21:22:43 +00005586
Hanno Becker87b56262019-07-10 14:37:41 +01005587
5588#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5589static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5590{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005591 unsigned int rec_epoch = (unsigned int)
5592 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005593
5594 /*
5595 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5596 * access the first byte of record content (handshake type), as we
5597 * have an active transform (possibly iv_len != 0), so use the
5598 * fact that the record header len is 13 instead.
5599 */
5600 if( rec_epoch == 0 &&
5601 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5602 MBEDTLS_SSL_IS_SERVER &&
5603 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5604 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5605 ssl->in_left > 13 &&
5606 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5607 {
5608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5609 "from the same port" ) );
5610 return( ssl_handle_possible_reconnect( ssl ) );
5611 }
5612
5613 return( 0 );
5614}
5615#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5616
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005617/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005618 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005619 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005620static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5621 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005622{
5623 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005626 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5629 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 ret = mbedtls_ssl_hw_record_read( ssl );
5634 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5637 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005638 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005639
5640 if( ret == 0 )
5641 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005644 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005645 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005646 unsigned char const old_msg_type = rec->type;
5647
Hanno Becker611a83b2018-01-03 14:27:32 +00005648 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005649 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005651 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005652
Hanno Beckera5a2b082019-05-15 14:03:01 +01005653#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005654 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005655 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5656 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005657 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005659 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005660 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005661#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005662
Paul Bakker5121ce52009-01-03 21:22:43 +00005663 return( ret );
5664 }
5665
Hanno Becker106f3da2019-07-12 09:35:58 +01005666 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005667 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005668 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005669 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005671
Paul Bakker5121ce52009-01-03 21:22:43 +00005672 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005673 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005674
Hanno Beckera5a2b082019-05-15 14:03:01 +01005675#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005676 /* We have already checked the record content type
5677 * in ssl_parse_record_header(), failing or silently
5678 * dropping the record in the case of an unknown type.
5679 *
5680 * Since with the use of CIDs, the record content type
5681 * might change during decryption, re-check the record
5682 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005683 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005684 {
5685 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5686 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5687 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005688#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005689
Hanno Becker106f3da2019-07-12 09:35:58 +01005690 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005691 {
5692#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005693 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005694 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005695 {
5696 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5698 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5699 }
5700#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5701
5702 ssl->nb_zero++;
5703
5704 /*
5705 * Three or more empty messages may be a DoS attack
5706 * (excessive CPU consumption).
5707 */
5708 if( ssl->nb_zero > 3 )
5709 {
5710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005711 "messages, possible DoS attack" ) );
5712 /* Treat the records as if they were not properly authenticated,
5713 * thereby failing the connection if we see more than allowed
5714 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005715 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5716 }
5717 }
5718 else
5719 ssl->nb_zero = 0;
5720
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005721 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5722#if defined(MBEDTLS_SSL_PROTO_TLS)
5723 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005724 {
5725 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005726 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005727 if( ++ssl->in_ctr[i - 1] != 0 )
5728 break;
5729
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005730 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005731 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005732 {
5733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5734 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5735 }
5736 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005737#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005738
Paul Bakker5121ce52009-01-03 21:22:43 +00005739 }
5740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005741#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005742 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005744 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005745 }
5746#endif
5747
Hanno Beckerf0242852019-07-09 17:30:02 +01005748 /* Check actual (decrypted) record content length against
5749 * configured maximum. */
5750 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5751 {
5752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5753 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5754 }
5755
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005756 return( 0 );
5757}
5758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005760
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005761/*
5762 * Read a record.
5763 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005764 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5765 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5766 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005767 */
Hanno Becker1097b342018-08-15 14:09:41 +01005768
5769/* Helper functions for mbedtls_ssl_read_record(). */
5770static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005771static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5772static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005773
Hanno Becker327c93b2018-08-15 13:56:18 +01005774int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005775 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005776{
5777 int ret;
5778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005780
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005781 if( ssl->keep_current_message == 0 )
5782 {
5783 do {
Simon Butcher99000142016-10-13 17:21:01 +01005784
Hanno Becker26994592018-08-15 14:14:59 +01005785 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005786 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005787 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005788
Hanno Beckere74d5562018-08-15 14:26:08 +01005789 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005790 {
Hanno Becker40f50842018-08-15 14:48:01 +01005791#if defined(MBEDTLS_SSL_PROTO_DTLS)
5792 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005793
Hanno Becker40f50842018-08-15 14:48:01 +01005794 /* We only check for buffered messages if the
5795 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005796 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005797 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005798 {
Hanno Becker40f50842018-08-15 14:48:01 +01005799 if( ssl_load_buffered_message( ssl ) == 0 )
5800 have_buffered = 1;
5801 }
5802
5803 if( have_buffered == 0 )
5804#endif /* MBEDTLS_SSL_PROTO_DTLS */
5805 {
5806 ret = ssl_get_next_record( ssl );
5807 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5808 continue;
5809
5810 if( ret != 0 )
5811 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005812 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005813 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005814 return( ret );
5815 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005816 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005817 }
5818
5819 ret = mbedtls_ssl_handle_message_type( ssl );
5820
Hanno Becker40f50842018-08-15 14:48:01 +01005821#if defined(MBEDTLS_SSL_PROTO_DTLS)
5822 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5823 {
5824 /* Buffer future message */
5825 ret = ssl_buffer_message( ssl );
5826 if( ret != 0 )
5827 return( ret );
5828
5829 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5830 }
5831#endif /* MBEDTLS_SSL_PROTO_DTLS */
5832
Hanno Becker90333da2017-10-10 11:27:13 +01005833 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5834 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005835
5836 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005837 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005838 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005839 return( ret );
5840 }
5841
Hanno Becker327c93b2018-08-15 13:56:18 +01005842 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005843 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005844 {
5845 mbedtls_ssl_update_handshake_status( ssl );
5846 }
Simon Butcher99000142016-10-13 17:21:01 +01005847 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005848 else
Simon Butcher99000142016-10-13 17:21:01 +01005849 {
Hanno Becker02f59072018-08-15 14:00:24 +01005850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005851 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005852 }
5853
5854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5855
5856 return( 0 );
5857}
5858
Hanno Becker40f50842018-08-15 14:48:01 +01005859#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005860static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005861{
Hanno Becker40f50842018-08-15 14:48:01 +01005862 if( ssl->in_left > ssl->next_record_offset )
5863 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005864
Hanno Becker40f50842018-08-15 14:48:01 +01005865 return( 0 );
5866}
5867
5868static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5869{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005870 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005871 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005872 int ret = 0;
5873
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005874 if( hs == NULL )
5875 return( -1 );
5876
Hanno Beckere00ae372018-08-20 09:39:42 +01005877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5878
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005879 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5880 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5881 {
5882 /* Check if we have seen a ChangeCipherSpec before.
5883 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005884 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005885 {
5886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5887 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005888 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005889 }
5890
Hanno Becker39b8bc92018-08-28 17:17:13 +01005891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005892 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5893 ssl->in_msglen = 1;
5894 ssl->in_msg[0] = 1;
5895
5896 /* As long as they are equal, the exact value doesn't matter. */
5897 ssl->in_left = 0;
5898 ssl->next_record_offset = 0;
5899
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005900 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005901 goto exit;
5902 }
Hanno Becker37f95322018-08-16 13:55:32 +01005903
Hanno Beckerb8f50142018-08-28 10:01:34 +01005904#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005905 /* Debug only */
5906 {
5907 unsigned offset;
5908 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5909 {
5910 hs_buf = &hs->buffering.hs[offset];
5911 if( hs_buf->is_valid == 1 )
5912 {
5913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5914 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005915 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005916 }
5917 }
5918 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005919#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005920
5921 /* Check if we have buffered and/or fully reassembled the
5922 * next handshake message. */
5923 hs_buf = &hs->buffering.hs[0];
5924 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5925 {
5926 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005927 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005928
5929 /* Double-check that we haven't accidentally buffered
5930 * a message that doesn't fit into the input buffer. */
5931 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5932 {
5933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5935 }
5936
5937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5938 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5939 hs_buf->data, msg_len + 12 );
5940
5941 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5942 ssl->in_hslen = msg_len + 12;
5943 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005944 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005945
5946 ret = 0;
5947 goto exit;
5948 }
5949 else
5950 {
5951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5952 hs->in_msg_seq ) );
5953 }
5954
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005955 ret = -1;
5956
5957exit:
5958
5959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5960 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005961}
5962
Hanno Beckera02b0b42018-08-21 17:20:27 +01005963static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5964 size_t desired )
5965{
5966 int offset;
5967 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5969 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005970
Hanno Becker01315ea2018-08-21 17:22:17 +01005971 /* Get rid of future records epoch first, if such exist. */
5972 ssl_free_buffered_record( ssl );
5973
5974 /* Check if we have enough space available now. */
5975 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5976 hs->buffering.total_bytes_buffered ) )
5977 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005979 return( 0 );
5980 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005981
Hanno Becker4f432ad2018-08-28 10:02:32 +01005982 /* We don't have enough space to buffer the next expected handshake
5983 * message. Remove buffers used for future messages to gain space,
5984 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005985 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5986 offset >= 0; offset-- )
5987 {
5988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5989 offset ) );
5990
Hanno Beckerb309b922018-08-23 13:18:05 +01005991 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005992
5993 /* Check if we have enough space available now. */
5994 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5995 hs->buffering.total_bytes_buffered ) )
5996 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005998 return( 0 );
5999 }
6000 }
6001
6002 return( -1 );
6003}
6004
Hanno Becker40f50842018-08-15 14:48:01 +01006005static int ssl_buffer_message( mbedtls_ssl_context *ssl )
6006{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006007 int ret = 0;
6008 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6009
6010 if( hs == NULL )
6011 return( 0 );
6012
6013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
6014
6015 switch( ssl->in_msgtype )
6016 {
6017 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006019
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006020 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006021 break;
6022
6023 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006024 {
6025 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006026 unsigned recv_msg_seq = (unsigned)
6027 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006028
Hanno Becker37f95322018-08-16 13:55:32 +01006029 mbedtls_ssl_hs_buffer *hs_buf;
6030 size_t msg_len = ssl->in_hslen - 12;
6031
6032 /* We should never receive an old handshake
6033 * message - double-check nonetheless. */
6034 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6035 {
6036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6038 }
6039
6040 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6041 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6042 {
6043 /* Silently ignore -- message too far in the future */
6044 MBEDTLS_SSL_DEBUG_MSG( 2,
6045 ( "Ignore future HS message with sequence number %u, "
6046 "buffering window %u - %u",
6047 recv_msg_seq, ssl->handshake->in_msg_seq,
6048 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6049
6050 goto exit;
6051 }
6052
6053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6054 recv_msg_seq, recv_msg_seq_offset ) );
6055
6056 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6057
6058 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006059 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006060 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006061 size_t reassembly_buf_sz;
6062
Hanno Becker37f95322018-08-16 13:55:32 +01006063 hs_buf->is_fragmented =
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04006064 ( ssl_hs_is_proper_fragment( ssl ) == PROPER_HS_FRAGMENT );
Hanno Becker37f95322018-08-16 13:55:32 +01006065
6066 /* We copy the message back into the input buffer
6067 * after reassembly, so check that it's not too large.
6068 * This is an implementation-specific limitation
6069 * and not one from the standard, hence it is not
6070 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006071 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006072 {
6073 /* Ignore message */
6074 goto exit;
6075 }
6076
Hanno Beckere0b150f2018-08-21 15:51:03 +01006077 /* Check if we have enough space to buffer the message. */
6078 if( hs->buffering.total_bytes_buffered >
6079 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6080 {
6081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6083 }
6084
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006085 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6086 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006087
6088 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6089 hs->buffering.total_bytes_buffered ) )
6090 {
6091 if( recv_msg_seq_offset > 0 )
6092 {
6093 /* If we can't buffer a future message because
6094 * of space limitations -- ignore. */
6095 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",
6096 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6097 (unsigned) hs->buffering.total_bytes_buffered ) );
6098 goto exit;
6099 }
Hanno Beckere1801392018-08-21 16:51:05 +01006100 else
6101 {
6102 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",
6103 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6104 (unsigned) hs->buffering.total_bytes_buffered ) );
6105 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006106
Hanno Beckera02b0b42018-08-21 17:20:27 +01006107 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006108 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006109 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",
6110 (unsigned) msg_len,
6111 (unsigned) reassembly_buf_sz,
6112 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006113 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006114 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6115 goto exit;
6116 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006117 }
6118
6119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6120 msg_len ) );
6121
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006122 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6123 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006124 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006125 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006126 goto exit;
6127 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006128 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006129
6130 /* Prepare final header: copy msg_type, length and message_seq,
6131 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006132 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006133 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006134 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006135
6136 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006137
6138 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006139 }
6140 else
6141 {
6142 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006143 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 +01006144 {
6145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6146 /* Ignore */
6147 goto exit;
6148 }
6149 }
6150
Hanno Becker4422bbb2018-08-20 09:40:19 +01006151 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006152 {
6153 size_t frag_len, frag_off;
6154 unsigned char * const msg = hs_buf->data + 12;
6155
6156 /*
6157 * Check and copy current fragment
6158 */
6159
6160 /* Validation of header fields already done in
6161 * mbedtls_ssl_prepare_handshake_record(). */
6162 frag_off = ssl_get_hs_frag_off( ssl );
6163 frag_len = ssl_get_hs_frag_len( ssl );
6164
6165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6166 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006167 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006168
6169 if( hs_buf->is_fragmented )
6170 {
6171 unsigned char * const bitmask = msg + msg_len;
6172 ssl_bitmask_set( bitmask, frag_off, frag_len );
6173 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6174 msg_len ) == 0 );
6175 }
6176 else
6177 {
6178 hs_buf->is_complete = 1;
6179 }
6180
6181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6182 hs_buf->is_complete ? "" : "not yet " ) );
6183 }
6184
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006185 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006186 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006187
6188 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006189 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006190 break;
6191 }
6192
6193exit:
6194
6195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6196 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006197}
6198#endif /* MBEDTLS_SSL_PROTO_DTLS */
6199
Hanno Becker1097b342018-08-15 14:09:41 +01006200static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006201{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006202 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006203 * Consume last content-layer message and potentially
6204 * update in_msglen which keeps track of the contents'
6205 * consumption state.
6206 *
6207 * (1) Handshake messages:
6208 * Remove last handshake message, move content
6209 * and adapt in_msglen.
6210 *
6211 * (2) Alert messages:
6212 * Consume whole record content, in_msglen = 0.
6213 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006214 * (3) Change cipher spec:
6215 * Consume whole record content, in_msglen = 0.
6216 *
6217 * (4) Application data:
6218 * Don't do anything - the record layer provides
6219 * the application data as a stream transport
6220 * and consumes through mbedtls_ssl_read only.
6221 *
6222 */
6223
6224 /* Case (1): Handshake messages */
6225 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006226 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006227 /* Hard assertion to be sure that no application data
6228 * is in flight, as corrupting ssl->in_msglen during
6229 * ssl->in_offt != NULL is fatal. */
6230 if( ssl->in_offt != NULL )
6231 {
6232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6233 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6234 }
6235
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006236 /*
6237 * Get next Handshake message in the current record
6238 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006239
Hanno Becker4a810fb2017-05-24 16:27:30 +01006240 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006241 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006242 * current handshake content: If DTLS handshake
6243 * fragmentation is used, that's the fragment
6244 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006245 * size here is faulty and should be changed at
6246 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006247 * (2) While it doesn't seem to cause problems, one
6248 * has to be very careful not to assume that in_hslen
6249 * is always <= in_msglen in a sensible communication.
6250 * Again, it's wrong for DTLS handshake fragmentation.
6251 * The following check is therefore mandatory, and
6252 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006253 * Additionally, ssl->in_hslen might be arbitrarily out of
6254 * bounds after handling a DTLS message with an unexpected
6255 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006256 */
6257 if( ssl->in_hslen < ssl->in_msglen )
6258 {
6259 ssl->in_msglen -= ssl->in_hslen;
6260 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6261 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006262
Hanno Becker4a810fb2017-05-24 16:27:30 +01006263 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6264 ssl->in_msg, ssl->in_msglen );
6265 }
6266 else
6267 {
6268 ssl->in_msglen = 0;
6269 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006270
Hanno Becker4a810fb2017-05-24 16:27:30 +01006271 ssl->in_hslen = 0;
6272 }
6273 /* Case (4): Application data */
6274 else if( ssl->in_offt != NULL )
6275 {
6276 return( 0 );
6277 }
6278 /* Everything else (CCS & Alerts) */
6279 else
6280 {
6281 ssl->in_msglen = 0;
6282 }
6283
Hanno Becker1097b342018-08-15 14:09:41 +01006284 return( 0 );
6285}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006286
Hanno Beckere74d5562018-08-15 14:26:08 +01006287static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6288{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006289 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006290 return( 1 );
6291
6292 return( 0 );
6293}
6294
Hanno Becker5f066e72018-08-16 14:56:31 +01006295#if defined(MBEDTLS_SSL_PROTO_DTLS)
6296
6297static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6298{
6299 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6300 if( hs == NULL )
6301 return;
6302
Hanno Becker01315ea2018-08-21 17:22:17 +01006303 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006304 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006305 hs->buffering.total_bytes_buffered -=
6306 hs->buffering.future_record.len;
6307
6308 mbedtls_free( hs->buffering.future_record.data );
6309 hs->buffering.future_record.data = NULL;
6310 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006311}
6312
6313static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6314{
6315 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6316 unsigned char * rec;
6317 size_t rec_len;
6318 unsigned rec_epoch;
6319
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006320 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006321 return( 0 );
6322
6323 if( hs == NULL )
6324 return( 0 );
6325
Hanno Becker5f066e72018-08-16 14:56:31 +01006326 rec = hs->buffering.future_record.data;
6327 rec_len = hs->buffering.future_record.len;
6328 rec_epoch = hs->buffering.future_record.epoch;
6329
6330 if( rec == NULL )
6331 return( 0 );
6332
Hanno Becker4cb782d2018-08-20 11:19:05 +01006333 /* Only consider loading future records if the
6334 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006335 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006336 return( 0 );
6337
Hanno Becker5f066e72018-08-16 14:56:31 +01006338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6339
6340 if( rec_epoch != ssl->in_epoch )
6341 {
6342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6343 goto exit;
6344 }
6345
6346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6347
6348 /* Double-check that the record is not too large */
6349 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6350 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6351 {
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6353 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6354 }
6355
Teppo Järvelin91d79382019-10-02 09:09:31 +03006356 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006357 ssl->in_left = rec_len;
6358 ssl->next_record_offset = 0;
6359
6360 ssl_free_buffered_record( ssl );
6361
6362exit:
6363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6364 return( 0 );
6365}
6366
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006367static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6368 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006369{
6370 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006371
6372 /* Don't buffer future records outside handshakes. */
6373 if( hs == NULL )
6374 return( 0 );
6375
6376 /* Only buffer handshake records (we are only interested
6377 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006378 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006379 return( 0 );
6380
6381 /* Don't buffer more than one future epoch record. */
6382 if( hs->buffering.future_record.data != NULL )
6383 return( 0 );
6384
Hanno Becker01315ea2018-08-21 17:22:17 +01006385 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006386 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006387 hs->buffering.total_bytes_buffered ) )
6388 {
6389 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 +01006390 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006391 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006392 return( 0 );
6393 }
6394
Hanno Becker5f066e72018-08-16 14:56:31 +01006395 /* Buffer record */
6396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6397 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006398 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006399
6400 /* ssl_parse_record_header() only considers records
6401 * of the next epoch as candidates for buffering. */
6402 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006403 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006404
6405 hs->buffering.future_record.data =
6406 mbedtls_calloc( 1, hs->buffering.future_record.len );
6407 if( hs->buffering.future_record.data == NULL )
6408 {
6409 /* If we run out of RAM trying to buffer a
6410 * record from the next epoch, just ignore. */
6411 return( 0 );
6412 }
6413
Teppo Järvelin91d79382019-10-02 09:09:31 +03006414 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006415
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006416 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006417 return( 0 );
6418}
6419
6420#endif /* MBEDTLS_SSL_PROTO_DTLS */
6421
Hanno Beckere74d5562018-08-15 14:26:08 +01006422static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006423{
6424 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006425 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006426
Hanno Becker5f066e72018-08-16 14:56:31 +01006427#if defined(MBEDTLS_SSL_PROTO_DTLS)
6428 /* We might have buffered a future record; if so,
6429 * and if the epoch matches now, load it.
6430 * On success, this call will set ssl->in_left to
6431 * the length of the buffered record, so that
6432 * the calls to ssl_fetch_input() below will
6433 * essentially be no-ops. */
6434 ret = ssl_load_buffered_record( ssl );
6435 if( ret != 0 )
6436 return( ret );
6437#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006438
Hanno Becker8b09b732019-05-08 12:03:28 +01006439 /* Ensure that we have enough space available for the default form
6440 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6441 * with no space for CIDs counted in). */
6442 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6443 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006446 return( ret );
6447 }
6448
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006449 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6450 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006453 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006454 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006455 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6456 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006457 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006458 if( ret != 0 )
6459 return( ret );
6460
6461 /* Fall through to handling of unexpected records */
6462 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6463 }
6464
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006465 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6466 {
Hanno Becker87b56262019-07-10 14:37:41 +01006467#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006468 /* Reset in pointers to default state for TLS/DTLS records,
6469 * assuming no CID and no offset between record content and
6470 * record plaintext. */
6471 ssl_update_in_pointers( ssl );
6472
Hanno Becker69412452019-07-12 08:33:49 +01006473 /* Setup internal message pointers from record structure. */
6474 ssl->in_msgtype = rec.type;
6475#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6476 ssl->in_len = ssl->in_cid + rec.cid_len;
6477#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006478 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006479 ssl->in_msglen = rec.data_len;
6480
Hanno Becker87b56262019-07-10 14:37:41 +01006481 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02006482 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker87b56262019-07-10 14:37:41 +01006483 if( ret != 0 )
6484 return( ret );
6485#endif
6486
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006487 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006488 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006489
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6491 "(header)" ) );
6492 }
6493 else
6494 {
6495 /* Skip invalid record and the rest of the datagram */
6496 ssl->next_record_offset = 0;
6497 ssl->in_left = 0;
6498
6499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6500 "(header)" ) );
6501 }
6502
6503 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006504 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006505 }
Hanno Becker87b56262019-07-10 14:37:41 +01006506 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006507#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006508 {
6509 return( ret );
6510 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006511 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006514 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006515 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006516 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006517 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006518 if( ssl->next_record_offset < ssl->in_left )
6519 {
6520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6521 }
6522 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006523 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006524#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006525#if defined(MBEDTLS_SSL_PROTO_TLS)
6526 {
Hanno Beckere0452772019-07-10 17:12:07 +01006527 /*
6528 * Fetch record contents from underlying transport.
6529 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006530 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006531 if( ret != 0 )
6532 {
6533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6534 return( ret );
6535 }
6536
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006537 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006538 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006539#endif /* MBEDTLS_SSL_PROTO_TLS */
6540
6541 /*
6542 * Decrypt record contents.
6543 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006544
Hanno Beckera89610a2019-07-11 13:07:45 +01006545 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006548 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006549 {
6550 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006551 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006552 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006553 /* Except when waiting for Finished as a bad mac here
6554 * probably means something went wrong in the handshake
6555 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6556 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6557 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6558 {
6559#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6560 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6561 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006562 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006563 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6564 }
6565#endif
6566 return( ret );
6567 }
6568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006569#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006570 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6571 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6574 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006575 }
6576#endif
6577
Hanno Becker4a810fb2017-05-24 16:27:30 +01006578 /* As above, invalid records cause
6579 * dismissal of the whole datagram. */
6580
6581 ssl->next_record_offset = 0;
6582 ssl->in_left = 0;
6583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006585 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006586 }
6587
6588 return( ret );
6589 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006590 MBEDTLS_SSL_TRANSPORT_ELSE
6591#endif /* MBEDTLS_SSL_PROTO_DTLS */
6592#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006593 {
6594 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6596 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006597 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006598 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006600 }
6601#endif
6602 return( ret );
6603 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006604#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006605 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006606
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006607
6608 /* Reset in pointers to default state for TLS/DTLS records,
6609 * assuming no CID and no offset between record content and
6610 * record plaintext. */
6611 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006612#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6613 ssl->in_len = ssl->in_cid + rec.cid_len;
6614#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006615 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006616
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006617 /* The record content type may change during decryption,
6618 * so re-read it. */
6619 ssl->in_msgtype = rec.type;
6620 /* Also update the input buffer, because unfortunately
6621 * the server-side ssl_parse_client_hello() reparses the
6622 * record header when receiving a ClientHello initiating
6623 * a renegotiation. */
6624 ssl->in_hdr[0] = rec.type;
6625 ssl->in_msg = rec.buf + rec.data_offset;
6626 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006627 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006628
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006629#if defined(MBEDTLS_ZLIB_SUPPORT)
6630 if( ssl->transform_in != NULL &&
6631 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6632 {
6633 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6634 {
6635 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6636 return( ret );
6637 }
6638
6639 /* Check actual (decompress) record content length against
6640 * configured maximum. */
6641 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6642 {
6643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6644 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6645 }
6646 }
6647#endif /* MBEDTLS_ZLIB_SUPPORT */
6648
Simon Butcher99000142016-10-13 17:21:01 +01006649 return( 0 );
6650}
6651
6652int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6653{
6654 int ret;
6655
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006656 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006657 * Handle particular types of records
6658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006660 {
Simon Butcher99000142016-10-13 17:21:01 +01006661 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6662 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006663 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 }
6666
Hanno Beckere678eaa2018-08-21 14:57:46 +01006667 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006668 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006669 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006670 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6672 ssl->in_msglen ) );
6673 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006674 }
6675
Hanno Beckere678eaa2018-08-21 14:57:46 +01006676 if( ssl->in_msg[0] != 1 )
6677 {
6678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6679 ssl->in_msg[0] ) );
6680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6681 }
6682
6683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006684 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006685 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6686 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6687 {
6688 if( ssl->handshake == NULL )
6689 {
6690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6691 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6692 }
6693
6694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6695 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6696 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006697#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006698 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006701 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006702 if( ssl->in_msglen != 2 )
6703 {
6704 /* Note: Standard allows for more than one 2 byte alert
6705 to be packed in a single message, but Mbed TLS doesn't
6706 currently support this. */
6707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6708 ssl->in_msglen ) );
6709 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6710 }
6711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006713 ssl->in_msg[0], ssl->in_msg[1] ) );
6714
6715 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006716 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006721 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006723 }
6724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6726 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6729 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006730 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006731
6732#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6733 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6734 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6735 {
Hanno Becker90333da2017-10-10 11:27:13 +01006736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006737 /* Will be handled when trying to parse ServerHello */
6738 return( 0 );
6739 }
6740#endif
6741
6742#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006743 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006744 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6745 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006746 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6747 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6748 {
6749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6750 /* Will be handled in mbedtls_ssl_parse_certificate() */
6751 return( 0 );
6752 }
6753#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6754
6755 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006756 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006757 }
6758
Hanno Beckerc76c6192017-06-06 10:03:17 +01006759#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006760 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006761 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006762 /* Drop unexpected ApplicationData records,
6763 * except at the beginning of renegotiations */
6764 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6765 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6766#if defined(MBEDTLS_SSL_RENEGOTIATION)
6767 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6768 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006769#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006770 )
6771 {
6772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6773 return( MBEDTLS_ERR_SSL_NON_FATAL );
6774 }
6775
6776 if( ssl->handshake != NULL &&
6777 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6778 {
6779 ssl_handshake_wrapup_free_hs_transform( ssl );
6780 }
6781 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006782#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006783
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 return( 0 );
6785}
6786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006788{
6789 int ret;
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6792 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6793 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006794 {
6795 return( ret );
6796 }
6797
6798 return( 0 );
6799}
6800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006802 unsigned char level,
6803 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006804{
6805 int ret;
6806
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006807 if( ssl == NULL || ssl->conf == NULL )
6808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006814 ssl->out_msglen = 2;
6815 ssl->out_msg[0] = level;
6816 ssl->out_msg[1] = message;
6817
Hanno Becker67bc7c32018-08-06 11:33:50 +01006818 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006821 return( ret );
6822 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006824
6825 return( 0 );
6826}
6827
Hanno Becker17572472019-02-08 07:19:04 +00006828#if defined(MBEDTLS_X509_CRT_PARSE_C)
6829static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6830{
6831#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6832 if( session->peer_cert != NULL )
6833 {
6834 mbedtls_x509_crt_free( session->peer_cert );
6835 mbedtls_free( session->peer_cert );
6836 session->peer_cert = NULL;
6837 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006838#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006839 if( session->peer_cert_digest != NULL )
6840 {
6841 /* Zeroization is not necessary. */
6842 mbedtls_free( session->peer_cert_digest );
6843 session->peer_cert_digest = NULL;
6844 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6845 session->peer_cert_digest_len = 0;
6846 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006847#else
6848 ((void) session);
6849#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006850}
6851#endif /* MBEDTLS_X509_CRT_PARSE_C */
6852
Paul Bakker5121ce52009-01-03 21:22:43 +00006853/*
6854 * Handshake functions
6855 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006856#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006857/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006859{
Hanno Beckerdf645962019-06-26 13:02:22 +01006860 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6861 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006864
Hanno Becker5097cba2019-02-05 13:36:46 +00006865 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006868 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6869 {
6870 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6871 }
6872 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6873 {
6874 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6875 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006876 else
6877 {
6878 ssl->state = MBEDTLS_SSL_INVALID;
6879 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006880 return( 0 );
6881 }
6882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006885}
6886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006888{
Hanno Beckerdf645962019-06-26 13:02:22 +01006889 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6890 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006893
Hanno Becker5097cba2019-02-05 13:36:46 +00006894 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006897 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6898 {
6899 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6900 }
6901 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6902 {
6903 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6904 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006905 else
6906 {
6907 ssl->state = MBEDTLS_SSL_INVALID;
6908 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006909 return( 0 );
6910 }
6911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006914}
Gilles Peskinef9828522017-05-03 12:28:43 +02006915
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006916#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006917/* Some certificate support -> implement write and parse */
6918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006920{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006922 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006924 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6925 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006928
Hanno Becker5097cba2019-02-05 13:36:46 +00006929 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006932 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6933 {
6934 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6935 }
6936 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6937 {
6938 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6939 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006940 else
6941 {
6942 ssl->state = MBEDTLS_SSL_INVALID;
6943 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006944 return( 0 );
6945 }
6946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006948 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6949 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006950 {
6951 if( ssl->client_auth == 0 )
6952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006954 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6955 {
6956 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6957 }
6958 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6959 {
6960 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6961 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006962 else
6963 {
6964 ssl->state = MBEDTLS_SSL_INVALID;
6965 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006966 return( 0 );
6967 }
6968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006970 /*
6971 * If using SSLv3 and got no cert, send an Alert message
6972 * (otherwise an empty Certificate message will be sent).
6973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006975 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006976 {
6977 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6979 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6980 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006983 goto write_msg;
6984 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006986 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987#endif /* MBEDTLS_SSL_CLI_C */
6988#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006989 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6994 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006995 }
6996 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006997#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006999 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007000
7001 /*
7002 * 0 . 0 handshake type
7003 * 1 . 3 handshake length
7004 * 4 . 6 length of all certs
7005 * 7 . 9 length of cert. 1
7006 * 10 . n-1 peer certificate
7007 * n . n+2 length of cert. 2
7008 * n+3 . ... upper level cert, etc.
7009 */
7010 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007012
Paul Bakker29087132010-03-21 21:03:34 +00007013 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007014 {
7015 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10007016 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00007017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10007019 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007021 }
7022
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007023 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007024
Teppo Järvelin91d79382019-10-02 09:09:31 +03007025 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007026 i += n; crt = crt->next;
7027 }
7028
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007029 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007030
7031 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7033 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007034
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007035#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007036write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007037#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007038
Jarno Lamsa2b205162019-11-12 15:36:21 +02007039 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7040 {
7041 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7042 }
7043 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7044 {
7045 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7046 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007047 else
7048 {
7049 ssl->state = MBEDTLS_SSL_INVALID;
7050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007051
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007052 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007053 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007055 return( ret );
7056 }
7057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007059
Paul Bakkered27a042013-04-18 22:46:23 +02007060 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007061}
7062
Hanno Becker285ff0c2019-01-31 07:44:03 +00007063#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007064
7065#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007066static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7067 unsigned char *crt_buf,
7068 size_t crt_buf_len )
7069{
7070 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7071
7072 if( peer_crt == NULL )
7073 return( -1 );
7074
7075 if( peer_crt->raw.len != crt_buf_len )
7076 return( -1 );
7077
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007078 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007079}
Hanno Becker5882dd02019-06-06 16:25:57 +01007080#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007081static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7082 unsigned char *crt_buf,
7083 size_t crt_buf_len )
7084{
7085 int ret;
7086 unsigned char const * const peer_cert_digest =
7087 ssl->session->peer_cert_digest;
7088 mbedtls_md_type_t const peer_cert_digest_type =
7089 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007090 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007091 mbedtls_md_info_from_type( peer_cert_digest_type );
7092 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7093 size_t digest_len;
7094
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007095 if( peer_cert_digest == NULL ||
7096 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7097 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007098 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007099 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007100
7101 digest_len = mbedtls_md_get_size( digest_info );
7102 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7103 return( -1 );
7104
7105 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7106 if( ret != 0 )
7107 return( -1 );
7108
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007109 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007110}
Hanno Becker5882dd02019-06-06 16:25:57 +01007111#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007112#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007113
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007114/*
7115 * Once the certificate message is read, parse it into a cert chain and
7116 * perform basic checks, but leave actual verification to the caller
7117 */
Hanno Becker35e41772019-02-05 15:37:23 +00007118static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7119 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007120{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007121 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007122#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7123 int crt_cnt=0;
7124#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007125 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007126 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007131 mbedtls_ssl_pend_fatal_alert( ssl,
7132 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007134 }
7135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7137 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007140 mbedtls_ssl_pend_fatal_alert( ssl,
7141 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007143 }
7144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007146
Paul Bakker5121ce52009-01-03 21:22:43 +00007147 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007149 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007150 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007151
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007152 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007156 mbedtls_ssl_pend_fatal_alert( ssl,
7157 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007159 }
7160
Hanno Becker33c3dc82019-01-30 14:46:46 +00007161 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7162 i += 3;
7163
Hanno Becker33c3dc82019-01-30 14:46:46 +00007164 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007165 while( i < ssl->in_hslen )
7166 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007167 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007168 if ( i + 3 > ssl->in_hslen ) {
7169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007170 mbedtls_ssl_pend_fatal_alert( ssl,
7171 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007172 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7173 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007174 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7175 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007176 if( ssl->in_msg[i] != 0 )
7177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007179 mbedtls_ssl_pend_fatal_alert( ssl,
7180 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007182 }
7183
Hanno Becker33c3dc82019-01-30 14:46:46 +00007184 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007185 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007186 i += 3;
7187
7188 if( n < 128 || i + n > ssl->in_hslen )
7189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007191 mbedtls_ssl_pend_fatal_alert( ssl,
7192 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007194 }
7195
Hanno Becker33c3dc82019-01-30 14:46:46 +00007196 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007197#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7198 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007199 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7200 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007201 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007202 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007203 /* During client-side renegotiation, check that the server's
7204 * end-CRTs hasn't changed compared to the initial handshake,
7205 * mitigating the triple handshake attack. On success, reuse
7206 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7208 if( ssl_check_peer_crt_unchanged( ssl,
7209 &ssl->in_msg[i],
7210 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007211 {
Hanno Becker35e41772019-02-05 15:37:23 +00007212 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007213 mbedtls_ssl_pend_fatal_alert( ssl,
7214 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007215 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007216 }
Hanno Becker35e41772019-02-05 15:37:23 +00007217
7218 /* Now we can safely free the original chain. */
7219 ssl_clear_peer_cert( ssl->session );
7220 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007221#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7222
Hanno Becker33c3dc82019-01-30 14:46:46 +00007223 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007224#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007225 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007226#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007227 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007228 * it in-place from the input buffer instead of making a copy. */
7229 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7230#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007231 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007232 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007233 case 0: /*ok*/
7234 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7235 /* Ignore certificate with an unknown algorithm: maybe a
7236 prior certificate was already trusted. */
7237 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007238
Hanno Becker33c3dc82019-01-30 14:46:46 +00007239 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7240 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7241 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007242
Hanno Becker33c3dc82019-01-30 14:46:46 +00007243 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7244 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7245 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007246
Hanno Becker33c3dc82019-01-30 14:46:46 +00007247 default:
7248 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7249 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007250 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007251 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7252 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007253 }
7254
7255 i += n;
7256 }
7257
Hanno Becker35e41772019-02-05 15:37:23 +00007258 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007259 return( 0 );
7260}
7261
Hanno Beckerb8a08572019-02-05 12:49:06 +00007262#if defined(MBEDTLS_SSL_SRV_C)
7263static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7264{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007265 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007266 return( -1 );
7267
7268#if defined(MBEDTLS_SSL_PROTO_SSL3)
7269 /*
7270 * Check if the client sent an empty certificate
7271 */
Hanno Becker2881d802019-05-22 14:44:53 +01007272 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007273 {
7274 if( ssl->in_msglen == 2 &&
7275 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7276 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7277 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7278 {
7279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7280 return( 0 );
7281 }
7282
7283 return( -1 );
7284 }
7285#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7286
7287#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7288 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7289 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7290 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7291 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007292 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 +00007293 {
7294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7295 return( 0 );
7296 }
7297
Hanno Beckerb8a08572019-02-05 12:49:06 +00007298#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7299 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007300
7301 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007302}
7303#endif /* MBEDTLS_SSL_SRV_C */
7304
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007305/* Check if a certificate message is expected.
7306 * Return either
7307 * - SSL_CERTIFICATE_EXPECTED, or
7308 * - SSL_CERTIFICATE_SKIP
7309 * indicating whether a Certificate message is expected or not.
7310 */
7311#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007312#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007313static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7314 int authmode )
7315{
Hanno Becker473f98f2019-06-26 10:27:32 +01007316 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007317 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007318
7319 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7320 return( SSL_CERTIFICATE_SKIP );
7321
7322#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007323 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007324 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007325 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7326 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7327 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007328 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007329 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007330
7331 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7332 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007333 ssl->session_negotiate->verify_result =
7334 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7335 return( SSL_CERTIFICATE_SKIP );
7336 }
7337 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007338#else
7339 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007340#endif /* MBEDTLS_SSL_SRV_C */
7341
7342 return( SSL_CERTIFICATE_EXPECTED );
7343}
7344
Hanno Becker3cf50612019-02-05 14:36:34 +00007345static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007346 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007347 mbedtls_x509_crt *chain,
7348 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007349{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007350 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007351 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007352 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007353 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007354 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007355 mbedtls_x509_crt *ca_chain;
7356 mbedtls_x509_crl *ca_crl;
7357
7358 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007359 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007360 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007361 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007362
Jarno Lamsae1621d42019-12-19 08:58:56 +02007363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007364#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7365 if( ssl->handshake->sni_ca_chain != NULL )
7366 {
7367 ca_chain = ssl->handshake->sni_ca_chain;
7368 ca_crl = ssl->handshake->sni_ca_crl;
7369 }
7370 else
7371#endif
7372 {
7373 ca_chain = ssl->conf->ca_chain;
7374 ca_crl = ssl->conf->ca_crl;
7375 }
7376
7377 /*
7378 * Main check: verify certificate
7379 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007380 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007381 chain,
7382 ca_chain, ca_crl,
7383 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007384#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007385 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007386#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007387 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007388#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7389 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7390#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7391 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007392
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007393 if( verify_ret == 0 )
7394 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007395 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007396 if( verify_ret == 0 )
7397 {
7398 flow_counter++;
7399 }
7400 else
7401 {
7402 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7403 }
7404 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007405 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007406 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007407 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007408 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007409 }
7410
7411#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007412 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007413 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7414#endif
7415
7416 /*
7417 * Secondary checks: always done, but change 'ret' only if it was 0
7418 */
7419
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007420#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007421 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007422#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007423 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007424#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007425 mbedtls_pk_context *pk;
7426 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7427 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007428 {
7429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007430 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007431 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007432
7433 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007434 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007435 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007436 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007437 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007438
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007439 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007440#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007441
7442 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007443 {
7444 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7445
7446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007447 if( verify_ret == 0 )
7448 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007449 flow_counter++;
7450 }
7451 if( ret == 0 )
7452 {
7453 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007454 }
7455 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007456#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007457
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007458 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007459 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007460 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7461 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007462 &ssl->session_negotiate->verify_result );
7463 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007464 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007465 flow_counter++;
7466 }
7467 else
7468 {
7469 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007471 if( verify_ret == 0 )
7472 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007473 }
7474
7475 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7476 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7477 * with details encoded in the verification flags. All other kinds
7478 * of error codes, including those from the user provided f_vrfy
7479 * functions, are treated as fatal and lead to a failure of
7480 * ssl_parse_certificate even if verification was optional. */
7481 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007482 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7483 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007484 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007485 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007486 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7487 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7488 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7489 {
7490 verify_ret = 0;
7491 flow_counter++;
7492 }
7493 else
7494 {
7495 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7496 }
7497 } else {
7498 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007499 }
7500
7501 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7502 {
7503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007504 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007505 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007506 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007507 else
7508 {
7509 flow_counter++;
7510 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007511
Hanno Becker8c13ee62019-02-26 16:48:17 +00007512 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007513 {
7514 uint8_t alert;
7515
7516 /* The certificate may have been rejected for several reasons.
7517 Pick one and send the corresponding alert. Which alert to send
7518 may be a subject of debate in some cases. */
7519 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7520 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7521 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7522 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7523 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7524 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7525 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7526 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7527 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7528 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7529 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7530 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7531 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7532 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7533 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7534 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7535 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7536 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7537 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7538 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7539 else
7540 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007541 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007542 }
7543
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007544 if( verify_ret == 0 &&
7545#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7546 flow_counter == 5 )
7547#else
7548 flow_counter == 4 )
7549#endif
7550 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007551 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007552 if( verify_ret == 0 &&
7553#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7554 flow_counter == 5 )
7555#else
7556 flow_counter == 4 )
7557#endif
7558 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007560 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7561 }
7562 else
7563 {
7564 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7565 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007566 } else {
7567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007568 }
7569
Hanno Becker3cf50612019-02-05 14:36:34 +00007570#if defined(MBEDTLS_DEBUG_C)
7571 if( ssl->session_negotiate->verify_result != 0 )
7572 {
7573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7574 ssl->session_negotiate->verify_result ) );
7575 }
7576 else
7577 {
7578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7579 }
7580#endif /* MBEDTLS_DEBUG_C */
7581
Hanno Becker8c13ee62019-02-26 16:48:17 +00007582 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007583}
7584
Hanno Becker34106f62019-02-08 14:59:05 +00007585#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007586
7587#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007588static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7589 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007590{
7591 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007592 /* Remember digest of the peer's end-CRT. */
7593 ssl->session_negotiate->peer_cert_digest =
7594 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7595 if( ssl->session_negotiate->peer_cert_digest == NULL )
7596 {
7597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7598 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007599 mbedtls_ssl_pend_fatal_alert( ssl,
7600 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007601
7602 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7603 }
7604
7605 ret = mbedtls_md( mbedtls_md_info_from_type(
7606 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7607 start, len,
7608 ssl->session_negotiate->peer_cert_digest );
7609
7610 ssl->session_negotiate->peer_cert_digest_type =
7611 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7612 ssl->session_negotiate->peer_cert_digest_len =
7613 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7614
7615 return( ret );
7616}
Hanno Becker5882dd02019-06-06 16:25:57 +01007617#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007618
7619static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7620 unsigned char *start, size_t len )
7621{
7622 unsigned char *end = start + len;
7623 int ret;
7624
7625 /* Make a copy of the peer's raw public key. */
7626 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7627 ret = mbedtls_pk_parse_subpubkey( &start, end,
7628 &ssl->handshake->peer_pubkey );
7629 if( ret != 0 )
7630 {
7631 /* We should have parsed the public key before. */
7632 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7633 }
7634
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007635 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007636 return( 0 );
7637}
7638#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7639
Hanno Becker3cf50612019-02-05 14:36:34 +00007640int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7641{
7642 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007643 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007644#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7645 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7646 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007647 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007648#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007649 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007650#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007651 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007652 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007653
7654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7655
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007656 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7657 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007658 {
7659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007660 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007661 }
7662
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007663#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7664 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007665 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007666 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007667 chain = ssl->handshake->ecrs_peer_cert;
7668 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007669 goto crt_verify;
7670 }
7671#endif
7672
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007673 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007674 {
7675 /* mbedtls_ssl_read_record may have sent an alert already. We
7676 let it decide whether to alert. */
7677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007678 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007679 }
7680
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007681#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007682 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7683 {
7684 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007685
Hanno Beckerb8a08572019-02-05 12:49:06 +00007686 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007687 ret = 0;
7688 else
7689 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007690
Hanno Becker613d4902019-02-05 13:11:17 +00007691 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007692 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007693#endif /* MBEDTLS_SSL_SRV_C */
7694
Hanno Becker35e41772019-02-05 15:37:23 +00007695 /* Clear existing peer CRT structure in case we tried to
7696 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007697 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007698
7699 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7700 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007701 {
7702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7703 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007704 mbedtls_ssl_pend_fatal_alert( ssl,
7705 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007706
Hanno Beckere4aeb762019-02-05 17:19:52 +00007707 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7708 goto exit;
7709 }
7710 mbedtls_x509_crt_init( chain );
7711
7712 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007713 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007714 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007715
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007716#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7717 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007718 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007719
7720crt_verify:
7721 if( ssl->handshake->ecrs_enabled)
7722 rs_ctx = &ssl->handshake->ecrs_ctx;
7723#endif
7724
Hanno Becker3cf50612019-02-05 14:36:34 +00007725 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007726 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007727 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007728 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007729
Hanno Becker3008d282019-02-05 17:02:28 +00007730#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007731 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007732 size_t pk_len;
7733 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007734
Hanno Becker34106f62019-02-08 14:59:05 +00007735 /* We parse the CRT chain without copying, so
7736 * these pointers point into the input buffer,
7737 * and are hence still valid after freeing the
7738 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007739
Hanno Becker5882dd02019-06-06 16:25:57 +01007740#if defined(MBEDTLS_SSL_RENEGOTIATION)
7741 unsigned char *crt_start;
7742 size_t crt_len;
7743
Hanno Becker34106f62019-02-08 14:59:05 +00007744 crt_start = chain->raw.p;
7745 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007746#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007747
Hanno Becker8c13ee62019-02-26 16:48:17 +00007748 pk_start = chain->cache->pk_raw.p;
7749 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007750
7751 /* Free the CRT structures before computing
7752 * digest and copying the peer's public key. */
7753 mbedtls_x509_crt_free( chain );
7754 mbedtls_free( chain );
7755 chain = NULL;
7756
Hanno Becker5882dd02019-06-06 16:25:57 +01007757#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007758 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007759 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007760 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007761#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007762
Hanno Becker34106f62019-02-08 14:59:05 +00007763 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007764 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007765 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007766 }
Hanno Becker34106f62019-02-08 14:59:05 +00007767#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7768 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007769 ssl->session_negotiate->peer_cert = chain;
7770 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007771#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007774
Hanno Becker613d4902019-02-05 13:11:17 +00007775exit:
7776
Hanno Beckere4aeb762019-02-05 17:19:52 +00007777 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007778 {
7779 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7780 {
7781 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7782 }
7783 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7784 {
7785 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7786 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007787 else
7788 {
7789 ssl->state = MBEDTLS_SSL_INVALID;
7790 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007791 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007792
7793#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7794 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7795 {
7796 ssl->handshake->ecrs_peer_cert = chain;
7797 chain = NULL;
7798 }
7799#endif
7800
7801 if( chain != NULL )
7802 {
7803 mbedtls_x509_crt_free( chain );
7804 mbedtls_free( chain );
7805 }
7806
Paul Bakker5121ce52009-01-03 21:22:43 +00007807 return( ret );
7808}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007809#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007812{
7813 int ret;
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007818 ssl->out_msglen = 1;
7819 ssl->out_msg[0] = 1;
7820
Jarno Lamsa2b205162019-11-12 15:36:21 +02007821 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7822 {
7823 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7824 }
7825 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7826 {
7827 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7828 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007829 else
7830 {
7831 ssl->state = MBEDTLS_SSL_INVALID;
7832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007833
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007834 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007835 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007837 return( ret );
7838 }
7839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007841
7842 return( 0 );
7843}
7844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007846{
7847 int ret;
7848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007850
Hanno Becker327c93b2018-08-15 13:56:18 +01007851 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007854 return( ret );
7855 }
7856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007860 mbedtls_ssl_pend_fatal_alert( ssl,
7861 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007863 }
7864
Hanno Beckere678eaa2018-08-21 14:57:46 +01007865 /* CCS records are only accepted if they have length 1 and content '1',
7866 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007867
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007868 /*
7869 * Switch to our negotiated transform and session parameters for inbound
7870 * data.
7871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007873 ssl->transform_in = ssl->transform_negotiate;
7874 ssl->session_in = ssl->session_negotiate;
7875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007877 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007880 ssl_dtls_replay_reset( ssl );
7881#endif
7882
7883 /* Increment epoch */
7884 if( ++ssl->in_epoch == 0 )
7885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007887 /* This is highly unlikely to happen for legitimate reasons, so
7888 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007890 }
7891 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007892 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007894#if defined(MBEDTLS_SSL_PROTO_TLS)
7895 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007896 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007897 }
7898#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007899
Hanno Beckerf5970a02019-05-08 09:38:41 +01007900 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7903 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007908 mbedtls_ssl_pend_fatal_alert( ssl,
7909 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007911 }
7912 }
7913#endif
7914
Jarno Lamsa2b205162019-11-12 15:36:21 +02007915 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7916 {
7917 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7918 }
7919 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7920 {
7921 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7922 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007923 else
7924 {
7925 ssl->state = MBEDTLS_SSL_INVALID;
7926 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007929
7930 return( 0 );
7931}
7932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007934{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7936 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007937 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007938 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7941#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007942 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007945 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007948}
7949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007951{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007953
7954 /*
7955 * Free our handshake params
7956 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007957 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007959 ssl->handshake = NULL;
7960
7961 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007962 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007963 */
7964 if( ssl->transform )
7965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 mbedtls_ssl_transform_free( ssl->transform );
7967 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007968 }
7969 ssl->transform = ssl->transform_negotiate;
7970 ssl->transform_negotiate = NULL;
7971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007973}
7974
Jarno Lamsae1621d42019-12-19 08:58:56 +02007975int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007976{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007977 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007978
7979#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007980 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007981 ? ssl->handshake->sni_authmode
7982 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7983#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007984 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007985#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007986#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7987 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7988 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7989#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992#if defined(MBEDTLS_SSL_RENEGOTIATION)
7993 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007996 ssl->renego_records_seen = 0;
7997 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007998#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007999
8000 /*
8001 * Free the previous session and switch in the current one
8002 */
Paul Bakker0a597072012-09-25 21:55:46 +00008003 if( ssl->session )
8004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01008006 /* RFC 7366 3.1: keep the EtM state */
8007 ssl->session_negotiate->encrypt_then_mac =
8008 ssl->session->encrypt_then_mac;
8009#endif
8010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 mbedtls_ssl_session_free( ssl->session );
8012 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00008013 }
8014 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00008015 ssl->session_negotiate = NULL;
8016
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008017#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00008018 /*
8019 * Add cache entry
8020 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008021 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02008022 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008023 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008024 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008025 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008027 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008028#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008029
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008030 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8031 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8032#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8033 crt_expected == SSL_CERTIFICATE_SKIP )
8034#else
8035 1 )
8036#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008037 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008038 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008039 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8040 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8041#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8042 crt_expected == SSL_CERTIFICATE_SKIP )
8043#else
8044 1 )
8045#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008046 {
8047 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8048 }
8049 else
8050 {
8051 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8052 goto cleanup;
8053 }
8054 }
8055
Jarno Lamsae1621d42019-12-19 08:58:56 +02008056#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008057 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008058 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008059 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008060 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008061 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008062 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008063 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008064 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008065 }
8066 else
8067 {
8068 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008069 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008070 }
8071 }
8072#endif
8073
8074 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8075 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008076 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008077 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8078 {
8079 ret = 0;
8080 }
8081 else
8082 {
8083 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008084 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008085 }
8086 }
8087 else
8088 {
8089 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008090 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008091 }
8092
Jarno Lamsa06164052019-12-19 14:40:36 +02008093 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8094 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8095 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8096 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008097 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008098 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8099 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8100 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8101 {
8102 ret = 0;
8103 }
8104 else
8105 {
8106 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8107 goto cleanup;
8108 }
8109 }
8110 else
8111 {
8112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8114 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8115 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8116 }
8117
8118cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008120 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008121 ssl->handshake->flight != NULL )
8122 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008123 /* Cancel handshake timer */
8124 ssl_set_timer( ssl, 0 );
8125
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008126 /* Keep last flight around in case we need to resend it:
8127 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008128 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008129 }
8130 else
8131#endif
8132 ssl_handshake_wrapup_free_hs_transform( ssl );
8133
Jarno Lamsa2b205162019-11-12 15:36:21 +02008134 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008137 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008138}
8139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008140int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008141{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008142 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008145
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008146 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008147
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008148 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8149 mbedtls_ssl_suite_get_mac(
8150 mbedtls_ssl_ciphersuite_from_id(
8151 mbedtls_ssl_session_get_ciphersuite(
8152 ssl->session_negotiate ) ) ),
8153 ssl, ssl->out_msg + 4,
8154 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008155
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008156 /*
8157 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8158 * may define some other value. Currently (early 2016), no defined
8159 * ciphersuite does this (and this is unlikely to change as activity has
8160 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8161 */
Hanno Becker2881d802019-05-22 14:44:53 +01008162 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008165 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008166 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008167#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008168
Paul Bakker5121ce52009-01-03 21:22:43 +00008169 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8171 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008172
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008173#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008174 /*
8175 * In case of session resuming, invert the client and server
8176 * ChangeCipherSpec messages order.
8177 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008178 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008181 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8182 MBEDTLS_SSL_IS_CLIENT )
8183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008185 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008188 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8189 MBEDTLS_SSL_IS_SERVER )
8190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008191 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008192 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008193#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008194 }
8195 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008196#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008197 {
8198 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8199 {
8200 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8201 }
8202 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8203 {
8204 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8205 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008206 else
8207 {
8208 ssl->state = MBEDTLS_SSL_INVALID;
8209 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008211
Paul Bakker48916f92012-09-16 19:57:18 +00008212 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008213 * Switch to our negotiated transform and session parameters for outbound
8214 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008219 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008220 {
8221 unsigned char i;
8222
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008223 /* Remember current epoch settings for resending */
8224 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008225 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008226
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008227 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008228 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008229
8230 /* Increment epoch */
8231 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008232 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008233 break;
8234
8235 /* The loop goes to its end iff the counter is wrapping */
8236 if( i == 0 )
8237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8239 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008240 }
8241 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008242 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008243#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008244#if defined(MBEDTLS_SSL_PROTO_TLS)
8245 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008246 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008247 }
8248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008249
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008250 ssl->transform_out = ssl->transform_negotiate;
8251 ssl->session_out = ssl->session_negotiate;
8252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008253#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8254 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8259 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008260 }
8261 }
8262#endif
8263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008265 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008267#endif
8268
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008269 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008270 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008272 return( ret );
8273 }
8274
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008276 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008277 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8278 {
8279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8280 return( ret );
8281 }
8282#endif
8283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008285
8286 return( 0 );
8287}
8288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008290#define SSL_MAX_HASH_LEN 36
8291#else
8292#define SSL_MAX_HASH_LEN 12
8293#endif
8294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008296{
Paul Bakker23986e52011-04-24 08:57:21 +00008297 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008298 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008299 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008302
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008303 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8304 mbedtls_ssl_suite_get_mac(
8305 mbedtls_ssl_ciphersuite_from_id(
8306 mbedtls_ssl_session_get_ciphersuite(
8307 ssl->session_negotiate ) ) ),
8308 ssl, buf,
8309 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008310
Hanno Becker327c93b2018-08-15 13:56:18 +01008311 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008314 return( ret );
8315 }
8316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008320 mbedtls_ssl_pend_fatal_alert( ssl,
8321 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008323 }
8324
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008325 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008327 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008328 hash_len = 36;
8329 else
8330#endif
8331 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8334 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008337 mbedtls_ssl_pend_fatal_alert( ssl,
8338 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008340 }
8341
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008342 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008343 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008346 mbedtls_ssl_pend_fatal_alert( ssl,
8347 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008349 }
8350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008352 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008353 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008354#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008355
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008356#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008357 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008360 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008362#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008364 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008366#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008367 }
8368 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008369#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008370 {
8371 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8372 {
8373 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8374 }
8375 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8376 {
8377 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8378 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008379 else
8380 {
8381 ssl->state = MBEDTLS_SSL_INVALID;
8382 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008383 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008386 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008388#endif
8389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008391
8392 return( 0 );
8393}
8394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008397 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8400 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8401 mbedtls_md5_init( &handshake->fin_md5 );
8402 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008403 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8404 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008405#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8407#if defined(MBEDTLS_SHA256_C)
8408 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008409 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411#if defined(MBEDTLS_SHA512_C)
8412 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008413 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008414#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008416
Hanno Becker7e5437a2017-04-28 17:15:26 +01008417#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8418 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8419 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8420#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_DHM_C)
8423 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008424#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425#if defined(MBEDTLS_ECDH_C)
8426 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008427#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008428#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008429 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008430#if defined(MBEDTLS_SSL_CLI_C)
8431 handshake->ecjpake_cache = NULL;
8432 handshake->ecjpake_cache_len = 0;
8433#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008434#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008435
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008436#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008437 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008438#endif
8439
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008440#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8441 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8442#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008443
8444#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8445 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8446 mbedtls_pk_init( &handshake->peer_pubkey );
8447#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008448}
8449
Hanno Becker611a83b2018-01-03 14:27:32 +00008450void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008451{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8455 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008456
Hanno Becker92231322018-01-03 15:32:51 +00008457#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 mbedtls_md_init( &transform->md_ctx_enc );
8459 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008460#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008461}
8462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008464{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008466}
8467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008469{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008470 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008471 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008473 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008475 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008476 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008477
8478 /*
8479 * Either the pointers are now NULL or cleared properly and can be freed.
8480 * Now allocate missing structures.
8481 */
8482 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008483 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008484 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008485 }
Paul Bakker48916f92012-09-16 19:57:18 +00008486
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008487 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008488 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008489 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008490 }
Paul Bakker48916f92012-09-16 19:57:18 +00008491
Paul Bakker82788fb2014-10-20 13:59:19 +02008492 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008493 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008494 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008495 }
Paul Bakker48916f92012-09-16 19:57:18 +00008496
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008497 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008498 if( ssl->handshake == NULL ||
8499 ssl->transform_negotiate == NULL ||
8500 ssl->session_negotiate == NULL )
8501 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504 mbedtls_free( ssl->handshake );
8505 mbedtls_free( ssl->transform_negotiate );
8506 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008507
8508 ssl->handshake = NULL;
8509 ssl->transform_negotiate = NULL;
8510 ssl->session_negotiate = NULL;
8511
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008512 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008513 }
8514
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008515 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008517 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008518 ssl_handshake_params_init( ssl->handshake );
8519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008521 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008522 {
8523 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008524
Hanno Becker2d9623f2019-06-13 12:07:05 +01008525 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008526 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8527 else
8528 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8529 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008530#endif
8531
Paul Bakker48916f92012-09-16 19:57:18 +00008532 return( 0 );
8533}
8534
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008535#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008536/* Dummy cookie callbacks for defaults */
8537static int ssl_cookie_write_dummy( void *ctx,
8538 unsigned char **p, unsigned char *end,
8539 const unsigned char *cli_id, size_t cli_id_len )
8540{
8541 ((void) ctx);
8542 ((void) p);
8543 ((void) end);
8544 ((void) cli_id);
8545 ((void) cli_id_len);
8546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008548}
8549
8550static int ssl_cookie_check_dummy( void *ctx,
8551 const unsigned char *cookie, size_t cookie_len,
8552 const unsigned char *cli_id, size_t cli_id_len )
8553{
8554 ((void) ctx);
8555 ((void) cookie);
8556 ((void) cookie_len);
8557 ((void) cli_id);
8558 ((void) cli_id_len);
8559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008560 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008561}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008562#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008563
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008564/* Once ssl->out_hdr as the address of the beginning of the
8565 * next outgoing record is set, deduce the other pointers.
8566 *
8567 * Note: For TLS, we save the implicit record sequence number
8568 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8569 * and the caller has to make sure there's space for this.
8570 */
8571
8572static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8573 mbedtls_ssl_transform *transform )
8574{
8575#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008576 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008577 {
8578 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008579#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008580 ssl->out_cid = ssl->out_ctr + 8;
8581 ssl->out_len = ssl->out_cid;
8582 if( transform != NULL )
8583 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008584#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008585 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008586#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008587 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008588 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008589 MBEDTLS_SSL_TRANSPORT_ELSE
8590#endif /* MBEDTLS_SSL_PROTO_DTLS */
8591#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008592 {
8593 ssl->out_ctr = ssl->out_hdr - 8;
8594 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008595#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008596 ssl->out_cid = ssl->out_len;
8597#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008598 ssl->out_iv = ssl->out_hdr + 5;
8599 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008600#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008601
8602 /* Adjust out_msg to make space for explicit IV, if used. */
8603 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008604 mbedtls_ssl_ver_geq(
8605 mbedtls_ssl_get_minor_ver( ssl ),
8606 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008607 {
8608 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8609 }
8610 else
8611 ssl->out_msg = ssl->out_iv;
8612}
8613
8614/* Once ssl->in_hdr as the address of the beginning of the
8615 * next incoming record is set, deduce the other pointers.
8616 *
8617 * Note: For TLS, we save the implicit record sequence number
8618 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8619 * and the caller has to make sure there's space for this.
8620 */
8621
Hanno Beckerf5970a02019-05-08 09:38:41 +01008622static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008623{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008624 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008625 * of unprotected TLS/DTLS records, with ssl->in_msg
8626 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008627 *
8628 * When decrypting a protected record, ssl->in_msg
8629 * will be shifted to point to the beginning of the
8630 * record plaintext.
8631 */
8632
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008634 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008635 {
Hanno Becker70e79282019-05-03 14:34:53 +01008636 /* This sets the header pointers to match records
8637 * without CID. When we receive a record containing
8638 * a CID, the fields are shifted accordingly in
8639 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008640 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008641#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008642 ssl->in_cid = ssl->in_ctr + 8;
8643 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008644#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008645 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008646#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008647 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008648 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008649 MBEDTLS_SSL_TRANSPORT_ELSE
8650#endif /* MBEDTLS_SSL_PROTO_DTLS */
8651#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008652 {
8653 ssl->in_ctr = ssl->in_hdr - 8;
8654 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008655#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008656 ssl->in_cid = ssl->in_len;
8657#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008658 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008659 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008660#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008661}
8662
Paul Bakker5121ce52009-01-03 21:22:43 +00008663/*
8664 * Initialize an SSL context
8665 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008666void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8667{
8668 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8669}
8670
8671/*
8672 * Setup an SSL context
8673 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008674
8675static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8676{
8677 /* Set the incoming and outgoing record pointers. */
8678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008679 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008680 {
8681 ssl->out_hdr = ssl->out_buf;
8682 ssl->in_hdr = ssl->in_buf;
8683 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008684 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008685#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008686#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008687 {
8688 ssl->out_hdr = ssl->out_buf + 8;
8689 ssl->in_hdr = ssl->in_buf + 8;
8690 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008691#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008692
8693 /* Derive other internal pointers. */
8694 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008695 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008696}
8697
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008698int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008699 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008700{
Paul Bakker48916f92012-09-16 19:57:18 +00008701 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008702
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008703 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008704
Hanno Beckeref982d52019-07-23 15:56:18 +01008705#if defined(MBEDTLS_USE_TINYCRYPT)
8706 uECC_set_rng( &uecc_rng_wrapper );
8707#endif
8708
Paul Bakker62f2dee2012-09-28 07:31:51 +00008709 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008710 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008711 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008712
8713 /* Set to NULL in case of an error condition */
8714 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008715
Angus Grattond8213d02016-05-25 20:56:48 +10008716 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8717 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008718 {
Angus Grattond8213d02016-05-25 20:56:48 +10008719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008720 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008721 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008722 }
8723
8724 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8725 if( ssl->out_buf == NULL )
8726 {
8727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008728 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008729 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008730 }
8731
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008732 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008733
Paul Bakker48916f92012-09-16 19:57:18 +00008734 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008735 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008736
Hanno Beckerc8f52992019-07-25 11:15:08 +01008737 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008738
Paul Bakker5121ce52009-01-03 21:22:43 +00008739 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008740
8741error:
8742 mbedtls_free( ssl->in_buf );
8743 mbedtls_free( ssl->out_buf );
8744
8745 ssl->conf = NULL;
8746
8747 ssl->in_buf = NULL;
8748 ssl->out_buf = NULL;
8749
8750 ssl->in_hdr = NULL;
8751 ssl->in_ctr = NULL;
8752 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008753 ssl->in_msg = NULL;
8754
8755 ssl->out_hdr = NULL;
8756 ssl->out_ctr = NULL;
8757 ssl->out_len = NULL;
8758 ssl->out_iv = NULL;
8759 ssl->out_msg = NULL;
8760
k-stachowiak9f7798e2018-07-31 16:52:32 +02008761 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008762}
8763
8764/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008765 * Reset an initialized and used SSL context for re-use while retaining
8766 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008767 *
8768 * If partial is non-zero, keep data in the input buffer and client ID.
8769 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008770 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008771static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008772{
Paul Bakker48916f92012-09-16 19:57:18 +00008773 int ret;
8774
Hanno Becker7e772132018-08-10 12:38:21 +01008775#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8776 !defined(MBEDTLS_SSL_SRV_C)
8777 ((void) partial);
8778#endif
8779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008781
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008782 /* Cancel any possibly running timer */
8783 ssl_set_timer( ssl, 0 );
8784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785#if defined(MBEDTLS_SSL_RENEGOTIATION)
8786 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008787 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008788
8789 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008790 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8791 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008792#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008794
Paul Bakker7eb013f2011-10-06 12:37:39 +00008795 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008796 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008797
8798 ssl->in_msgtype = 0;
8799 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008800#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008801 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008802 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008803#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008804#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008805 ssl_dtls_replay_reset( ssl );
8806#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008807
8808 ssl->in_hslen = 0;
8809 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008810
8811 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008812
8813 ssl->out_msgtype = 0;
8814 ssl->out_msglen = 0;
8815 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008816#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8817 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008818 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008819#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008820
Hanno Becker19859472018-08-06 09:40:20 +01008821 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8822
Paul Bakker48916f92012-09-16 19:57:18 +00008823 ssl->transform_in = NULL;
8824 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008825
Hanno Becker78640902018-08-13 16:35:15 +01008826 ssl->session_in = NULL;
8827 ssl->session_out = NULL;
8828
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008829 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008830
8831#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008832 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008833#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8834 {
8835 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008836 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008837 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8840 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8843 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8846 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008847 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008848 }
8849#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008850
Paul Bakker48916f92012-09-16 19:57:18 +00008851 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 mbedtls_ssl_transform_free( ssl->transform );
8854 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008855 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008856 }
Paul Bakker48916f92012-09-16 19:57:18 +00008857
Paul Bakkerc0463502013-02-14 11:19:38 +01008858 if( ssl->session )
8859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 mbedtls_ssl_session_free( ssl->session );
8861 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008862 ssl->session = NULL;
8863 }
8864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008866 ssl->alpn_chosen = NULL;
8867#endif
8868
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008869#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008870#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008871 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008872#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008873 {
8874 mbedtls_free( ssl->cli_id );
8875 ssl->cli_id = NULL;
8876 ssl->cli_id_len = 0;
8877 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008878#endif
8879
Paul Bakker48916f92012-09-16 19:57:18 +00008880 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8881 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008882
8883 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008884}
8885
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008886/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008887 * Reset an initialized and used SSL context for re-use while retaining
8888 * all application-set variables, function pointers and data.
8889 */
8890int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8891{
8892 return( ssl_session_reset_int( ssl, 0 ) );
8893}
8894
8895/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008896 * SSL set accessors
8897 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008898#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008899void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008900{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008901 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008902}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008903#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008904
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008905void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008906{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008907 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008908}
8909
Hanno Becker7f376f42019-06-12 16:20:48 +01008910#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8911 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008912void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008913{
Hanno Becker7f376f42019-06-12 16:20:48 +01008914 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008915}
Hanno Becker7f376f42019-06-12 16:20:48 +01008916#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008917
Hanno Beckerde671542019-06-12 16:30:46 +01008918#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8919 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8920void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8921 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008922{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008923 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008924}
Hanno Beckerde671542019-06-12 16:30:46 +01008925#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008928
Hanno Becker1841b0a2018-08-24 11:13:57 +01008929void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8930 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008931{
8932 ssl->disable_datagram_packing = !allow_packing;
8933}
8934
Hanno Becker1f835fa2019-06-13 10:14:59 +01008935#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8936 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008937void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8938 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008939{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008940 conf->hs_timeout_min = min;
8941 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008942}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008943#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8944 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8945void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8946 uint32_t min, uint32_t max )
8947{
8948 ((void) conf);
8949 ((void) min);
8950 ((void) max);
8951}
8952#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8953 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8954
8955#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008956
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008957void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008958{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008959#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8960 conf->authmode = authmode;
8961#else
8962 ((void) conf);
8963 ((void) authmode);
8964#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008965}
8966
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008967#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8968 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008969void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008970 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008971 void *p_vrfy )
8972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008973 conf->f_vrfy = f_vrfy;
8974 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008975}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008976#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008977
Hanno Beckerece325c2019-06-13 15:39:27 +01008978#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008979void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008980 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008981 void *p_rng )
8982{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008983 conf->f_rng = f_rng;
8984 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008985}
Hanno Beckerece325c2019-06-13 15:39:27 +01008986#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008987
Hanno Becker14a4a442019-07-02 17:00:34 +01008988#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008989void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008990 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008991 void *p_dbg )
8992{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008993 conf->f_dbg = f_dbg;
8994 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008995}
Hanno Becker14a4a442019-07-02 17:00:34 +01008996#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008997
Hanno Beckera58a8962019-06-13 16:11:15 +01008998#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8999 !defined(MBEDTLS_SSL_CONF_SEND) && \
9000 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009002 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00009003 mbedtls_ssl_send_t *f_send,
9004 mbedtls_ssl_recv_t *f_recv,
9005 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009006{
Hanno Beckera58a8962019-06-13 16:11:15 +01009007 ssl->p_bio = p_bio;
9008 ssl->f_send = f_send;
9009 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009010 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009011}
Hanno Beckera58a8962019-06-13 16:11:15 +01009012#else
9013void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
9014 void *p_bio )
9015{
9016 ssl->p_bio = p_bio;
9017}
9018#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009019
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02009020#if defined(MBEDTLS_SSL_PROTO_DTLS)
9021void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
9022{
9023 ssl->mtu = mtu;
9024}
9025#endif
9026
Hanno Becker1f835fa2019-06-13 10:14:59 +01009027#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009028void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009029{
9030 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009031}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009032#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009033
Hanno Becker0ae6b242019-06-13 16:45:36 +01009034#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9035 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009036void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9037 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009038 mbedtls_ssl_set_timer_t *f_set_timer,
9039 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009040{
9041 ssl->p_timer = p_timer;
9042 ssl->f_set_timer = f_set_timer;
9043 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009044 /* Make sure we start with no timer running */
9045 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009046}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009047#else
9048void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9049 void *p_timer )
9050{
9051 ssl->p_timer = p_timer;
9052 /* Make sure we start with no timer running */
9053 ssl_set_timer( ssl, 0 );
9054}
9055#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009056
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009057#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009058void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009059 void *p_cache,
9060 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9061 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009062{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009063 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009064 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009065 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009066}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009067#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009068
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009069#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009071{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009072 int ret;
9073
9074 if( ssl == NULL ||
9075 session == NULL ||
9076 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009077 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009080 }
9081
Hanno Becker58fccf22019-02-06 14:30:46 +00009082 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9083 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009084 return( ret );
9085
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009086 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009087
9088 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009089}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009090#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009091
Hanno Becker73f4cb12019-06-27 13:51:07 +01009092#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009093void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009094 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009095{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009096 conf->ciphersuite_list[0] = ciphersuites;
9097 conf->ciphersuite_list[1] = ciphersuites;
9098 conf->ciphersuite_list[2] = ciphersuites;
9099 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009100}
9101
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009102void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009103 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009104 int major, int minor )
9105{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009106 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009107 return;
9108
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009109 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9110 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9111 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009112 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009113 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009114
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009115 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9116 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009117}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009118#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009121void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009122 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009123{
9124 conf->cert_profile = profile;
9125}
9126
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009127/* Append a new keycert entry to a (possibly empty) list */
9128static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9129 mbedtls_x509_crt *cert,
9130 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009131{
niisato8ee24222018-06-25 19:05:48 +09009132 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009133
niisato8ee24222018-06-25 19:05:48 +09009134 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9135 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009136 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009137
niisato8ee24222018-06-25 19:05:48 +09009138 new_cert->cert = cert;
9139 new_cert->key = key;
9140 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009141
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009142 /* Update head is the list was null, else add to the end */
9143 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009144 {
niisato8ee24222018-06-25 19:05:48 +09009145 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009146 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009147 else
9148 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009149 mbedtls_ssl_key_cert *cur = *head;
9150 while( cur->next != NULL )
9151 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009152 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009153 }
9154
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009155 return( 0 );
9156}
9157
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009158int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009159 mbedtls_x509_crt *own_cert,
9160 mbedtls_pk_context *pk_key )
9161{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009162 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009163}
9164
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009165void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009166 mbedtls_x509_crt *ca_chain,
9167 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009168{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009169 conf->ca_chain = ca_chain;
9170 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009171}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009172#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009173
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009174#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9175int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9176 mbedtls_x509_crt *own_cert,
9177 mbedtls_pk_context *pk_key )
9178{
9179 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9180 own_cert, pk_key ) );
9181}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009182
9183void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9184 mbedtls_x509_crt *ca_chain,
9185 mbedtls_x509_crl *ca_crl )
9186{
9187 ssl->handshake->sni_ca_chain = ca_chain;
9188 ssl->handshake->sni_ca_crl = ca_crl;
9189}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009190
9191void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9192 int authmode )
9193{
9194 ssl->handshake->sni_authmode = authmode;
9195}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009196#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9197
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009198#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009199/*
9200 * Set EC J-PAKE password for current handshake
9201 */
9202int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9203 const unsigned char *pw,
9204 size_t pw_len )
9205{
9206 mbedtls_ecjpake_role role;
9207
Janos Follath8eb64132016-06-03 15:40:57 +01009208 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9210
Hanno Becker2d9623f2019-06-13 12:07:05 +01009211 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009212 role = MBEDTLS_ECJPAKE_SERVER;
9213 else
9214 role = MBEDTLS_ECJPAKE_CLIENT;
9215
9216 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9217 role,
9218 MBEDTLS_MD_SHA256,
9219 MBEDTLS_ECP_DP_SECP256R1,
9220 pw, pw_len ) );
9221}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009222#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009225int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009226 const unsigned char *psk, size_t psk_len,
9227 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009228{
Paul Bakker6db455e2013-09-18 17:29:31 +02009229 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009232 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009234
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009235 /* Identity len will be encoded on two bytes */
9236 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009237 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009238 {
9239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9240 }
9241
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009242 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009243 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009244 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009245
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009246 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009247 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009248 conf->psk_len = 0;
9249 }
9250 if( conf->psk_identity != NULL )
9251 {
9252 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009253 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009254 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009255 }
9256
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009257 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9258 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009259 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009260 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009261 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009262 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009263 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009264 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009265 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009266
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009267 conf->psk_len = psk_len;
9268 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009269
Teppo Järvelin91d79382019-10-02 09:09:31 +03009270 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9271 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009272
9273 return( 0 );
9274}
9275
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009276int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9277 const unsigned char *psk, size_t psk_len )
9278{
9279 if( psk == NULL || ssl->handshake == NULL )
9280 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9281
9282 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9284
9285 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009286 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009287 mbedtls_platform_zeroize( ssl->handshake->psk,
9288 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009289 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009290 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009291 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009292
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009293 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009294 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009295
9296 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009297 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009298
9299 return( 0 );
9300}
9301
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009302void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009304 size_t),
9305 void *p_psk )
9306{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009307 conf->f_psk = f_psk;
9308 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009311
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009312#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009313
9314#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009315int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009316{
9317 int ret;
9318
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009319 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9320 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9321 {
9322 mbedtls_mpi_free( &conf->dhm_P );
9323 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009324 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009326
9327 return( 0 );
9328}
Hanno Becker470a8c42017-10-04 15:28:46 +01009329#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009330
Hanno Beckera90658f2017-10-04 15:29:08 +01009331int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9332 const unsigned char *dhm_P, size_t P_len,
9333 const unsigned char *dhm_G, size_t G_len )
9334{
9335 int ret;
9336
9337 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9338 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9339 {
9340 mbedtls_mpi_free( &conf->dhm_P );
9341 mbedtls_mpi_free( &conf->dhm_G );
9342 return( ret );
9343 }
9344
9345 return( 0 );
9346}
Paul Bakker5121ce52009-01-03 21:22:43 +00009347
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009348int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009349{
9350 int ret;
9351
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009352 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9353 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9354 {
9355 mbedtls_mpi_free( &conf->dhm_P );
9356 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009357 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009358 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009359
9360 return( 0 );
9361}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009362#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009363
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009364#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9365/*
9366 * Set the minimum length for Diffie-Hellman parameters
9367 */
9368void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9369 unsigned int bitlen )
9370{
9371 conf->dhm_min_bitlen = bitlen;
9372}
9373#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9374
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009375#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009376/*
9377 * Set allowed/preferred hashes for handshake signatures
9378 */
9379void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9380 const int *hashes )
9381{
Hanno Becker56595f42019-06-19 16:31:38 +01009382#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009383 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009384#else
9385 ((void) conf);
9386 ((void) hashes);
9387#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009388}
Hanno Becker947194e2017-04-07 13:25:49 +01009389#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009390
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009391#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009392#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009393/*
9394 * Set the allowed elliptic curves
9395 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009396void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009397 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009398{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009399 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009400}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009401#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009402#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009403
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009404#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009405int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009406{
Hanno Becker947194e2017-04-07 13:25:49 +01009407 /* Initialize to suppress unnecessary compiler warning */
9408 size_t hostname_len = 0;
9409
9410 /* Check if new hostname is valid before
9411 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009412 if( hostname != NULL )
9413 {
9414 hostname_len = strlen( hostname );
9415
9416 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9417 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9418 }
9419
9420 /* Now it's clear that we will overwrite the old hostname,
9421 * so we can free it safely */
9422
9423 if( ssl->hostname != NULL )
9424 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009425 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009426 mbedtls_free( ssl->hostname );
9427 }
9428
9429 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009430
Paul Bakker5121ce52009-01-03 21:22:43 +00009431 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009432 {
9433 ssl->hostname = NULL;
9434 }
9435 else
9436 {
9437 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009438 if( ssl->hostname == NULL )
9439 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009440
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009441 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9442 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009443
Hanno Becker947194e2017-04-07 13:25:49 +01009444 ssl->hostname[hostname_len] = '\0';
9445 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009446
9447 return( 0 );
9448}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009449#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009450
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009451#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009452void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009454 const unsigned char *, size_t),
9455 void *p_sni )
9456{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009457 conf->f_sni = f_sni;
9458 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009459}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009463int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009464{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009465 size_t cur_len, tot_len;
9466 const char **p;
9467
9468 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009469 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9470 * MUST NOT be truncated."
9471 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009472 */
9473 tot_len = 0;
9474 for( p = protos; *p != NULL; p++ )
9475 {
9476 cur_len = strlen( *p );
9477 tot_len += cur_len;
9478
9479 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009481 }
9482
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009483 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009484
9485 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009486}
9487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009489{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009490 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009493
Hanno Becker33b9b252019-07-05 11:23:25 +01009494#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9495 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9496void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9497 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009499 conf->max_major_ver = major;
9500 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009501}
Hanno Becker33b9b252019-07-05 11:23:25 +01009502#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9503 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009504
Hanno Becker33b9b252019-07-05 11:23:25 +01009505#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9506 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9507void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9508 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009510 conf->min_major_ver = major;
9511 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009512}
Hanno Becker33b9b252019-07-05 11:23:25 +01009513#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9514 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009517void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009518{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009519 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009520}
9521#endif
9522
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009523#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009524void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9525 char cert_req_ca_list )
9526{
9527 conf->cert_req_ca_list = cert_req_ca_list;
9528}
9529#endif
9530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009532void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009534 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009535}
9536#endif
9537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009539#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009540void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009541{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009542 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009543}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009544#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9545#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009546void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009547 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009548{
9549 conf->enforce_extended_master_secret = ems_enf;
9550}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009551#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009552#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009553
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009554#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009555void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009556{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009557 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009558}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009559#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009562int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009565 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009568 }
9569
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009570 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009571
9572 return( 0 );
9573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009577void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009578{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009579 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009584void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009585{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009586 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009587}
9588#endif
9589
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009590#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009591void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009593 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009594}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009595#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009598void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009599{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009600 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009601}
9602
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009603void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009604{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009605 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009606}
9607
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009608void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009609 const unsigned char period[8] )
9610{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009611 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009612}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009616#if defined(MBEDTLS_SSL_CLI_C)
9617void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009618{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009619 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009620}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009621#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009622
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009623#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009624void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9625 mbedtls_ssl_ticket_write_t *f_ticket_write,
9626 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9627 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009628{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009629 conf->f_ticket_write = f_ticket_write;
9630 conf->f_ticket_parse = f_ticket_parse;
9631 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009632}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009635
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009636#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9637void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9638 mbedtls_ssl_export_keys_t *f_export_keys,
9639 void *p_export_keys )
9640{
9641 conf->f_export_keys = f_export_keys;
9642 conf->p_export_keys = p_export_keys;
9643}
9644#endif
9645
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009646#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009647void mbedtls_ssl_conf_async_private_cb(
9648 mbedtls_ssl_config *conf,
9649 mbedtls_ssl_async_sign_t *f_async_sign,
9650 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9651 mbedtls_ssl_async_resume_t *f_async_resume,
9652 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009653 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009654{
9655 conf->f_async_sign_start = f_async_sign;
9656 conf->f_async_decrypt_start = f_async_decrypt;
9657 conf->f_async_resume = f_async_resume;
9658 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009659 conf->p_async_config_data = async_config_data;
9660}
9661
Gilles Peskine8f97af72018-04-26 11:46:10 +02009662void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9663{
9664 return( conf->p_async_config_data );
9665}
9666
Gilles Peskine1febfef2018-04-30 11:54:39 +02009667void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009668{
9669 if( ssl->handshake == NULL )
9670 return( NULL );
9671 else
9672 return( ssl->handshake->user_async_ctx );
9673}
9674
Gilles Peskine1febfef2018-04-30 11:54:39 +02009675void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009676 void *ctx )
9677{
9678 if( ssl->handshake != NULL )
9679 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009680}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009681#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009682
Paul Bakker5121ce52009-01-03 21:22:43 +00009683/*
9684 * SSL get accessors
9685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009687{
9688 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9689}
9690
Hanno Becker8b170a02017-10-10 11:51:19 +01009691int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9692{
9693 /*
9694 * Case A: We're currently holding back
9695 * a message for further processing.
9696 */
9697
9698 if( ssl->keep_current_message == 1 )
9699 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009701 return( 1 );
9702 }
9703
9704 /*
9705 * Case B: Further records are pending in the current datagram.
9706 */
9707
9708#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009709 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009710 ssl->in_left > ssl->next_record_offset )
9711 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009713 return( 1 );
9714 }
9715#endif /* MBEDTLS_SSL_PROTO_DTLS */
9716
9717 /*
9718 * Case C: A handshake message is being processed.
9719 */
9720
Hanno Becker8b170a02017-10-10 11:51:19 +01009721 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9722 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009724 return( 1 );
9725 }
9726
9727 /*
9728 * Case D: An application data message is being processed
9729 */
9730 if( ssl->in_offt != NULL )
9731 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009732 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009733 return( 1 );
9734 }
9735
9736 /*
9737 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009738 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009739 * we implement support for multiple alerts in single records.
9740 */
9741
9742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9743 return( 0 );
9744}
9745
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009746uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009747{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009748 if( ssl->session != NULL )
9749 return( ssl->session->verify_result );
9750
9751 if( ssl->session_negotiate != NULL )
9752 return( ssl->session_negotiate->verify_result );
9753
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009754 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009755}
9756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009758{
Hanno Beckere02758c2019-06-26 15:31:31 +01009759 int suite;
9760
Paul Bakker926c8e42013-03-06 10:23:34 +01009761 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009762 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009763
Hanno Beckere02758c2019-06-26 15:31:31 +01009764 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9765 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009766}
9767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009769{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009771 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009772 {
Hanno Becker2881d802019-05-22 14:44:53 +01009773 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009776 return( "DTLSv1.0" );
9777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009779 return( "DTLSv1.2" );
9780
9781 default:
9782 return( "unknown (DTLS)" );
9783 }
9784 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009785 MBEDTLS_SSL_TRANSPORT_ELSE
9786#endif /* MBEDTLS_SSL_PROTO_DTLS */
9787#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009788 {
Hanno Becker2881d802019-05-22 14:44:53 +01009789 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009790 {
9791 case MBEDTLS_SSL_MINOR_VERSION_0:
9792 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009793
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009794 case MBEDTLS_SSL_MINOR_VERSION_1:
9795 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009796
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009797 case MBEDTLS_SSL_MINOR_VERSION_2:
9798 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009799
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009800 case MBEDTLS_SSL_MINOR_VERSION_3:
9801 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009802
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009803 default:
9804 return( "unknown" );
9805 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009806 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009807#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009808}
9809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009811{
Hanno Becker3136ede2018-08-17 15:28:19 +01009812 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009814
Hanno Becker43395762019-05-03 14:46:38 +01009815 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9816
Hanno Becker78640902018-08-13 16:35:15 +01009817 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009818 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820#if defined(MBEDTLS_ZLIB_SUPPORT)
9821 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9822 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009823#endif
9824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009826 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009827#if defined(MBEDTLS_GCM_C) || \
9828 defined(MBEDTLS_CCM_C) || \
9829 defined(MBEDTLS_CHACHAPOLY_C)
9830#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009832#endif
9833#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009835#endif
9836#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009837 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009838#endif
9839 transform_expansion =
9840 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009841 break;
9842
Hanno Beckera9d5c452019-07-25 16:47:12 +01009843#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9844 MBEDTLS_CHACHAPOLY_C */
9845
9846#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9847 case MBEDTLS_MODE_STREAM:
9848 transform_expansion = transform->maclen;
9849 break;
9850#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9851
9852#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009854 {
9855 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009856
9857 block_size = mbedtls_cipher_get_block_size(
9858 &transform->cipher_ctx_enc );
9859
Hanno Becker3136ede2018-08-17 15:28:19 +01009860 /* Expansion due to the addition of the MAC. */
9861 transform_expansion += transform->maclen;
9862
9863 /* Expansion due to the addition of CBC padding;
9864 * Theoretically up to 256 bytes, but we never use
9865 * more than the block size of the underlying cipher. */
9866 transform_expansion += block_size;
9867
9868 /* For TLS 1.1 or higher, an explicit IV is added
9869 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009870#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009871 if( mbedtls_ssl_ver_geq(
9872 mbedtls_ssl_get_minor_ver( ssl ),
9873 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9874 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009875 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009876 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009877#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009878
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009879 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009880 }
9881#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009882
9883 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009886 }
9887
Hanno Beckera5a2b082019-05-15 14:03:01 +01009888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009889 if( transform->out_cid_len != 0 )
9890 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009891#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009892
Hanno Becker43395762019-05-03 14:46:38 +01009893 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009894}
9895
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009896#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9897size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9898{
9899 size_t max_len;
9900
9901 /*
9902 * Assume mfl_code is correct since it was checked when set
9903 */
Angus Grattond8213d02016-05-25 20:56:48 +10009904 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009905
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009906 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009907 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009908 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009909 {
Angus Grattond8213d02016-05-25 20:56:48 +10009910 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009911 }
9912
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009913 /* During a handshake, use the value being negotiated */
9914 if( ssl->session_negotiate != NULL &&
9915 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9916 {
9917 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9918 }
9919
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009920 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009921}
9922#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9923
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009924#if defined(MBEDTLS_SSL_PROTO_DTLS)
9925static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9926{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009927 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009928 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009929 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9930 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009931 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009932
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009933 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9934 return( ssl->mtu );
9935
9936 if( ssl->mtu == 0 )
9937 return( ssl->handshake->mtu );
9938
9939 return( ssl->mtu < ssl->handshake->mtu ?
9940 ssl->mtu : ssl->handshake->mtu );
9941}
9942#endif /* MBEDTLS_SSL_PROTO_DTLS */
9943
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009944int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9945{
9946 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9947
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009948#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9949 !defined(MBEDTLS_SSL_PROTO_DTLS)
9950 (void) ssl;
9951#endif
9952
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009953#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9954 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9955
9956 if( max_len > mfl )
9957 max_len = mfl;
9958#endif
9959
9960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009961 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009962 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009963 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009964 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9965 const size_t overhead = (size_t) ret;
9966
9967 if( ret < 0 )
9968 return( ret );
9969
9970 if( mtu <= overhead )
9971 {
9972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9973 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9974 }
9975
9976 if( max_len > mtu - overhead )
9977 max_len = mtu - overhead;
9978 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009979#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009980
Hanno Becker0defedb2018-08-10 12:35:02 +01009981#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9982 !defined(MBEDTLS_SSL_PROTO_DTLS)
9983 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009984#endif
9985
9986 return( (int) max_len );
9987}
9988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989#if defined(MBEDTLS_X509_CRT_PARSE_C)
9990const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009991{
9992 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009993 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009994
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009995#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009996 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009997#else
9998 return( NULL );
9999#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +000010000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +000010002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +000010004int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
10005 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010006{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010007 if( ssl == NULL ||
10008 dst == NULL ||
10009 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +010010010 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010013 }
10014
Hanno Becker58fccf22019-02-06 14:30:46 +000010015 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010018
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +020010019const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
10020{
10021 if( ssl == NULL )
10022 return( NULL );
10023
10024 return( ssl->session );
10025}
10026
Paul Bakker5121ce52009-01-03 21:22:43 +000010027/*
Hanno Beckerb5352f02019-05-16 12:39:07 +010010028 * Define ticket header determining Mbed TLS version
10029 * and structure of the ticket.
10030 */
10031
Hanno Becker41527622019-05-16 12:50:45 +010010032/*
Hanno Becker26829e92019-05-28 14:30:45 +010010033 * Define bitflag determining compile-time settings influencing
10034 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010035 */
10036
Hanno Becker26829e92019-05-28 14:30:45 +010010037#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010038#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010039#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010040#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010041#endif /* MBEDTLS_HAVE_TIME */
10042
10043#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010044#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010045#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010046#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010047#endif /* MBEDTLS_X509_CRT_PARSE_C */
10048
10049#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010050#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010051#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010052#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010053#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10054
10055#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010056#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010057#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010058#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010059#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10060
10061#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010062#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010063#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010064#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010065#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10066
10067#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010068#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010069#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010070#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010071#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10072
Hanno Becker41527622019-05-16 12:50:45 +010010073#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10074#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10075#else
10076#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10077#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10078
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010079#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10080#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10081#else
10082#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10083#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10084
Hanno Becker88440552019-07-03 14:16:13 +010010085#if defined(MBEDTLS_ZLIB_SUPPORT)
10086#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10087#else
10088#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10089#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10090
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010091#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10092#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10093#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10094#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10095#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10096#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10097#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010098#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010099#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010100
Hanno Becker26829e92019-05-28 14:30:45 +010010101#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010102 ( (uint16_t) ( \
10103 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10104 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10105 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10106 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10107 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10108 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010109 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010110 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010111 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010112
Hanno Becker557fe9f2019-05-16 12:41:07 +010010113static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010114 MBEDTLS_VERSION_MAJOR,
10115 MBEDTLS_VERSION_MINOR,
10116 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010117 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10118 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010119};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010120
10121/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010122 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010123 * (in the presentation language of TLS, RFC 8446 section 3)
10124 *
Hanno Becker26829e92019-05-28 14:30:45 +010010125 * opaque mbedtls_version[3]; // major, minor, patch
10126 * opaque session_format[2]; // version-specific 16-bit field determining
10127 * // the format of the remaining
10128 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010129 *
10130 * Note: When updating the format, remember to keep
10131 * these version+format bytes.
10132 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010133 * // In this version, `session_format` determines
10134 * // the setting of those compile-time
10135 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010136 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010137 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010138 * uint8 ciphersuite[2]; // defined by the standard
10139 * uint8 compression; // 0 or 1
10140 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010141 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010142 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010143 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010144 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10145 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10146 * case disabled: uint8_t peer_cert_digest_type;
10147 * opaque peer_cert_digest<0..2^8-1>;
10148 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010149 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010150 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010151 * uint8 mfl_code; // up to 255 according to standard
10152 * uint8 trunc_hmac; // 0 or 1
10153 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010154 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010155 * The order is the same as in the definition of the structure, except
10156 * verify_result is put before peer_cert so that all mandatory fields come
10157 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010158 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010159static int ssl_session_save( const mbedtls_ssl_session *session,
10160 unsigned char omit_header,
10161 unsigned char *buf,
10162 size_t buf_len,
10163 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010164{
10165 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010166 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010167#if defined(MBEDTLS_HAVE_TIME)
10168 uint64_t start;
10169#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010170#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010171#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010172 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010173#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010174#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010175
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010176 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010177 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010178 /*
10179 * Add version identifier
10180 */
10181
10182 used += sizeof( ssl_serialized_session_header );
10183
10184 if( used <= buf_len )
10185 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010186 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010187 sizeof( ssl_serialized_session_header ) );
10188 p += sizeof( ssl_serialized_session_header );
10189 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010190 }
10191
10192 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010193 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010194 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010195#if defined(MBEDTLS_HAVE_TIME)
10196 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010197
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010198 if( used <= buf_len )
10199 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010200 start = (uint64_t) session->start;
10201
10202 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10203 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10204 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10205 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10206 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10207 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10208 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10209 *p++ = (unsigned char)( ( start ) & 0xFF );
10210 }
10211#endif /* MBEDTLS_HAVE_TIME */
10212
10213 /*
10214 * Basic mandatory fields
10215 */
Hanno Becker88440552019-07-03 14:16:13 +010010216 {
10217 size_t const ciphersuite_len = 2;
10218#if defined(MBEDTLS_ZLIB_SUPPORT)
10219 size_t const compression_len = 1;
10220#else
10221 size_t const compression_len = 0;
10222#endif
10223 size_t const id_len_len = 1;
10224 size_t const id_len = 32;
10225 size_t const master_len = 48;
10226 size_t const verif_result_len = 4;
10227
10228 size_t const basic_len =
10229 ciphersuite_len +
10230 compression_len +
10231 id_len_len +
10232 id_len +
10233 master_len +
10234 verif_result_len;
10235
10236 used += basic_len;
10237 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010238
10239 if( used <= buf_len )
10240 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010241 const int ciphersuite =
10242 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010243 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010244
Hanno Becker88440552019-07-03 14:16:13 +010010245#if defined(MBEDTLS_ZLIB_SUPPORT)
10246 *p++ = (unsigned char)(
10247 mbedtls_ssl_session_get_compression( session ) );
10248#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010249
10250 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010251 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10252 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010253 p += 32;
10254
Teppo Järvelin91d79382019-10-02 09:09:31 +030010255 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010256 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010257 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010258 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010259
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010260 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010261 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010262 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010263#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010264#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010265 if( session->peer_cert == NULL )
10266 cert_len = 0;
10267 else
10268 cert_len = session->peer_cert->raw.len;
10269
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010270 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010271
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010272 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010273 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010274 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010275
10276 if( session->peer_cert != NULL )
10277 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010278 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010279 p += cert_len;
10280 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010281 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010282
Hanno Becker5882dd02019-06-06 16:25:57 +010010283#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010284 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010285 if( session->peer_cert_digest != NULL )
10286 {
10287 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10288 if( used <= buf_len )
10289 {
10290 *p++ = (unsigned char) session->peer_cert_digest_type;
10291 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010292 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010293 session->peer_cert_digest_len );
10294 p += session->peer_cert_digest_len;
10295 }
10296 }
10297 else
10298 {
10299 used += 2;
10300 if( used <= buf_len )
10301 {
10302 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10303 *p++ = 0;
10304 }
10305 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010306#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010307#endif /* MBEDTLS_X509_CRT_PARSE_C */
10308
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010309 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010310 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010311 */
10312#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010313 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010314
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010315 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010316 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010317 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010318
10319 if( session->ticket != NULL )
10320 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010321 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010322 p += session->ticket_len;
10323 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010324
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010325 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010326 }
10327#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10328
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010329 /*
10330 * Misc extension-related info
10331 */
10332#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10333 used += 1;
10334
10335 if( used <= buf_len )
10336 *p++ = session->mfl_code;
10337#endif
10338
10339#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10340 used += 1;
10341
10342 if( used <= buf_len )
10343 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10344#endif
10345
10346#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10347 used += 1;
10348
10349 if( used <= buf_len )
10350 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10351#endif
10352
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010353 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010354 *olen = used;
10355
10356 if( used > buf_len )
10357 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010358
10359 return( 0 );
10360}
10361
10362/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010363 * Public wrapper for ssl_session_save()
10364 */
10365int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10366 unsigned char *buf,
10367 size_t buf_len,
10368 size_t *olen )
10369{
10370 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10371}
10372
10373/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010374 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010375 *
10376 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010377 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010378 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010379static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010380 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010381 const unsigned char *buf,
10382 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010383{
10384 const unsigned char *p = buf;
10385 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010386 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010387#if defined(MBEDTLS_HAVE_TIME)
10388 uint64_t start;
10389#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010390#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010391#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010392 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010393#endif
10394#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010395
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010396 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010397 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010398 /*
10399 * Check version identifier
10400 */
10401
10402 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10404
Teppo Järvelin0efac532019-10-04 13:21:08 +030010405 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010406 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010407 sizeof( ssl_serialized_session_header ) ) != 0 )
10408 {
10409 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10410 }
10411 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010412 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010413
10414 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010415 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010416 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010417#if defined(MBEDTLS_HAVE_TIME)
10418 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10420
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010421 start = ( (uint64_t) p[0] << 56 ) |
10422 ( (uint64_t) p[1] << 48 ) |
10423 ( (uint64_t) p[2] << 40 ) |
10424 ( (uint64_t) p[3] << 32 ) |
10425 ( (uint64_t) p[4] << 24 ) |
10426 ( (uint64_t) p[5] << 16 ) |
10427 ( (uint64_t) p[6] << 8 ) |
10428 ( (uint64_t) p[7] );
10429 p += 8;
10430
10431 session->start = (time_t) start;
10432#endif /* MBEDTLS_HAVE_TIME */
10433
10434 /*
10435 * Basic mandatory fields
10436 */
Hanno Becker88440552019-07-03 14:16:13 +010010437 {
10438 size_t const ciphersuite_len = 2;
10439#if defined(MBEDTLS_ZLIB_SUPPORT)
10440 size_t const compression_len = 1;
10441#else
10442 size_t const compression_len = 0;
10443#endif
10444 size_t const id_len_len = 1;
10445 size_t const id_len = 32;
10446 size_t const master_len = 48;
10447 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010448
Hanno Becker88440552019-07-03 14:16:13 +010010449 size_t const basic_len =
10450 ciphersuite_len +
10451 compression_len +
10452 id_len_len +
10453 id_len +
10454 master_len +
10455 verif_result_len;
10456
10457 if( basic_len > (size_t)( end - p ) )
10458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10459 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010460
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010461 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010462 p += 2;
10463
Hanno Becker73f4cb12019-06-27 13:51:07 +010010464#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010465 session->ciphersuite = ciphersuite;
10466#else
10467 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010468 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010469 {
10470 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10471 }
10472#endif
10473
Hanno Becker88440552019-07-03 14:16:13 +010010474#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010475 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010476#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010477
10478 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010479 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10480 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010481 p += 32;
10482
Teppo Järvelin91d79382019-10-02 09:09:31 +030010483 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010484 p += 48;
10485
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010486 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010487 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010488
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010489 /* Immediately clear invalid pointer values that have been read, in case
10490 * we exit early before we replaced them with valid ones. */
10491#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010492#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010493 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010494#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010495 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010496#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010497#endif
10498#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10499 session->ticket = NULL;
10500#endif
10501
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010502 /*
10503 * Peer certificate
10504 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010505#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010506#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010507 if( 3 > (size_t)( end - p ) )
10508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10509
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010510 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10511
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010512 p += 3;
10513
10514 if( cert_len == 0 )
10515 {
10516 session->peer_cert = NULL;
10517 }
10518 else
10519 {
10520 int ret;
10521
10522 if( cert_len > (size_t)( end - p ) )
10523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10524
10525 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10526
10527 if( session->peer_cert == NULL )
10528 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10529
10530 mbedtls_x509_crt_init( session->peer_cert );
10531
10532 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10533 p, cert_len ) ) != 0 )
10534 {
10535 mbedtls_x509_crt_free( session->peer_cert );
10536 mbedtls_free( session->peer_cert );
10537 session->peer_cert = NULL;
10538 return( ret );
10539 }
10540
10541 p += cert_len;
10542 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010543#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010544 /* Deserialize CRT digest from the end of the ticket. */
10545 if( 2 > (size_t)( end - p ) )
10546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10547
10548 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10549 session->peer_cert_digest_len = (size_t) *p++;
10550
10551 if( session->peer_cert_digest_len != 0 )
10552 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010553 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010554 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010555 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10557 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10559
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010560 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10562
10563 session->peer_cert_digest =
10564 mbedtls_calloc( 1, session->peer_cert_digest_len );
10565 if( session->peer_cert_digest == NULL )
10566 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10567
Teppo Järvelin91d79382019-10-02 09:09:31 +030010568 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010569 session->peer_cert_digest_len );
10570 p += session->peer_cert_digest_len;
10571 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010572#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010573#endif /* MBEDTLS_X509_CRT_PARSE_C */
10574
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010575 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010576 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010577 */
10578#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10579 if( 3 > (size_t)( end - p ) )
10580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10581
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010582 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010583 p += 3;
10584
10585 if( session->ticket_len != 0 )
10586 {
10587 if( session->ticket_len > (size_t)( end - p ) )
10588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10589
10590 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10591 if( session->ticket == NULL )
10592 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10593
Teppo Järvelin91d79382019-10-02 09:09:31 +030010594 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010595 p += session->ticket_len;
10596 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010597
10598 if( 4 > (size_t)( end - p ) )
10599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10600
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010601 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010602 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010603#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10604
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010605 /*
10606 * Misc extension-related info
10607 */
10608#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10609 if( 1 > (size_t)( end - p ) )
10610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10611
10612 session->mfl_code = *p++;
10613#endif
10614
10615#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10616 if( 1 > (size_t)( end - p ) )
10617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10618
10619 session->trunc_hmac = *p++;
10620#endif
10621
10622#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10623 if( 1 > (size_t)( end - p ) )
10624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10625
10626 session->encrypt_then_mac = *p++;
10627#endif
10628
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010629 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010630 if( p != end )
10631 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10632
10633 return( 0 );
10634}
10635
10636/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010637 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010638 */
10639int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10640 const unsigned char *buf,
10641 size_t len )
10642{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010643 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010644
10645 if( ret != 0 )
10646 mbedtls_ssl_session_free( session );
10647
10648 return( ret );
10649}
10650
10651/*
Paul Bakker1961b702013-01-25 14:49:24 +010010652 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010657
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010658 if( ssl == NULL || ssl->conf == NULL )
10659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010662 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010663 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010664#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010666 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010668#endif
10669
Hanno Beckerb82350b2019-07-26 07:24:05 +010010670 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010671 return( ret );
10672}
10673
10674/*
10675 * Perform the SSL handshake
10676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010678{
10679 int ret = 0;
10680
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010681 if( ssl == NULL || ssl->conf == NULL )
10682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010689
10690 if( ret != 0 )
10691 break;
10692 }
10693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010695
10696 return( ret );
10697}
10698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699#if defined(MBEDTLS_SSL_RENEGOTIATION)
10700#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010701/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010702 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010705{
10706 int ret;
10707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010709
10710 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010711 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10712 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010713
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010714 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010715 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010717 return( ret );
10718 }
10719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010721
10722 return( 0 );
10723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010725
10726/*
10727 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728 * - any side: calling mbedtls_ssl_renegotiate(),
10729 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10730 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010731 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010732 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010736{
10737 int ret;
10738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010740
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010741 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10742 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010743
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010744 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10745 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010747 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010749 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010750 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10751 MBEDTLS_SSL_IS_SERVER )
10752 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010753 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010754 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010755 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010756 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010757 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010758 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010759 }
10760#endif
10761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10763 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010768 return( ret );
10769 }
10770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010772
10773 return( 0 );
10774}
10775
10776/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010777 * Renegotiate current connection on client,
10778 * or request renegotiation on server
10779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010781{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010782 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010783
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010784 if( ssl == NULL || ssl->conf == NULL )
10785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010788 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010789 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010794 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010795
10796 /* Did we already try/start sending HelloRequest? */
10797 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010799
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010800 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010801 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010802#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010804#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010805 /*
10806 * On client, either start the renegotiation process or,
10807 * if already in progress, continue the handshake
10808 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010813
10814 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010817 return( ret );
10818 }
10819 }
10820 else
10821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010822 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010825 return( ret );
10826 }
10827 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010828#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010829
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010830 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010831}
10832
10833/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010834 * Check record counters and renegotiate if they're above the limit.
10835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010837{
Andres AG2196c7f2016-12-15 17:01:16 +000010838 size_t ep_len = ssl_ep_len( ssl );
10839 int in_ctr_cmp;
10840 int out_ctr_cmp;
10841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010842 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10843 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010844 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010845 {
10846 return( 0 );
10847 }
10848
Teppo Järvelin0efac532019-10-04 13:21:08 +030010849 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010850 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010851 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010852 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010853 ssl->conf->renego_period + ep_len, 8 - ep_len );
10854
10855 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010856 {
10857 return( 0 );
10858 }
10859
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010861 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010862}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010864
10865/*
10866 * Receive application data decrypted from the SSL layer
10867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010869{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010870 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010871 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010872
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010873 if( ssl == NULL || ssl->conf == NULL )
10874 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010879 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010882 return( ret );
10883
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010884 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010885 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010886 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010887 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010888 return( ret );
10889 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010890 }
10891#endif
10892
Hanno Becker4a810fb2017-05-24 16:27:30 +010010893 /*
10894 * Check if renegotiation is necessary and/or handshake is
10895 * in process. If yes, perform/continue, and fall through
10896 * if an unexpected packet is received while the client
10897 * is waiting for the ServerHello.
10898 *
10899 * (There is no equivalent to the last condition on
10900 * the server-side as it is not treated as within
10901 * a handshake while waiting for the ClientHello
10902 * after a renegotiation request.)
10903 */
10904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010905#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010906 ret = ssl_check_ctr_renegotiate( ssl );
10907 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10908 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010911 return( ret );
10912 }
10913#endif
10914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010918 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10919 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010922 return( ret );
10923 }
10924 }
10925
Hanno Beckere41158b2017-10-23 13:30:32 +010010926 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010927 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010928 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010929 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010930 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10931 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010932 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010933 ssl_set_timer( ssl,
10934 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010935 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010936
Hanno Becker327c93b2018-08-15 13:56:18 +010010937 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010938 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010939 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10940 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010941
Hanno Becker4a810fb2017-05-24 16:27:30 +010010942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10943 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010944 }
10945
10946 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010948 {
10949 /*
10950 * OpenSSL sends empty messages to randomize the IV
10951 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010952 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010955 return( 0 );
10956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010958 return( ret );
10959 }
10960 }
10961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010962 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010965
Hanno Becker4a810fb2017-05-24 16:27:30 +010010966 /*
10967 * - For client-side, expect SERVER_HELLO_REQUEST.
10968 * - For server-side, expect CLIENT_HELLO.
10969 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10970 */
10971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010972#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010973 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10974 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010976 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010979
10980 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010982 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010983 {
10984 continue;
10985 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010986 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010987#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010988#if defined(MBEDTLS_SSL_PROTO_TLS)
10989 {
10990 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10991 }
10992#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010993 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010994#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010995
Hanno Becker4a810fb2017-05-24 16:27:30 +010010996#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010997 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10998 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010999 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011002
11003 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011005 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010011006 {
11007 continue;
11008 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011009 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011010#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011011#if defined(MBEDTLS_SSL_PROTO_TLS)
11012 {
11013 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
11014 }
11015#endif
Paul Bakker48916f92012-09-16 19:57:18 +000011016 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011017#endif /* MBEDTLS_SSL_SRV_C */
11018
Hanno Becker21df7f92017-10-17 11:03:26 +010011019#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010011020 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011021 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
11022 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010011023 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011024 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
11025 {
11026 /*
11027 * Accept renegotiation request
11028 */
Paul Bakker48916f92012-09-16 19:57:18 +000011029
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011030 /* DTLS clients need to know renego is server-initiated */
11031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011032 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011033 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11034 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011035 {
11036 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11037 }
11038#endif
11039 ret = ssl_start_renegotiation( ssl );
11040 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11041 ret != 0 )
11042 {
11043 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11044 return( ret );
11045 }
11046 }
11047 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011048#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011049 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011050 /*
11051 * Refuse renegotiation
11052 */
11053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011056#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011057 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011058 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011059 /* SSLv3 does not have a "no_renegotiation" warning, so
11060 we send a fatal alert and abort the connection. */
11061 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11062 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11063 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011064 }
11065 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11067#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11068 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011069 if( mbedtls_ssl_ver_geq(
11070 mbedtls_ssl_get_minor_ver( ssl ),
11071 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011072 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011073 ret = mbedtls_ssl_send_alert_message( ssl,
11074 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11075 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11076 if( ret != 0 )
11077 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011078 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011079 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11081 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011085 }
Paul Bakker48916f92012-09-16 19:57:18 +000011086 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011087
Hanno Becker90333da2017-10-10 11:27:13 +010011088 /* At this point, we don't know whether the renegotiation has been
11089 * completed or not. The cases to consider are the following:
11090 * 1) The renegotiation is complete. In this case, no new record
11091 * has been read yet.
11092 * 2) The renegotiation is incomplete because the client received
11093 * an application data record while awaiting the ServerHello.
11094 * 3) The renegotiation is incomplete because the client received
11095 * a non-handshake, non-application data message while awaiting
11096 * the ServerHello.
11097 * In each of these case, looping will be the proper action:
11098 * - For 1), the next iteration will read a new record and check
11099 * if it's application data.
11100 * - For 2), the loop condition isn't satisfied as application data
11101 * is present, hence continue is the same as break
11102 * - For 3), the loop condition is satisfied and read_record
11103 * will re-deliver the message that was held back by the client
11104 * when expecting the ServerHello.
11105 */
11106 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011107 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011108#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011109 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011110 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011111 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011112 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011113 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011116 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011118 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011119 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011120 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011121#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011123 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11124 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011127 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011128 }
11129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011130 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11133 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011134 }
11135
11136 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011137
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011138 /* We're going to return something now, cancel timer,
11139 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011140 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011141 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011142
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011144 /* If we requested renego but received AppData, resend HelloRequest.
11145 * Do it now, after setting in_offt, to avoid taking this branch
11146 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011147#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011148 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11149 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011150 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011151 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011152 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011155 return( ret );
11156 }
11157 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011158#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011159#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011160 }
11161
11162 n = ( len < ssl->in_msglen )
11163 ? len : ssl->in_msglen;
11164
Teppo Järvelin91d79382019-10-02 09:09:31 +030011165 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011166 ssl->in_msglen -= n;
11167
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011168 // clear incoming data after it's copied to buffer
11169 mbedtls_platform_memset(ssl->in_offt, 0, n);
11170
Paul Bakker5121ce52009-01-03 21:22:43 +000011171 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011172 {
11173 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011174 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011175 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011176 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011177 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011178 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011179 /* more data available */
11180 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011181 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011184
Paul Bakker23986e52011-04-24 08:57:21 +000011185 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011186}
11187
11188/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011189 * Send application data to be encrypted by the SSL layer, taking care of max
11190 * fragment length and buffer size.
11191 *
11192 * According to RFC 5246 Section 6.2.1:
11193 *
11194 * Zero-length fragments of Application data MAY be sent as they are
11195 * potentially useful as a traffic analysis countermeasure.
11196 *
11197 * Therefore, it is possible that the input message length is 0 and the
11198 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011199 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011200static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011201 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011202{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011203 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11204 const size_t max_len = (size_t) ret;
11205
11206 if( ret < 0 )
11207 {
11208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11209 return( ret );
11210 }
11211
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011212 if( len > max_len )
11213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011214#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011215 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011218 "maximum fragment length: %d > %d",
11219 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011221 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011222 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011223#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011224#if defined(MBEDTLS_SSL_PROTO_TLS)
11225 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011226 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011227 }
11228#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011229 }
Paul Bakker887bd502011-06-08 13:10:54 +000011230
Paul Bakker5121ce52009-01-03 21:22:43 +000011231 if( ssl->out_left != 0 )
11232 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011233 /*
11234 * The user has previously tried to send the data and
11235 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11236 * written. In this case, we expect the high-level write function
11237 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011239 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011242 return( ret );
11243 }
11244 }
Paul Bakker887bd502011-06-08 13:10:54 +000011245 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011246 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011247 /*
11248 * The user is trying to send a message the first time, so we need to
11249 * copy the data into the internal buffers and setup the data structure
11250 * to keep track of partial writes
11251 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011252 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011253 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011254 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011255
Hanno Becker67bc7c32018-08-06 11:33:50 +010011256 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011259 return( ret );
11260 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011261 }
11262
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011263 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011264}
11265
11266/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011267 * Write application data, doing 1/n-1 splitting if necessary.
11268 *
11269 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011270 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011271 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011273#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011274static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011275 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011276{
11277 int ret;
11278
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011279 if( ssl->conf->cbc_record_splitting ==
11280 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011281 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011282 mbedtls_ssl_ver_gt(
11283 mbedtls_ssl_get_minor_ver( ssl ),
11284 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011285 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11286 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011287 {
11288 return( ssl_write_real( ssl, buf, len ) );
11289 }
11290
11291 if( ssl->split_done == 0 )
11292 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011293 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011294 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011295 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011296 }
11297
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011298 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11299 return( ret );
11300 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011301
11302 return( ret + 1 );
11303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011304#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011305
11306/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011307 * Write application data (public-facing wrapper)
11308 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011309int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011310{
11311 int ret;
11312
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011314
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011315 if( ssl == NULL || ssl->conf == NULL )
11316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11317
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011318#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011319 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11320 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011321 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011322 return( ret );
11323 }
11324#endif
11325
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011326 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011327 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011328 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011329 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011331 return( ret );
11332 }
11333 }
11334
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011335#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011336 ret = ssl_write_split( ssl, buf, len );
11337#else
11338 ret = ssl_write_real( ssl, buf, len );
11339#endif
11340
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011342
11343 return( ret );
11344}
11345
11346/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011347 * Notify the peer that the connection is being closed
11348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011349int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011350{
11351 int ret;
11352
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011353 if( ssl == NULL || ssl->conf == NULL )
11354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011357
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011358 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011359 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011361 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011363 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11364 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11365 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011367 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011368 return( ret );
11369 }
11370 }
11371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011372 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011373
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011374 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011375}
11376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011377void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011378{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011379 if( transform == NULL )
11380 return;
11381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011382#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011383 deflateEnd( &transform->ctx_deflate );
11384 inflateEnd( &transform->ctx_inflate );
11385#endif
11386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011387 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11388 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011389
Hanno Becker92231322018-01-03 15:32:51 +000011390#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011391 mbedtls_md_free( &transform->md_ctx_enc );
11392 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011393#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011394
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011395 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011396}
11397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011398#if defined(MBEDTLS_X509_CRT_PARSE_C)
11399static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011400{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011401 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011402
11403 while( cur != NULL )
11404 {
11405 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011406 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011407 cur = next;
11408 }
11409}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011410#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011411
Hanno Becker0271f962018-08-16 13:23:47 +010011412#if defined(MBEDTLS_SSL_PROTO_DTLS)
11413
11414static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11415{
11416 unsigned offset;
11417 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11418
11419 if( hs == NULL )
11420 return;
11421
Hanno Becker283f5ef2018-08-24 09:34:47 +010011422 ssl_free_buffered_record( ssl );
11423
Hanno Becker0271f962018-08-16 13:23:47 +010011424 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011425 ssl_buffering_free_slot( ssl, offset );
11426}
11427
11428static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11429 uint8_t slot )
11430{
11431 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11432 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011433
11434 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11435 return;
11436
Hanno Beckere605b192018-08-21 15:59:07 +010011437 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011438 {
Hanno Beckere605b192018-08-21 15:59:07 +010011439 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011440 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011441 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011442 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011443 }
11444}
11445
11446#endif /* MBEDTLS_SSL_PROTO_DTLS */
11447
Gilles Peskine9b562d52018-04-25 20:32:43 +020011448void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011449{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011450 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11451
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011452 if( handshake == NULL )
11453 return;
11454
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011455#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11456 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11457 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011458 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011459 handshake->async_in_progress = 0;
11460 }
11461#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11462
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011463#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11464 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11465 mbedtls_md5_free( &handshake->fin_md5 );
11466 mbedtls_sha1_free( &handshake->fin_sha1 );
11467#endif
11468#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11469#if defined(MBEDTLS_SHA256_C)
11470 mbedtls_sha256_free( &handshake->fin_sha256 );
11471#endif
11472#if defined(MBEDTLS_SHA512_C)
11473 mbedtls_sha512_free( &handshake->fin_sha512 );
11474#endif
11475#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011477#if defined(MBEDTLS_DHM_C)
11478 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011479#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011480#if defined(MBEDTLS_ECDH_C)
11481 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011482#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011483#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011484 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011485#if defined(MBEDTLS_SSL_CLI_C)
11486 mbedtls_free( handshake->ecjpake_cache );
11487 handshake->ecjpake_cache = NULL;
11488 handshake->ecjpake_cache_len = 0;
11489#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011490#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011491
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011492#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11493 if( handshake->psk != NULL )
11494 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011495 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011496 mbedtls_free( handshake->psk );
11497 }
11498#endif
11499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011500#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11501 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011502 /*
11503 * Free only the linked list wrapper, not the keys themselves
11504 * since the belong to the SNI callback
11505 */
11506 if( handshake->sni_key_cert != NULL )
11507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011508 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011509
11510 while( cur != NULL )
11511 {
11512 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011513 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011514 cur = next;
11515 }
11516 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011517#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011518
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011519#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011520 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011521 if( handshake->ecrs_peer_cert != NULL )
11522 {
11523 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11524 mbedtls_free( handshake->ecrs_peer_cert );
11525 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011526#endif
11527
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011528#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11529 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11530 mbedtls_pk_free( &handshake->peer_pubkey );
11531#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011533#if defined(MBEDTLS_SSL_PROTO_DTLS)
11534 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011535 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011536 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011537#endif
11538
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011539 mbedtls_platform_zeroize( handshake,
11540 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011541}
11542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011543void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011544{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011545 if( session == NULL )
11546 return;
11547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011548#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011549 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011550#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011551
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011552#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011553 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011554#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011555
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011556 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011557}
11558
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011559#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011560
11561#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11562#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11563#else
11564#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11565#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11566
11567#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11568#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11569#else
11570#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11571#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11572
11573#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11574#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11575#else
11576#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11577#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11578
11579#if defined(MBEDTLS_SSL_ALPN)
11580#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11581#else
11582#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11583#endif /* MBEDTLS_SSL_ALPN */
11584
11585#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11586#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11587#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11588#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11589
11590#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11591 ( (uint32_t) ( \
11592 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11593 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11594 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11595 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11596 0u ) )
11597
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011598static unsigned char ssl_serialized_context_header[] = {
11599 MBEDTLS_VERSION_MAJOR,
11600 MBEDTLS_VERSION_MINOR,
11601 MBEDTLS_VERSION_PATCH,
11602 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11603 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011604 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11605 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11606 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011607};
11608
Paul Bakker5121ce52009-01-03 21:22:43 +000011609/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011610 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011611 *
11612 * The format of the serialized data is:
11613 * (in the presentation language of TLS, RFC 8446 section 3)
11614 *
11615 * // header
11616 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011617 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011618 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011619 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011620 * Note: When updating the format, remember to keep these
11621 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011622 *
11623 * // session sub-structure
11624 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11625 * // transform sub-structure
11626 * uint8 random[64]; // ServerHello.random+ClientHello.random
11627 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11628 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11629 * // fields from ssl_context
11630 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11631 * uint64 in_window_top; // DTLS: last validated record seq_num
11632 * uint64 in_window; // DTLS: bitmask for replay protection
11633 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11634 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11635 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11636 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11637 *
11638 * Note that many fields of the ssl_context or sub-structures are not
11639 * serialized, as they fall in one of the following categories:
11640 *
11641 * 1. forced value (eg in_left must be 0)
11642 * 2. pointer to dynamically-allocated memory (eg session, transform)
11643 * 3. value can be re-derived from other data (eg session keys from MS)
11644 * 4. value was temporary (eg content of input buffer)
11645 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011646 */
11647int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11648 unsigned char *buf,
11649 size_t buf_len,
11650 size_t *olen )
11651{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011652 unsigned char *p = buf;
11653 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011654 size_t session_len;
11655 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011656
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011657 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011658 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11659 * this function's documentation.
11660 *
11661 * These are due to assumptions/limitations in the implementation. Some of
11662 * them are likely to stay (no handshake in progress) some might go away
11663 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011664 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011665 /* The initial handshake must be over */
11666 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011668 if( ssl->handshake != NULL )
11669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11670 /* Double-check that sub-structures are indeed ready */
11671 if( ssl->transform == NULL || ssl->session == NULL )
11672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11673 /* There must be no pending incoming or outgoing data */
11674 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11675 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11676 if( ssl->out_left != 0 )
11677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11678 /* Protocol must be DLTS, not TLS */
11679 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11680 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11681 /* Version must be 1.2 */
11682 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11684 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11686 /* We must be using an AEAD ciphersuite */
11687 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11689 /* Renegotiation must not be enabled */
11690 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011692
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011693 /*
11694 * Version and format identifier
11695 */
11696 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011697
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011698 if( used <= buf_len )
11699 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011700 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011701 sizeof( ssl_serialized_context_header ) );
11702 p += sizeof( ssl_serialized_context_header );
11703 }
11704
11705 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011706 * Session (length + data)
11707 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011708 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011709 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11710 return( ret );
11711
11712 used += 4 + session_len;
11713 if( used <= buf_len )
11714 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011715 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011716
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011717 ret = ssl_session_save( ssl->session, 1,
11718 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011719 if( ret != 0 )
11720 return( ret );
11721
11722 p += session_len;
11723 }
11724
11725 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011726 * Transform
11727 */
11728 used += sizeof( ssl->transform->randbytes );
11729 if( used <= buf_len )
11730 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011731 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011732 sizeof( ssl->transform->randbytes ) );
11733 p += sizeof( ssl->transform->randbytes );
11734 }
11735
11736#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11737 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11738 if( used <= buf_len )
11739 {
11740 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011741 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11742 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011743 p += ssl->transform->in_cid_len;
11744
11745 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011746 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11747 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011748 p += ssl->transform->out_cid_len;
11749 }
11750#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11751
11752 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011753 * Saved fields from top-level ssl_context structure
11754 */
11755#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11756 used += 4;
11757 if( used <= buf_len )
11758 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011759 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011760 }
11761#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11762
11763#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11764 used += 16;
11765 if( used <= buf_len )
11766 {
11767 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11768 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11769 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11770 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11771 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11772 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11773 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11774 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11775
11776 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11777 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11778 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11779 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11780 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11781 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11782 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11783 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11784 }
11785#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11786
11787#if defined(MBEDTLS_SSL_PROTO_DTLS)
11788 used += 1;
11789 if( used <= buf_len )
11790 {
11791 *p++ = ssl->disable_datagram_packing;
11792 }
11793#endif /* MBEDTLS_SSL_PROTO_DTLS */
11794
11795 used += 8;
11796 if( used <= buf_len )
11797 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011798 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011799 p += 8;
11800 }
11801
11802#if defined(MBEDTLS_SSL_PROTO_DTLS)
11803 used += 2;
11804 if( used <= buf_len )
11805 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011806 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011807 }
11808#endif /* MBEDTLS_SSL_PROTO_DTLS */
11809
11810#if defined(MBEDTLS_SSL_ALPN)
11811 {
11812 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011813 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011814 : 0;
11815
11816 used += 1 + alpn_len;
11817 if( used <= buf_len )
11818 {
11819 *p++ = alpn_len;
11820
11821 if( ssl->alpn_chosen != NULL )
11822 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011823 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011824 p += alpn_len;
11825 }
11826 }
11827 }
11828#endif /* MBEDTLS_SSL_ALPN */
11829
11830 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011831 * Done
11832 */
11833 *olen = used;
11834
11835 if( used > buf_len )
11836 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011837
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011838 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11839
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011840 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011841}
11842
11843/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011844 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011845 *
11846 * This internal version is wrapped by a public function that cleans up in
11847 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011848 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011849static int ssl_context_load( mbedtls_ssl_context *ssl,
11850 const unsigned char *buf,
11851 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011852{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011853 const unsigned char *p = buf;
11854 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011855 size_t session_len;
11856 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011857
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011858 /*
11859 * The context should have been freshly setup or reset.
11860 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011861 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011862 * renegotiating, or if the user mistakenly loaded a session first.)
11863 */
11864 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11865 ssl->session != NULL )
11866 {
11867 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11868 }
11869
11870 /*
11871 * We can't check that the config matches the initial one, but we can at
11872 * least check it matches the requirements for serializing.
11873 */
11874 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011875 mbedtls_ssl_ver_lt(
11876 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11877 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11878 mbedtls_ssl_ver_gt(
11879 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11880 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11881 mbedtls_ssl_ver_lt(
11882 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11883 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11884 mbedtls_ssl_ver_gt(
11885 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11886 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011887 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011888 {
11889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11890 }
11891
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011892 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11893
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011894 /*
11895 * Check version identifier
11896 */
11897 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11899
Teppo Järvelin650343c2019-10-03 15:36:59 +030011900 // use regular memcmp as header is not that critical
11901 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011902 sizeof( ssl_serialized_context_header ) ) != 0 )
11903 {
11904 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11905 }
11906 p += sizeof( ssl_serialized_context_header );
11907
11908 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011909 * Session
11910 */
11911 if( (size_t)( end - p ) < 4 )
11912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11913
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011914 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011915 p += 4;
11916
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011917 /* This has been allocated by ssl_handshake_init(), called by
11918 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11919 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011920 ssl->session_in = ssl->session;
11921 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011922 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011923
11924 if( (size_t)( end - p ) < session_len )
11925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11926
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011927 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011928 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011929 {
11930 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011931 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011932 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011933
11934 p += session_len;
11935
11936 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011937 * Transform
11938 */
11939
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011940 /* This has been allocated by ssl_handshake_init(), called by
11941 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11942 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011943 ssl->transform_in = ssl->transform;
11944 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011945 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011946
11947 /* Read random bytes and populate structure */
11948 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11949 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11950
11951 ret = ssl_populate_transform( ssl->transform,
11952 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11953 ssl->session->master,
11954#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11955#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11956 ssl->session->encrypt_then_mac,
11957#endif
11958#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11959 ssl->session->trunc_hmac,
11960#endif
11961#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11962#if defined(MBEDTLS_ZLIB_SUPPORT)
11963 ssl->session->compression,
11964#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011965 p, /* currently pointing to randbytes */
11966 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11967 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11968 ssl );
11969 if( ret != 0 )
11970 return( ret );
11971
11972 p += sizeof( ssl->transform->randbytes );
11973
11974#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11975 /* Read connection IDs and store them */
11976 if( (size_t)( end - p ) < 1 )
11977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11978
11979 ssl->transform->in_cid_len = *p++;
11980
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011981 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011982 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11983
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011984 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11985 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011986 p += ssl->transform->in_cid_len;
11987
11988 ssl->transform->out_cid_len = *p++;
11989
11990 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11992
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011993 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11994 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011995 p += ssl->transform->out_cid_len;
11996#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11997
11998 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011999 * Saved fields from top-level ssl_context structure
12000 */
12001#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
12002 if( (size_t)( end - p ) < 4 )
12003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12004
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012005 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012006 p += 4;
12007#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
12008
12009#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
12010 if( (size_t)( end - p ) < 16 )
12011 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12012
12013 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
12014 ( (uint64_t) p[1] << 48 ) |
12015 ( (uint64_t) p[2] << 40 ) |
12016 ( (uint64_t) p[3] << 32 ) |
12017 ( (uint64_t) p[4] << 24 ) |
12018 ( (uint64_t) p[5] << 16 ) |
12019 ( (uint64_t) p[6] << 8 ) |
12020 ( (uint64_t) p[7] );
12021 p += 8;
12022
12023 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
12024 ( (uint64_t) p[1] << 48 ) |
12025 ( (uint64_t) p[2] << 40 ) |
12026 ( (uint64_t) p[3] << 32 ) |
12027 ( (uint64_t) p[4] << 24 ) |
12028 ( (uint64_t) p[5] << 16 ) |
12029 ( (uint64_t) p[6] << 8 ) |
12030 ( (uint64_t) p[7] );
12031 p += 8;
12032#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12033
12034#if defined(MBEDTLS_SSL_PROTO_DTLS)
12035 if( (size_t)( end - p ) < 1 )
12036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12037
12038 ssl->disable_datagram_packing = *p++;
12039#endif /* MBEDTLS_SSL_PROTO_DTLS */
12040
12041 if( (size_t)( end - p ) < 8 )
12042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12043
Teppo Järvelin91d79382019-10-02 09:09:31 +030012044 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012045 p += 8;
12046
12047#if defined(MBEDTLS_SSL_PROTO_DTLS)
12048 if( (size_t)( end - p ) < 2 )
12049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012050 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012051 p += 2;
12052#endif /* MBEDTLS_SSL_PROTO_DTLS */
12053
12054#if defined(MBEDTLS_SSL_ALPN)
12055 {
12056 uint8_t alpn_len;
12057 const char **cur;
12058
12059 if( (size_t)( end - p ) < 1 )
12060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12061
12062 alpn_len = *p++;
12063
12064 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12065 {
12066 /* alpn_chosen should point to an item in the configured list */
12067 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12068 {
12069 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012070 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012071 {
12072 ssl->alpn_chosen = *cur;
12073 break;
12074 }
12075 }
12076 }
12077
12078 /* can only happen on conf mismatch */
12079 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12081
12082 p += alpn_len;
12083 }
12084#endif /* MBEDTLS_SSL_ALPN */
12085
12086 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012087 * Forced fields from top-level ssl_context structure
12088 *
12089 * Most of them already set to the correct value by mbedtls_ssl_init() and
12090 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12091 */
12092 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12093
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012094#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012095 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012096#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12097#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012098 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012099#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012100
Hanno Becker83985822019-08-30 10:42:49 +010012101 /* Adjust pointers for header fields of outgoing records to
12102 * the given transform, accounting for explicit IV and CID. */
12103 ssl_update_out_pointers( ssl, ssl->transform );
12104
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012105#if defined(MBEDTLS_SSL_PROTO_DTLS)
12106 ssl->in_epoch = 1;
12107#endif
12108
12109 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12110 * which we don't want - otherwise we'd end up freeing the wrong transform
12111 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12112 if( ssl->handshake != NULL )
12113 {
12114 mbedtls_ssl_handshake_free( ssl );
12115 mbedtls_free( ssl->handshake );
12116 ssl->handshake = NULL;
12117 }
12118
12119 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012120 * Done - should have consumed entire buffer
12121 */
12122 if( p != end )
12123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012124
12125 return( 0 );
12126}
12127
12128/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012129 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012130 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012131int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012132 const unsigned char *buf,
12133 size_t len )
12134{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012135 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012136
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012137 if( ret != 0 )
12138 mbedtls_ssl_free( context );
12139
12140 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012141}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012142#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012143
12144/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012145 * Free an SSL context
12146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012147void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012148{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012149 if( ssl == NULL )
12150 return;
12151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012153
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012154 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012155 {
Angus Grattond8213d02016-05-25 20:56:48 +100012156 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012157 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012158 }
12159
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012160 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012161 {
Angus Grattond8213d02016-05-25 20:56:48 +100012162 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012163 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012164 }
12165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012166#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012167 if( ssl->compress_buf != NULL )
12168 {
Angus Grattond8213d02016-05-25 20:56:48 +100012169 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012170 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012171 }
12172#endif
12173
Paul Bakker48916f92012-09-16 19:57:18 +000012174 if( ssl->transform )
12175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012176 mbedtls_ssl_transform_free( ssl->transform );
12177 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012178 }
12179
12180 if( ssl->handshake )
12181 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012182 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012183 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12184 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012186 mbedtls_free( ssl->handshake );
12187 mbedtls_free( ssl->transform_negotiate );
12188 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012189 }
12190
Paul Bakkerc0463502013-02-14 11:19:38 +010012191 if( ssl->session )
12192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012193 mbedtls_ssl_session_free( ssl->session );
12194 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012195 }
12196
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012197#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012198 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012199 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012200 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012201 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012202 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012203#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012205#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12206 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12209 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012210 }
12211#endif
12212
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012213#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012214 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012215#endif
12216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012218
Paul Bakker86f04f42013-02-14 11:20:09 +010012219 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012220 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012221}
12222
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012223/*
12224 * Initialze mbedtls_ssl_config
12225 */
12226void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12227{
12228 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012229
12230#if !defined(MBEDTLS_SSL_PROTO_TLS)
12231 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12232#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012233}
12234
Simon Butcherc97b6972015-12-27 23:48:17 +000012235#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012236#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012237static int ssl_preset_default_hashes[] = {
12238#if defined(MBEDTLS_SHA512_C)
12239 MBEDTLS_MD_SHA512,
12240 MBEDTLS_MD_SHA384,
12241#endif
12242#if defined(MBEDTLS_SHA256_C)
12243 MBEDTLS_MD_SHA256,
12244 MBEDTLS_MD_SHA224,
12245#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012246#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012247 MBEDTLS_MD_SHA1,
12248#endif
12249 MBEDTLS_MD_NONE
12250};
Simon Butcherc97b6972015-12-27 23:48:17 +000012251#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012252#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012253
Hanno Becker73f4cb12019-06-27 13:51:07 +010012254#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012255static int ssl_preset_suiteb_ciphersuites[] = {
12256 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12257 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12258 0
12259};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012260#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012261
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012262#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012263#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012264static int ssl_preset_suiteb_hashes[] = {
12265 MBEDTLS_MD_SHA256,
12266 MBEDTLS_MD_SHA384,
12267 MBEDTLS_MD_NONE
12268};
12269#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012270#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012271
Hanno Beckerc1096e72019-06-19 12:30:41 +010012272#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012273static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012274#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012275 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012276#endif
12277#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012278 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012279#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012280 MBEDTLS_ECP_DP_NONE
12281};
12282#endif
12283
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012284/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012285 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012286 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012287int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012288 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012289{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012290#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012291 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012292#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012293
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012294 /* Use the functions here so that they are covered in tests,
12295 * but otherwise access member directly for efficiency */
12296 mbedtls_ssl_conf_endpoint( conf, endpoint );
12297 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012298
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012299 /*
12300 * Things that are common to all presets
12301 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012302#if defined(MBEDTLS_SSL_CLI_C)
12303 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12304 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012305#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012306 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012307#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012308#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12309 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12310#endif
12311 }
12312#endif
12313
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012314#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012315 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012316#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012317
12318#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12319 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12320#endif
12321
12322#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012323#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012324 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012325#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12326#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012327 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012328 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012329#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012330#endif
12331
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012332#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12333 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12334#endif
12335
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012336#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012337 conf->f_cookie_write = ssl_cookie_write_dummy;
12338 conf->f_cookie_check = ssl_cookie_check_dummy;
12339#endif
12340
Hanno Becker7f376f42019-06-12 16:20:48 +010012341#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12342 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012343 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12344#endif
12345
Janos Follath088ce432017-04-10 12:42:31 +010012346#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012347#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012348 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012349#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12350#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012351
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012353#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012354 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012355#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12356#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012357 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012358#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12359#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012360
12361#if defined(MBEDTLS_SSL_RENEGOTIATION)
12362 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012363 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12364 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012365#endif
12366
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012367#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12368 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12369 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012370 const unsigned char dhm_p[] =
12371 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12372 const unsigned char dhm_g[] =
12373 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12374
Hanno Beckera90658f2017-10-04 15:29:08 +010012375 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12376 dhm_p, sizeof( dhm_p ),
12377 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012378 {
12379 return( ret );
12380 }
12381 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012382#endif
12383
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012384 /*
12385 * Preset-specific defaults
12386 */
12387 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012388 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012389 /*
12390 * NSA Suite B
12391 */
12392 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012393#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012394 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012395#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12396#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012397 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012398#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12399#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012400 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012401#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12402#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012403 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012404#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012405
Hanno Becker73f4cb12019-06-27 13:51:07 +010012406#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012407 conf->ciphersuite_list[0] =
12408 conf->ciphersuite_list[1] =
12409 conf->ciphersuite_list[2] =
12410 conf->ciphersuite_list[3] =
12411 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012412#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012413
12414#if defined(MBEDTLS_X509_CRT_PARSE_C)
12415 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012416#endif
12417
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012418#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012419#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012420 conf->sig_hashes = ssl_preset_suiteb_hashes;
12421#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012422#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012423
12424#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012425#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012426 conf->curve_list = ssl_preset_suiteb_curves;
12427#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012428#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012429 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012430
12431 /*
12432 * Default
12433 */
12434 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012435#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012436 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12437 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12438 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12439 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012440#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12441#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012442 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12443 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12444 MBEDTLS_SSL_MIN_MINOR_VERSION :
12445 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012446#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012447 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012448 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12449#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012450#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12451#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12452 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12453#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12454#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12455 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12456#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012457
Hanno Becker73f4cb12019-06-27 13:51:07 +010012458#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012459 conf->ciphersuite_list[0] =
12460 conf->ciphersuite_list[1] =
12461 conf->ciphersuite_list[2] =
12462 conf->ciphersuite_list[3] =
12463 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012464#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012465
12466#if defined(MBEDTLS_X509_CRT_PARSE_C)
12467 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12468#endif
12469
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012470#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012471#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012472 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012473#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012474#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012475
12476#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012477#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012478 conf->curve_list = mbedtls_ecp_grp_id_list();
12479#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012480#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012481
12482#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12483 conf->dhm_min_bitlen = 1024;
12484#endif
12485 }
12486
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012487 return( 0 );
12488}
12489
12490/*
12491 * Free mbedtls_ssl_config
12492 */
12493void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12494{
12495#if defined(MBEDTLS_DHM_C)
12496 mbedtls_mpi_free( &conf->dhm_P );
12497 mbedtls_mpi_free( &conf->dhm_G );
12498#endif
12499
12500#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12501 if( conf->psk != NULL )
12502 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012503 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012504 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012505 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012506 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012507 }
12508
12509 if( conf->psk_identity != NULL )
12510 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012511 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012512 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012513 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012514 conf->psk_identity_len = 0;
12515 }
12516#endif
12517
12518#if defined(MBEDTLS_X509_CRT_PARSE_C)
12519 ssl_key_cert_free( conf->key_cert );
12520#endif
12521
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012522 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012523}
12524
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012525#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012526 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12527 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012528/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012529 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012531unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012533#if defined(MBEDTLS_RSA_C)
12534 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12535 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012536#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012537#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012538 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12539 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012540#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012541 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012542}
12543
Hanno Becker7e5437a2017-04-28 17:15:26 +010012544unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12545{
12546 switch( type ) {
12547 case MBEDTLS_PK_RSA:
12548 return( MBEDTLS_SSL_SIG_RSA );
12549 case MBEDTLS_PK_ECDSA:
12550 case MBEDTLS_PK_ECKEY:
12551 return( MBEDTLS_SSL_SIG_ECDSA );
12552 default:
12553 return( MBEDTLS_SSL_SIG_ANON );
12554 }
12555}
12556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012557mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012558{
12559 switch( sig )
12560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012561#if defined(MBEDTLS_RSA_C)
12562 case MBEDTLS_SSL_SIG_RSA:
12563 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012564#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012565#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012566 case MBEDTLS_SSL_SIG_ECDSA:
12567 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012568#endif
12569 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012570 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012571 }
12572}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012573#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012574
Hanno Becker7e5437a2017-04-28 17:15:26 +010012575#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12576 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12577
12578/* Find an entry in a signature-hash set matching a given hash algorithm. */
12579mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12580 mbedtls_pk_type_t sig_alg )
12581{
12582 switch( sig_alg )
12583 {
12584 case MBEDTLS_PK_RSA:
12585 return( set->rsa );
12586 case MBEDTLS_PK_ECDSA:
12587 return( set->ecdsa );
12588 default:
12589 return( MBEDTLS_MD_NONE );
12590 }
12591}
12592
12593/* Add a signature-hash-pair to a signature-hash set */
12594void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12595 mbedtls_pk_type_t sig_alg,
12596 mbedtls_md_type_t md_alg )
12597{
12598 switch( sig_alg )
12599 {
12600 case MBEDTLS_PK_RSA:
12601 if( set->rsa == MBEDTLS_MD_NONE )
12602 set->rsa = md_alg;
12603 break;
12604
12605 case MBEDTLS_PK_ECDSA:
12606 if( set->ecdsa == MBEDTLS_MD_NONE )
12607 set->ecdsa = md_alg;
12608 break;
12609
12610 default:
12611 break;
12612 }
12613}
12614
12615/* Allow exactly one hash algorithm for each signature. */
12616void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12617 mbedtls_md_type_t md_alg )
12618{
12619 set->rsa = md_alg;
12620 set->ecdsa = md_alg;
12621}
12622
12623#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12624 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12625
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012626/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012627 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012629mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012630{
12631 switch( hash )
12632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012633#if defined(MBEDTLS_MD5_C)
12634 case MBEDTLS_SSL_HASH_MD5:
12635 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012636#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012637#if defined(MBEDTLS_SHA1_C)
12638 case MBEDTLS_SSL_HASH_SHA1:
12639 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012640#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012641#if defined(MBEDTLS_SHA256_C)
12642 case MBEDTLS_SSL_HASH_SHA224:
12643 return( MBEDTLS_MD_SHA224 );
12644 case MBEDTLS_SSL_HASH_SHA256:
12645 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012646#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012647#if defined(MBEDTLS_SHA512_C)
12648 case MBEDTLS_SSL_HASH_SHA384:
12649 return( MBEDTLS_MD_SHA384 );
12650 case MBEDTLS_SSL_HASH_SHA512:
12651 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012652#endif
12653 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012654 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012655 }
12656}
12657
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012658/*
12659 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12660 */
12661unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12662{
12663 switch( md )
12664 {
12665#if defined(MBEDTLS_MD5_C)
12666 case MBEDTLS_MD_MD5:
12667 return( MBEDTLS_SSL_HASH_MD5 );
12668#endif
12669#if defined(MBEDTLS_SHA1_C)
12670 case MBEDTLS_MD_SHA1:
12671 return( MBEDTLS_SSL_HASH_SHA1 );
12672#endif
12673#if defined(MBEDTLS_SHA256_C)
12674 case MBEDTLS_MD_SHA224:
12675 return( MBEDTLS_SSL_HASH_SHA224 );
12676 case MBEDTLS_MD_SHA256:
12677 return( MBEDTLS_SSL_HASH_SHA256 );
12678#endif
12679#if defined(MBEDTLS_SHA512_C)
12680 case MBEDTLS_MD_SHA384:
12681 return( MBEDTLS_SSL_HASH_SHA384 );
12682 case MBEDTLS_MD_SHA512:
12683 return( MBEDTLS_SSL_HASH_SHA512 );
12684#endif
12685 default:
12686 return( MBEDTLS_SSL_HASH_NONE );
12687 }
12688}
12689
Hanno Beckeree902df2019-08-23 13:47:47 +010012690#if defined(MBEDTLS_USE_TINYCRYPT)
12691/*
12692 * Check if a curve proposed by the peer is in our list.
12693 * Return 0 if we're willing to use it, -1 otherwise.
12694 */
12695int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12696 mbedtls_uecc_group_id grp_id )
12697{
12698 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12699 if( own_ec_id == grp_id )
12700 return( 0 );
12701 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12702
12703 return( -1 );
12704}
12705#endif /* MBEDTLS_USE_TINYCRYPT */
12706
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012707#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012708/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012709 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012710 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012711 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012712int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12713 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012714{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012715 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12716 if( own_ec_id == grp_id )
12717 return( 0 );
12718 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012719
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012720 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012721}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012722#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012723
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012724#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012725/*
12726 * Check if a hash proposed by the peer is in our list.
12727 * Return 0 if we're willing to use it, -1 otherwise.
12728 */
12729int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12730 mbedtls_md_type_t md )
12731{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012732 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12733 if( md_alg == md )
12734 return( 0 );
12735 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012736
12737 return( -1 );
12738}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012739#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012741#if defined(MBEDTLS_X509_CRT_PARSE_C)
12742int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012743 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012744 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012745 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012746{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012747 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012748#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012749 int usage = 0;
12750#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012751#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012752 const char *ext_oid;
12753 size_t ext_len;
12754#endif
12755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012756#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12757 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012758 ((void) cert);
12759 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012760 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012761#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012763#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12764 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012765 {
12766 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012767 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012769 case MBEDTLS_KEY_EXCHANGE_RSA:
12770 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012771 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012772 break;
12773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012774 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12775 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12776 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12777 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012778 break;
12779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012780 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12781 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012782 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012783 break;
12784
12785 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012786 case MBEDTLS_KEY_EXCHANGE_NONE:
12787 case MBEDTLS_KEY_EXCHANGE_PSK:
12788 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12789 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012790 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012791 usage = 0;
12792 }
12793 }
12794 else
12795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012796 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12797 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012798 }
12799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012800 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012801 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012802 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012803 ret = -1;
12804 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012805#else
12806 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012807#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012809#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12810 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012812 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12813 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012814 }
12815 else
12816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012817 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12818 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012819 }
12820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012821 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012822 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012823 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012824 ret = -1;
12825 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012826#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012827
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012828 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012830#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012831
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012832#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12833 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12834int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12835 unsigned char *output,
12836 unsigned char *data, size_t data_len )
12837{
12838 int ret = 0;
12839 mbedtls_md5_context mbedtls_md5;
12840 mbedtls_sha1_context mbedtls_sha1;
12841
12842 mbedtls_md5_init( &mbedtls_md5 );
12843 mbedtls_sha1_init( &mbedtls_sha1 );
12844
12845 /*
12846 * digitally-signed struct {
12847 * opaque md5_hash[16];
12848 * opaque sha_hash[20];
12849 * };
12850 *
12851 * md5_hash
12852 * MD5(ClientHello.random + ServerHello.random
12853 * + ServerParams);
12854 * sha_hash
12855 * SHA(ClientHello.random + ServerHello.random
12856 * + ServerParams);
12857 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012858 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012859 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012861 goto exit;
12862 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012863 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012864 ssl->handshake->randbytes, 64 ) ) != 0 )
12865 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012867 goto exit;
12868 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012869 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012870 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012872 goto exit;
12873 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012874 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012875 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012877 goto exit;
12878 }
12879
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012880 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012881 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012883 goto exit;
12884 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012885 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012886 ssl->handshake->randbytes, 64 ) ) != 0 )
12887 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012888 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012889 goto exit;
12890 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012891 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012892 data_len ) ) != 0 )
12893 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012895 goto exit;
12896 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012897 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012898 output + 16 ) ) != 0 )
12899 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012901 goto exit;
12902 }
12903
12904exit:
12905 mbedtls_md5_free( &mbedtls_md5 );
12906 mbedtls_sha1_free( &mbedtls_sha1 );
12907
12908 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012909 mbedtls_ssl_pend_fatal_alert( ssl,
12910 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012911
12912 return( ret );
12913
12914}
12915#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12916 MBEDTLS_SSL_PROTO_TLS1_1 */
12917
12918#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12919 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12920int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012921 unsigned char *hash, size_t *hashlen,
12922 unsigned char *data, size_t data_len,
12923 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012924{
12925 int ret = 0;
12926 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012927 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012928 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012929
12930 mbedtls_md_init( &ctx );
12931
12932 /*
12933 * digitally-signed struct {
12934 * opaque client_random[32];
12935 * opaque server_random[32];
12936 * ServerDHParams params;
12937 * };
12938 */
12939 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12940 {
12941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12942 goto exit;
12943 }
12944 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12945 {
12946 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12947 goto exit;
12948 }
12949 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12950 {
12951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12952 goto exit;
12953 }
12954 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12955 {
12956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12957 goto exit;
12958 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012959 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012960 {
12961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12962 goto exit;
12963 }
12964
12965exit:
12966 mbedtls_md_free( &ctx );
12967
12968 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012969 mbedtls_ssl_pend_fatal_alert( ssl,
12970 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012971
12972 return( ret );
12973}
12974#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12975 MBEDTLS_SSL_PROTO_TLS1_2 */
12976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012977#endif /* MBEDTLS_SSL_TLS_C */