blob: 4f41ac9fe1e80d398a0e08e178bdc04303af8f6c [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;
Hanno Becker03e2db62019-07-12 14:40:00 +0100180 mbedtls_record rec;
181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
182 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
183
184 /* We don't support record checking in TLS because
185 * (a) there doesn't seem to be a usecase for it, and
186 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
187 * and we'd need to backup the transform here.
188 */
189#if defined(MBEDTLS_SSL_PROTO_TLS)
190 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
191 {
192 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
193 goto exit;
194 }
195 MBEDTLS_SSL_TRANSPORT_ELSE
196#endif /* MBEDTLS_SSL_PROTO_TLS */
197#if defined(MBEDTLS_SSL_PROTO_DTLS)
198 {
199 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
200 if( ret != 0 )
201 {
202 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
203 goto exit;
204 }
205
206 if( ssl->transform_in != NULL )
207 {
208 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
209 if( ret != 0 )
210 {
211 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
212 goto exit;
213 }
214 }
215 }
216#endif /* MBEDTLS_SSL_PROTO_DTLS */
217
218exit:
219 /* On success, we have decrypted the buffer in-place, so make
220 * sure we don't leak any plaintext data. */
221 mbedtls_platform_zeroize( buf, buflen );
222
223 /* For the purpose of this API, treat messages with unexpected CID
224 * as well as such from future epochs as unexpected. */
225 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
226 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
227 {
228 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
229 }
230
231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
232 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100233}
234#endif /* MBEDTLS_SSL_RECORD_CHECKING */
235
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236#define SSL_DONT_FORCE_FLUSH 0
237#define SSL_FORCE_FLUSH 1
238
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100240
Hanno Beckera5a2b082019-05-15 14:03:01 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100242/* Top-level Connection ID API */
243
Hanno Beckere0200da2019-06-13 09:23:43 +0100244#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
245 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
246 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100247int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
248 size_t len,
249 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100250{
251 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
253
Hanno Becker791ec6b2019-05-14 11:45:26 +0100254 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
255 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
256 {
257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
258 }
259
260 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100261 conf->cid_len = len;
262 return( 0 );
263}
Hanno Beckere0200da2019-06-13 09:23:43 +0100264#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
265 !MBEDTLS_SSL_CONF_CID_LEN &&
266 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
267
268#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
269#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
270#endif
271#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
272 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
273#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
274#endif
275
276#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
277 !MBEDTLS_SSL_CONF_CID_LEN &&
278 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100279
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100280int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
281 int enable,
282 unsigned char const *own_cid,
283 size_t own_cid_len )
284{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200285 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
287
Hanno Becker07489862019-04-25 16:01:49 +0100288 ssl->negotiate_cid = enable;
289 if( enable == MBEDTLS_SSL_CID_DISABLED )
290 {
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
292 return( 0 );
293 }
294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100295 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100296
Hanno Beckere0200da2019-06-13 09:23:43 +0100297 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100298 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
300 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100301 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
303 }
304
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300305 /* Not using more secure mbedtls_platform_memcpy as cid is public */
306 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100307 /* Truncation is not an issue here because
308 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
309 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100310
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100311 return( 0 );
312}
313
314int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
315 int *enabled,
316 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
317 size_t *peer_cid_len )
318{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100319 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200321 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100322 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
323 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100325 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100326
Hanno Beckercb063f52019-05-03 12:54:52 +0100327 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
328 * were used, but client and server requested the empty CID.
329 * This is indistinguishable from not using the CID extension
330 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100331 if( ssl->transform_in->in_cid_len == 0 &&
332 ssl->transform_in->out_cid_len == 0 )
333 {
334 return( 0 );
335 }
336
Hanno Becker633d6042019-05-22 16:50:35 +0100337 if( peer_cid_len != NULL )
338 {
339 *peer_cid_len = ssl->transform_in->out_cid_len;
340 if( peer_cid != NULL )
341 {
Teppo Järvelin6f4e0302019-10-04 13:53:53 +0300342 /* Not using more secure mbedtls_platform_memcpy as cid is public */
343 memcpy( peer_cid, ssl->transform_in->out_cid,
Hanno Becker633d6042019-05-22 16:50:35 +0100344 ssl->transform_in->out_cid_len );
345 }
346 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100347
348 *enabled = MBEDTLS_SSL_CID_ENABLED;
349
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100350 return( 0 );
351}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100352#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100353
Hanno Beckerd5847772018-08-28 10:09:23 +0100354/* Forward declarations for functions related to message buffering. */
355static void ssl_buffering_free( mbedtls_ssl_context *ssl );
356static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
357 uint8_t slot );
358static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
359static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
360static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
361static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100362static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
363 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100364static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100365
Hanno Beckera67dee22018-08-22 10:05:20 +0100366static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100367static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368{
Hanno Becker11682cc2018-08-22 14:41:02 +0100369 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100370
371 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100372 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100373
374 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
375}
376
Hanno Becker67bc7c32018-08-06 11:33:50 +0100377static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
378{
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 size_t const bytes_written = ssl->out_left;
380 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100381
382 /* Double-check that the write-index hasn't gone
383 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100384 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100385 {
386 /* Should never happen... */
387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
388 }
389
390 return( (int) ( mtu - bytes_written ) );
391}
392
393static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
394{
395 int ret;
396 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400397 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100398
399#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
400 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
401
402 if( max_len > mfl )
403 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100404
405 /* By the standard (RFC 6066 Sect. 4), the MFL extension
406 * only limits the maximum record payload size, so in theory
407 * we would be allowed to pack multiple records of payload size
408 * MFL into a single datagram. However, this would mean that there's
409 * no way to explicitly communicate MTU restrictions to the peer.
410 *
411 * The following reduction of max_len makes sure that we never
412 * write datagrams larger than MFL + Record Expansion Overhead.
413 */
414 if( max_len <= ssl->out_left )
415 return( 0 );
416
417 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100418#endif
419
420 ret = ssl_get_remaining_space_in_datagram( ssl );
421 if( ret < 0 )
422 return( ret );
423 remaining = (size_t) ret;
424
425 ret = mbedtls_ssl_get_record_expansion( ssl );
426 if( ret < 0 )
427 return( ret );
428 expansion = (size_t) ret;
429
430 if( remaining <= expansion )
431 return( 0 );
432
433 remaining -= expansion;
434 if( remaining >= max_len )
435 remaining = max_len;
436
437 return( (int) remaining );
438}
439
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440/*
441 * Double the retransmit timeout value, within the allowed range,
442 * returning -1 if the maximum value has already been reached.
443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200445{
446 uint32_t new_timeout;
447
Hanno Becker1f835fa2019-06-13 10:14:59 +0100448 if( ssl->handshake->retransmit_timeout >=
449 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
450 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200451 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100452 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200453
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200454 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
455 * in the following way: after the initial transmission and a first
456 * retransmission, back off to a temporary estimated MTU of 508 bytes.
457 * This value is guaranteed to be deliverable (if not guaranteed to be
458 * delivered) of any compliant IPv4 (and IPv6) network, and should work
459 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100460 if( ssl->handshake->retransmit_timeout !=
461 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400462 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200463 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
465 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200466
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 new_timeout = 2 * ssl->handshake->retransmit_timeout;
468
469 /* Avoid arithmetic overflow and range overflow */
470 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100471 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200472 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100473 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200474 }
475
476 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200478 ssl->handshake->retransmit_timeout ) );
479
480 return( 0 );
481}
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200484{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100485 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200487 ssl->handshake->retransmit_timeout ) );
488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200492/*
493 * Convert max_fragment_length codes to length.
494 * RFC 6066 says:
495 * enum{
496 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
497 * } MaxFragmentLength;
498 * and we add 0 -> extension unused
499 */
Angus Grattond8213d02016-05-25 20:56:48 +1000500static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200501{
Angus Grattond8213d02016-05-25 20:56:48 +1000502 switch( mfl )
503 {
504 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300505 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000506 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
507 return 512;
508 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
509 return 1024;
510 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
511 return 2048;
512 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
513 return 4096;
514 default:
Arto Kinnunen4f4849a2019-09-09 10:21:18 +0300515 return( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
Angus Grattond8213d02016-05-25 20:56:48 +1000516 }
517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200519
Hanno Becker58fccf22019-02-06 14:30:46 +0000520int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
521 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_ssl_session_free( dst );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300524 mbedtls_platform_memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000527
528#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200529 if( src->peer_cert != NULL )
530 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200531 int ret;
532
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200533 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200534 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200535 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200540 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200543 dst->peer_cert = NULL;
544 return( ret );
545 }
546 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100547#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000548 if( src->peer_cert_digest != NULL )
549 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000550 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000551 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000552 if( dst->peer_cert_digest == NULL )
553 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
554
Teppo Järvelin91d79382019-10-02 09:09:31 +0300555 mbedtls_platform_memcpy( dst->peer_cert_digest, src->peer_cert_digest,
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556 src->peer_cert_digest_len );
557 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000558 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000559 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100560#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200563
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200564#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565 if( src->ticket != NULL )
566 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200567 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200568 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200569 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200570
Teppo Järvelin91d79382019-10-02 09:09:31 +0300571 mbedtls_platform_memcpy( dst->ticket, src->ticket, src->ticket_len );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200572 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200573#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200574
575 return( 0 );
576}
577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
579int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200580 const unsigned char *key_enc, const unsigned char *key_dec,
581 size_t keylen,
582 const unsigned char *iv_enc, const unsigned char *iv_dec,
583 size_t ivlen,
584 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200585 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
587int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
588int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
589int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
590int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
591#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000592
Paul Bakker5121ce52009-01-03 21:22:43 +0000593/*
594 * Key material generation
595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100597MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200598 const char *label,
599 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000600 unsigned char *dstbuf, size_t dlen )
601{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100602 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000603 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200604 mbedtls_md5_context md5;
605 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000606 unsigned char padding[16];
607 unsigned char sha1sum[20];
608 ((void)label);
609
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200610 mbedtls_md5_init( &md5 );
611 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200612
Paul Bakker5f70b252012-09-13 14:23:06 +0000613 /*
614 * SSLv3:
615 * block =
616 * MD5( secret + SHA1( 'A' + secret + random ) ) +
617 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
618 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
619 * ...
620 */
621 for( i = 0; i < dlen / 16; i++ )
622 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200623 mbedtls_platform_memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000624
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100625 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100626 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100627 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100628 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100629 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100630 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000635
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100636 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100637 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100638 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100639 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100640 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100642 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100643 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000644 }
645
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100646exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200647 mbedtls_md5_free( &md5 );
648 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500650 mbedtls_platform_zeroize( padding, sizeof( padding ) );
651 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000652
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100653 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100658MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200659 const char *label,
660 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000661 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000662{
Paul Bakker23986e52011-04-24 08:57:21 +0000663 size_t nb, hs;
664 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200665 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 unsigned char tmp[128];
667 unsigned char h_i[20];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100668 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100670 int ret;
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
674 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
677 hs = ( slen + 1 ) / 2;
678 S1 = secret;
679 S2 = secret + slen - hs;
680
681 nb = strlen( label );
Teppo Järvelin91d79382019-10-02 09:09:31 +0300682 mbedtls_platform_memcpy( tmp + 20, label, nb );
683 mbedtls_platform_memcpy( tmp + 20 + nb, random, rlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 nb += rlen;
685
686 /*
687 * First compute P_md5(secret,label+random)[0..dlen]
688 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100689 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) ==
690 MBEDTLS_MD_INVALID_HANDLE )
691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100693 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100696 return( ret );
697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
699 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
700 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
702 for( i = 0; i < dlen; i += 16 )
703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_md_hmac_reset ( &md_ctx );
705 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
706 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_md_hmac_reset ( &md_ctx );
709 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
710 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
712 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
713
714 for( j = 0; j < k; j++ )
715 dstbuf[i + j] = h_i[j];
716 }
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719
Paul Bakker5121ce52009-01-03 21:22:43 +0000720 /*
721 * XOR out with P_sha1(secret,label+random)[0..dlen]
722 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100723 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) ==
724 MBEDTLS_MD_INVALID_HANDLE )
725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100727 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730 return( ret );
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
733 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
734 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
736 for( i = 0; i < dlen; i += 20 )
737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 mbedtls_md_hmac_reset ( &md_ctx );
739 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
740 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_md_hmac_reset ( &md_ctx );
743 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
744 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
746 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
747
748 for( j = 0; j < k; j++ )
749 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
750 }
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100753
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500754 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
755 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757 return( 0 );
758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100762#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
763MBEDTLS_ALWAYS_INLINE static inline
764#else
765static
766#endif
767int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100768 const unsigned char *secret, size_t slen,
769 const char *label,
770 const unsigned char *random, size_t rlen,
771 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000772{
773 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000775 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100777 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100779 int ret;
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000782
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100783 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) ==
784 MBEDTLS_MD_INVALID_HANDLE )
785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100787 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100790
791 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000793
794 nb = strlen( label );
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200795 (void)mbedtls_platform_memcpy( tmp + md_len, label, nb );
796 (void)mbedtls_platform_memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000797 nb += rlen;
798
799 /*
800 * Compute P_<hash>(secret, label + random)[0..dlen]
801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100803 return( ret );
804
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200805 if ( ( ret = mbedtls_md_hmac_starts( &md_ctx, secret, slen ) ) != 0 )
806 return( ret );
807 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ) ) != 0 )
808 return( ret );
809 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
810 return( ret );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100811
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100812 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000813 {
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200814 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
815 return( ret );
816 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ) ) != 0 )
817 return( ret );
818 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, h_i ) ) != 0 )
819 return( ret );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100820
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200821 if ( ( ret = mbedtls_md_hmac_reset ( &md_ctx ) ) != 0 )
822 return( ret );
823 if ( ( ret = mbedtls_md_hmac_update( &md_ctx, tmp, md_len ) ) != 0 )
824 return( ret );
825 if ( ( ret = mbedtls_md_hmac_finish( &md_ctx, tmp ) ) != 0 )
826 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000827
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100828 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000829
830 for( j = 0; j < k; j++ )
831 dstbuf[i + j] = h_i[j];
832 }
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100835
Teppo Järvelin8f7e36f2020-01-02 10:40:19 +0200836 (void)mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
837 (void)mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000838
839 return( 0 );
840}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100843MBEDTLS_NO_INLINE static int tls_prf_sha256(
844 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100845 const char *label,
846 const unsigned char *random, size_t rlen,
847 unsigned char *dstbuf, size_t dlen )
848{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100850 label, random, rlen, dstbuf, dlen ) );
851}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100855MBEDTLS_NO_INLINE static int tls_prf_sha384(
856 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200857 const char *label,
858 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000859 unsigned char *dstbuf, size_t dlen )
860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100862 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000863}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864#endif /* MBEDTLS_SHA512_C */
865#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000866
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100867/*
868 * Call the appropriate PRF function
869 */
Hanno Becker2793f742019-08-16 14:28:43 +0100870MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100871 mbedtls_md_type_t hash,
872 const unsigned char *secret, size_t slen,
873 const char *label,
874 const unsigned char *random, size_t rlen,
875 unsigned char *dstbuf, size_t dlen )
876{
877#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
878 (void) hash;
879#endif
880
881#if defined(MBEDTLS_SSL_PROTO_SSL3)
882 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
883 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
884 else
885#endif
886#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +0100887 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100888 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
889 else
890#endif
891#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
892#if defined(MBEDTLS_SHA512_C)
893 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
894 hash == MBEDTLS_MD_SHA384 )
895 {
896 return( tls_prf_sha384( secret, slen, label, random, rlen,
897 dstbuf, dlen ) );
898 }
899 else
900#endif
901#if defined(MBEDTLS_SHA256_C)
902 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
903 {
904 return( tls_prf_sha256( secret, slen, label, random, rlen,
905 dstbuf, dlen ) );
906 }
907#endif
908#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
909
910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
911}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200912
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100913#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100914MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100915 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
916{
917 const char *sender;
918 mbedtls_md5_context md5;
919 mbedtls_sha1_context sha1;
920
921 unsigned char padbuf[48];
922 unsigned char md5sum[16];
923 unsigned char sha1sum[20];
924
925 mbedtls_ssl_session *session = ssl->session_negotiate;
926 if( !session )
927 session = ssl->session;
928
929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
930
931 mbedtls_md5_init( &md5 );
932 mbedtls_sha1_init( &sha1 );
933
934 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
935 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
936
937 /*
938 * SSLv3:
939 * hash =
940 * MD5( master + pad2 +
941 * MD5( handshake + sender + master + pad1 ) )
942 * + SHA1( master + pad2 +
943 * SHA1( handshake + sender + master + pad1 ) )
944 */
945
946#if !defined(MBEDTLS_MD5_ALT)
947 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
948 md5.state, sizeof( md5.state ) );
949#endif
950
951#if !defined(MBEDTLS_SHA1_ALT)
952 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
953 sha1.state, sizeof( sha1.state ) );
954#endif
955
956 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
957 : "SRVR";
958
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200959 mbedtls_platform_memset( padbuf, 0x36, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100960
961 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
962 mbedtls_md5_update_ret( &md5, session->master, 48 );
963 mbedtls_md5_update_ret( &md5, padbuf, 48 );
964 mbedtls_md5_finish_ret( &md5, md5sum );
965
966 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
967 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
968 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
969 mbedtls_sha1_finish_ret( &sha1, sha1sum );
970
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200971 mbedtls_platform_memset( padbuf, 0x5C, 48 );
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100972
973 mbedtls_md5_starts_ret( &md5 );
974 mbedtls_md5_update_ret( &md5, session->master, 48 );
975 mbedtls_md5_update_ret( &md5, padbuf, 48 );
976 mbedtls_md5_update_ret( &md5, md5sum, 16 );
977 mbedtls_md5_finish_ret( &md5, buf );
978
979 mbedtls_sha1_starts_ret( &sha1 );
980 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
981 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
982 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
983 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
984
985 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
986
987 mbedtls_md5_free( &md5 );
988 mbedtls_sha1_free( &sha1 );
989
990 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
991 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
992 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
993
994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
995}
996#endif /* MBEDTLS_SSL_PROTO_SSL3 */
997
998#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100999MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001000 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1001{
1002 int len = 12;
1003 const char *sender;
1004 mbedtls_md5_context md5;
1005 mbedtls_sha1_context sha1;
1006 unsigned char padbuf[36];
1007
1008 mbedtls_ssl_session *session = ssl->session_negotiate;
1009 if( !session )
1010 session = ssl->session;
1011
1012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
1013
1014 mbedtls_md5_init( &md5 );
1015 mbedtls_sha1_init( &sha1 );
1016
1017 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1018 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1019
1020 /*
1021 * TLSv1:
1022 * hash = PRF( master, finished_label,
1023 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1024 */
1025
1026#if !defined(MBEDTLS_MD5_ALT)
1027 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1028 md5.state, sizeof( md5.state ) );
1029#endif
1030
1031#if !defined(MBEDTLS_SHA1_ALT)
1032 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1033 sha1.state, sizeof( sha1.state ) );
1034#endif
1035
1036 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1037 ? "client finished"
1038 : "server finished";
1039
1040 mbedtls_md5_finish_ret( &md5, padbuf );
1041 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1042
1043 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1044 mbedtls_ssl_suite_get_mac(
1045 mbedtls_ssl_ciphersuite_from_id(
1046 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1047 session->master, 48, sender,
1048 padbuf, 36, buf, len );
1049
1050 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1051
1052 mbedtls_md5_free( &md5 );
1053 mbedtls_sha1_free( &sha1 );
1054
1055 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1056
1057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1058}
1059#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1060
1061#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1062#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001063MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001064 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1065{
1066 int len = 12;
1067 const char *sender;
1068 mbedtls_sha256_context sha256;
1069 unsigned char padbuf[32];
1070
1071 mbedtls_ssl_session *session = ssl->session_negotiate;
1072 if( !session )
1073 session = ssl->session;
1074
1075 mbedtls_sha256_init( &sha256 );
1076
1077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1078
1079 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1080
1081 /*
1082 * TLSv1.2:
1083 * hash = PRF( master, finished_label,
1084 * Hash( handshake ) )[0.11]
1085 */
1086
1087#if !defined(MBEDTLS_SHA256_ALT)
1088 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1089 sha256.state, sizeof( sha256.state ) );
1090#endif
1091
1092 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1093 ? "client finished"
1094 : "server finished";
1095
1096 mbedtls_sha256_finish_ret( &sha256, padbuf );
1097
1098 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1099 mbedtls_ssl_suite_get_mac(
1100 mbedtls_ssl_ciphersuite_from_id(
1101 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1102 session->master, 48, sender,
1103 padbuf, 32, buf, len );
1104
1105 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1106
1107 mbedtls_sha256_free( &sha256 );
1108
1109 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1110
1111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1112}
1113#endif /* MBEDTLS_SHA256_C */
1114
1115#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001116MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001117 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1118{
1119 int len = 12;
1120 const char *sender;
1121 mbedtls_sha512_context sha512;
1122 unsigned char padbuf[48];
1123
1124 mbedtls_ssl_session *session = ssl->session_negotiate;
1125 if( !session )
1126 session = ssl->session;
1127
1128 mbedtls_sha512_init( &sha512 );
1129
1130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1131
1132 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1133
1134 /*
1135 * TLSv1.2:
1136 * hash = PRF( master, finished_label,
1137 * Hash( handshake ) )[0.11]
1138 */
1139
1140#if !defined(MBEDTLS_SHA512_ALT)
1141 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1142 sha512.state, sizeof( sha512.state ) );
1143#endif
1144
1145 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1146 ? "client finished"
1147 : "server finished";
1148
1149 mbedtls_sha512_finish_ret( &sha512, padbuf );
1150
1151 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1152 mbedtls_ssl_suite_get_mac(
1153 mbedtls_ssl_ciphersuite_from_id(
1154 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1155 session->master, 48, sender,
1156 padbuf, 48, buf, len );
1157
1158 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1159
1160 mbedtls_sha512_free( &sha512 );
1161
1162 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1163
1164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1165}
1166#endif /* MBEDTLS_SHA512_C */
1167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1168
Hanno Becker2793f742019-08-16 14:28:43 +01001169MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1170 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001171 mbedtls_md_type_t hash,
1172 mbedtls_ssl_context *ssl,
1173 unsigned char *buf,
1174 int from )
1175{
1176#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1177 (void) hash;
1178#endif
1179
1180#if defined(MBEDTLS_SSL_PROTO_SSL3)
1181 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1182 ssl_calc_finished_ssl( ssl, buf, from );
1183 else
1184#endif
1185#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001186 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001187 ssl_calc_finished_tls( ssl, buf, from );
1188 else
1189#endif
1190#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1191#if defined(MBEDTLS_SHA512_C)
1192 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1193 hash == MBEDTLS_MD_SHA384 )
1194 {
1195 ssl_calc_finished_tls_sha384( ssl, buf, from );
1196 }
1197 else
1198#endif
1199#if defined(MBEDTLS_SHA256_C)
1200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1201 ssl_calc_finished_tls_sha256( ssl, buf, from );
1202 else
1203#endif
1204#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1205 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1206
1207 return( 0 );
1208}
1209
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001210/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001211 * Populate a transform structure with session keys and all the other
1212 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001213 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001214 * Parameters:
1215 * - [in/out]: transform: structure to populate
1216 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001217 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001218 * - [in] ciphersuite
1219 * - [in] master
1220 * - [in] encrypt_then_mac
1221 * - [in] trunc_hmac
1222 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001223 * - [in] tls_prf: pointer to PRF to use for key derivation
1224 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001225 * - [in] minor_ver: SSL/TLS minor version
1226 * - [in] endpoint: client or server
1227 * - [in] ssl: optionally used for:
1228 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1229 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1230 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001231 */
Hanno Becker298a4702019-08-16 10:21:32 +01001232/* Force compilers to inline this function if it's used only
1233 * from one place, because at least ARMC5 doesn't do that
1234 * automatically. */
1235#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1236MBEDTLS_ALWAYS_INLINE static inline
1237#else
1238static
1239#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1240int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001241 int ciphersuite,
1242 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001243#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001244#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1245 int encrypt_then_mac,
1246#endif
1247#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1248 int trunc_hmac,
1249#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001250#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001251#if defined(MBEDTLS_ZLIB_SUPPORT)
1252 int compression,
1253#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001254 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001255 int minor_ver,
1256 unsigned endpoint,
Manuel Pégourié-Gonnard13bebd02020-03-13 11:28:19 +01001257#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1258 const
1259#endif
1260 mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001261{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001262 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 unsigned char keyblk[256];
1264 unsigned char *key1;
1265 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001266 unsigned char *mac_enc;
1267 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001268 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001269 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001270 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001271 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 const mbedtls_cipher_info_t *cipher_info;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001273 mbedtls_md_handle_t md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001274
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001275#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1276 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1277 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001278 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001279 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001280#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001281
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001282 /*
1283 * Some data just needs copying into the structure
1284 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001285#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1286 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001287 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001288#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001289
1290#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001291 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001292#else
1293 ((void) minor_ver);
1294#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001295
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001296#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Teppo Järvelin91d79382019-10-02 09:09:31 +03001297 mbedtls_platform_memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001298#endif
1299
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001300 /*
1301 * Get various info structures
1302 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001303 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001304 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001305 {
1306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001307 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1309 }
1310
Hanno Becker473f98f2019-06-26 10:27:32 +01001311 cipher_info = mbedtls_cipher_info_from_type(
1312 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001313 if( cipher_info == NULL )
1314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001316 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001318 }
1319
Hanno Becker473f98f2019-06-26 10:27:32 +01001320 md_info = mbedtls_md_info_from_type(
1321 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01001322 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakker68884e32013-01-07 18:20:04 +01001323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001325 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001327 }
1328
Hanno Beckera5a2b082019-05-15 14:03:01 +01001329#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001330 /* Copy own and peer's CID if the use of the CID
1331 * extension has been negotiated. */
1332 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1333 {
1334 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001335
Hanno Becker4932f9f2019-05-03 15:23:51 +01001336 transform->in_cid_len = ssl->own_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03001337 /* Not using more secure mbedtls_platform_memcpy as cid is public */
1338 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001339 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001340 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001341
1342 transform->out_cid_len = ssl->handshake->peer_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->out_cid, ssl->handshake->peer_cid,
Hanno Beckere582d122019-05-15 10:21:55 +01001345 ssl->handshake->peer_cid_len );
1346 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1347 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001348 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001349#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001350
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001352 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001353 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001354 ret = ssl_prf( minor_ver,
1355 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1356 master, 48, "key expansion", randbytes, 64,
1357 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001358 if( ret != 0 )
1359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001361 return( ret );
1362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001365 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001366 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001367 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 /*
1371 * Determine the appropriate key, IV and MAC length.
1372 */
Paul Bakker68884e32013-01-07 18:20:04 +01001373
Hanno Beckere7f2df02017-12-27 08:17:40 +00001374 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001375
Hanno Beckerf1229442018-01-03 15:32:31 +00001376#if defined(MBEDTLS_GCM_C) || \
1377 defined(MBEDTLS_CCM_C) || \
1378 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001380 cipher_info->mode == MBEDTLS_MODE_CCM ||
1381 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001383 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001384 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001385 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1386 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001387
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001388 /* All modes haves 96-bit IVs;
1389 * GCM and CCM has 4 implicit and 8 explicit bytes
1390 * ChachaPoly has all 12 bytes implicit
1391 */
Paul Bakker68884e32013-01-07 18:20:04 +01001392 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001393 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1394 transform->fixed_ivlen = 12;
1395 else
1396 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001397 }
1398 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001399#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1400#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1401 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1402 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001403 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001404 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1406 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001409 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001412 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001413 mac_key_len = mbedtls_md_get_size( md_info );
1414 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001417 /*
1418 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1419 * (rfc 6066 page 13 or rfc 2104 section 4),
1420 * so we only need to adjust the length here.
1421 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001422 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001425
1426#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1427 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001428 * HMAC implementation which also truncates the key
1429 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001430 mac_key_len = transform->maclen;
1431#endif
1432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001434
1435 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001436 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001438 else
1439#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1440 {
1441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1443 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
Hanno Beckera9d5c452019-07-25 16:47:12 +01001445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001446 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001447 (unsigned) transform->ivlen,
1448 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001449
1450 /*
1451 * Finally setup the cipher contexts, IVs and MAC secrets.
1452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001454 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001456 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001457 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001458
Paul Bakker68884e32013-01-07 18:20:04 +01001459 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001460 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001461
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001462 /*
1463 * This is not used in TLS v1.1.
1464 */
Paul Bakker48916f92012-09-16 19:57:18 +00001465 iv_copy_len = ( transform->fixed_ivlen ) ?
1466 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001467 mbedtls_platform_memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1468 mbedtls_platform_memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001469 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 }
1471 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472#endif /* MBEDTLS_SSL_CLI_C */
1473#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001474 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001475 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001476 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001477 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001478
Hanno Becker81c7b182017-11-09 18:39:33 +00001479 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001480 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001481
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001482 /*
1483 * This is not used in TLS v1.1.
1484 */
Paul Bakker48916f92012-09-16 19:57:18 +00001485 iv_copy_len = ( transform->fixed_ivlen ) ?
1486 transform->fixed_ivlen : transform->ivlen;
Teppo Järvelin91d79382019-10-02 09:09:31 +03001487 mbedtls_platform_memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1488 mbedtls_platform_memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001489 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001491 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001496 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001497
Hanno Becker92231322018-01-03 15:32:51 +00001498#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001500 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001501 {
Hanno Becker92231322018-01-03 15:32:51 +00001502 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001506 }
1507
Teppo Järvelin91d79382019-10-02 09:09:31 +03001508 mbedtls_platform_memcpy( transform->mac_enc, mac_enc, mac_key_len );
1509 mbedtls_platform_memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001510 }
1511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1513#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1514 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001515 if( mbedtls_ssl_ver_geq( minor_ver, MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakker68884e32013-01-07 18:20:04 +01001516 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001517 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1518 For AEAD-based ciphersuites, there is nothing to do here. */
1519 if( mac_key_len != 0 )
1520 {
1521 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1522 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1523 }
Paul Bakker68884e32013-01-07 18:20:04 +01001524 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001525 else
1526#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001530 }
Hanno Becker92231322018-01-03 15:32:51 +00001531#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1534 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001537
Hanno Beckere7f2df02017-12-27 08:17:40 +00001538 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001539 transform->iv_enc, transform->iv_dec,
1540 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001541 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001542 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1545 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001546 }
1547 }
Hanno Becker92231322018-01-03 15:32:51 +00001548#else
1549 ((void) mac_dec);
1550 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001552
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001553#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1554 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001555 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001556 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001557 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001558 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001559 iv_copy_len );
1560 }
1561#endif
1562
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001563 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001565 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001567 return( ret );
1568 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001569
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001570 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 cipher_info ) ) != 0 )
1572 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 return( ret );
1575 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001578 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582 return( ret );
1583 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001586 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001590 return( ret );
1591 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_CIPHER_MODE_CBC)
1594 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1597 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001600 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001601 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1604 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001607 return( ret );
1608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001612 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001614 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001616 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001619
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001620 mbedtls_platform_memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1621 mbedtls_platform_memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001622
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001623 if( deflateInit( &transform->ctx_deflate,
1624 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001625 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1628 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001629 }
1630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001632
Paul Bakker5121ce52009-01-03 21:22:43 +00001633 return( 0 );
1634}
1635
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001636#if defined(MBEDTLS_SSL_PROTO_SSL3)
1637static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1638 unsigned char hash[36],
1639 size_t *hlen )
1640{
1641 mbedtls_md5_context md5;
1642 mbedtls_sha1_context sha1;
1643 unsigned char pad_1[48];
1644 unsigned char pad_2[48];
1645
1646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1647
1648 mbedtls_md5_init( &md5 );
1649 mbedtls_sha1_init( &sha1 );
1650
1651 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1652 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1653
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02001654 mbedtls_platform_memset( pad_1, 0x36, 48 );
1655 mbedtls_platform_memset( pad_2, 0x5C, 48 );
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001656
1657 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1658 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1659 mbedtls_md5_finish_ret( &md5, hash );
1660
1661 mbedtls_md5_starts_ret( &md5 );
1662 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1663 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1664 mbedtls_md5_update_ret( &md5, hash, 16 );
1665 mbedtls_md5_finish_ret( &md5, hash );
1666
1667 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1668 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1669 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1670
1671 mbedtls_sha1_starts_ret( &sha1 );
1672 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1673 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1674 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1675 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1676
1677 *hlen = 36;
1678
1679 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1681
1682 mbedtls_md5_free( &md5 );
1683 mbedtls_sha1_free( &sha1 );
1684
1685 return;
1686}
1687#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1688
1689#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1690static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1691 unsigned char hash[36],
1692 size_t *hlen )
1693{
1694 mbedtls_md5_context md5;
1695 mbedtls_sha1_context sha1;
1696
1697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1698
1699 mbedtls_md5_init( &md5 );
1700 mbedtls_sha1_init( &sha1 );
1701
1702 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1703 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1704
1705 mbedtls_md5_finish_ret( &md5, hash );
1706 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1707
1708 *hlen = 36;
1709
1710 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1712
1713 mbedtls_md5_free( &md5 );
1714 mbedtls_sha1_free( &sha1 );
1715
1716 return;
1717}
1718#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1719
1720#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1721#if defined(MBEDTLS_SHA256_C)
1722static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1723 unsigned char hash[32],
1724 size_t *hlen )
1725{
1726 mbedtls_sha256_context sha256;
1727
1728 mbedtls_sha256_init( &sha256 );
1729
1730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1731
1732 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1733 mbedtls_sha256_finish_ret( &sha256, hash );
1734
1735 *hlen = 32;
1736
1737 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1739
1740 mbedtls_sha256_free( &sha256 );
1741
1742 return;
1743}
1744#endif /* MBEDTLS_SHA256_C */
1745
1746#if defined(MBEDTLS_SHA512_C)
1747static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1748 unsigned char hash[48],
1749 size_t *hlen )
1750{
1751 mbedtls_sha512_context sha512;
1752
1753 mbedtls_sha512_init( &sha512 );
1754
1755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1756
1757 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1758 mbedtls_sha512_finish_ret( &sha512, hash );
1759
1760 *hlen = 48;
1761
1762 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1764
1765 mbedtls_sha512_free( &sha512 );
1766
1767 return;
1768}
1769#endif /* MBEDTLS_SHA512_C */
1770#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1771
Hanno Becker2f41b242019-08-15 17:29:43 +01001772int mbedtls_ssl_calc_verify( int minor_ver,
1773 mbedtls_md_type_t hash,
1774 mbedtls_ssl_context const *ssl,
1775 unsigned char *dst,
1776 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001778#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1779 (void) hash;
1780#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001781
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001782#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001783 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001784 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001785 else
1786#endif
1787#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01001788 if( mbedtls_ssl_ver_lt( minor_ver, MBEDTLS_SSL_MINOR_VERSION_3 ) )
Hanno Becker2f41b242019-08-15 17:29:43 +01001789 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001790 else
1791#endif
1792#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1793#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001794 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1795 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001796 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001797 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001798 }
1799 else
1800#endif
1801#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001802 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001803 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001804 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001805 }
1806 else
1807#endif
1808#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1809 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1811 }
1812
1813 return( 0 );
1814}
1815
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001816/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001817 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001818 *
1819 * Parameters:
1820 * [in/out] handshake
1821 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1822 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001823 * [out] master
1824 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001825 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001826static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001827 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001828 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001829{
1830 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001831
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001832/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1833/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1834/* (void) ssl; */
1835/* #endif */
1836
1837 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1838 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001839
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001840#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02001841 if( handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001842 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001843 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1844 return( 0 );
1845 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001846#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001847
1848 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1849 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001850
1851#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001852 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1853 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001854 {
1855 unsigned char session_hash[48];
1856 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001857
Hanno Becker2f41b242019-08-15 17:29:43 +01001858 mbedtls_ssl_calc_verify(
1859 mbedtls_ssl_get_minor_ver( ssl ),
1860 mbedtls_ssl_suite_get_mac( ciphersuite ),
1861 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001862
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001863 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1864 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001865
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001866 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1867 mbedtls_ssl_suite_get_mac( ciphersuite ),
1868 handshake->premaster, handshake->pmslen,
1869 "extended master secret",
1870 session_hash, hash_len,
1871 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001872 }
1873 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001874#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001875 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001876 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1877 mbedtls_ssl_suite_get_mac( ciphersuite ),
1878 handshake->premaster, handshake->pmslen,
1879 "master secret",
1880 handshake->randbytes, 64,
1881 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001882 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001883 if( ret != 0 )
1884 {
1885 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1886 return( ret );
1887 }
1888
1889 mbedtls_platform_zeroize( handshake->premaster,
1890 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001891
1892 return( 0 );
1893}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001894
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001895int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1896{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001897 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001898
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001900 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_UNSET;
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001901 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001902 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001903 ssl->session_negotiate->master,
1904 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001905 if( ret != 0 )
1906 {
1907 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1908 return( ret );
1909 }
1910
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001911 /* Swap the client and server random values:
1912 * - MS derivation wanted client+server (RFC 5246 8.1)
1913 * - key derivation wants server+client (RFC 5246 6.3) */
1914 {
1915 unsigned char tmp[64];
Teppo Järvelin91d79382019-10-02 09:09:31 +03001916 mbedtls_platform_memcpy( tmp, ssl->handshake->randbytes, 64 );
1917 mbedtls_platform_memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1918 mbedtls_platform_memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001919 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1920 }
1921
1922 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001923 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001924 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1925 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001926#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001927#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001928 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001929#endif
1930#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001931 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001932#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001933#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001934#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001935 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001936#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001937 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001938 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001939 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1940 ssl );
Jarno Lamsa4031a452019-12-19 08:11:12 +02001941 if( ret == 0 )
1942 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02001943 mbedtls_platform_random_delay();
Jarno Lamsa4031a452019-12-19 08:11:12 +02001944 if( ret == 0 )
1945 {
1946 ssl->handshake->key_derivation_done = MBEDTLS_SSL_FI_FLAG_SET;
1947 }
1948 else
1949 {
1950 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1951 }
1952 }
1953 else
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001954 {
1955 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1956 return( ret );
1957 }
1958
1959 /* We no longer need Server/ClientHello.random values */
1960 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1961 sizeof( ssl->handshake->randbytes ) );
1962
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001963 /* Allocate compression buffer */
1964#if defined(MBEDTLS_ZLIB_SUPPORT)
jiblime92af9a92019-12-18 21:40:01 -08001965 if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001966 ssl->compress_buf == NULL )
1967 {
1968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1969 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1970 if( ssl->compress_buf == NULL )
1971 {
1972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001973 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001974 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1975 }
1976 }
1977#endif
1978
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1980
1981 return( 0 );
1982}
1983
Hanno Becker09d23642019-07-22 17:18:18 +01001984int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1985{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04001986 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Hanno Becker09d23642019-07-22 17:18:18 +01001987
1988 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1989 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Beckera3c2c172019-07-23 16:51:57 +01001990#if defined(MBEDTLS_USE_TINYCRYPT)
1991 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001992 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
Hanno Beckera3c2c172019-07-23 16:51:57 +01001993 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
Hanno Beckerecf5d3f2019-09-01 07:47:29 +01001994 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1995 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1996 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1997 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1998 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
Hanno Beckera3c2c172019-07-23 16:51:57 +01001999 {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002000 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2001 ssl->handshake->ecdh_privkey,
2002 ssl->handshake->premaster );
2003 if( ret == UECC_FAULT_DETECTED )
2004 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2005 if( ret != UECC_SUCCESS )
Hanno Beckera3c2c172019-07-23 16:51:57 +01002006 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002007 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Hanno Beckera3c2c172019-07-23 16:51:57 +01002008
2009 ssl->handshake->pmslen = NUM_ECC_BYTES;
2010 }
2011 else
2012#endif
Hanno Becker09d23642019-07-22 17:18:18 +01002013#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
2014 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2015 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
2016 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002017 ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002018 ssl->handshake->premaster,
2019 MBEDTLS_PREMASTER_SIZE,
2020 &ssl->handshake->pmslen,
2021 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002022 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2023 if( ret == 0 )
2024 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002025 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002026 if( ret == 0 )
2027 {
2028 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2029 }
2030 else
2031 {
Piotr Nowickie048b912020-06-05 17:59:28 +02002032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret",
2033 MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2034 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002035 }
2036 }
2037 else
Hanno Becker09d23642019-07-22 17:18:18 +01002038 {
2039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2040 return( ret );
2041 }
2042
2043 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2044 }
2045 else
2046#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002047#if defined(MBEDTLS_ECDH_C) && \
2048 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2049 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2050 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2051 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002052 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2053 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2054 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2055 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2056 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2057 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2058 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2059 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2060 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002061 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002062 &ssl->handshake->pmslen,
2063 ssl->handshake->premaster,
2064 MBEDTLS_MPI_MAX_SIZE,
2065 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002066 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2067 if( ret == 0 )
2068 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002069 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002070 if( ret == 0 )
2071 {
2072 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2073 }
2074 else
2075 {
2076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002077 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002078 }
2079 }
2080 else
Hanno Becker09d23642019-07-22 17:18:18 +01002081 {
2082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2083 return( ret );
2084 }
2085
2086 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2087 }
2088 else
2089#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2090 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2091 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2092 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2093#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2094 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2095 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002096 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2097 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2098 if( ret == 0 )
2099 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002100 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002101 if( ret == 0 )
2102 {
2103 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2104 }
2105 else
2106 {
2107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002108 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002109 }
2110 }
2111 else
Hanno Becker09d23642019-07-22 17:18:18 +01002112 {
2113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2114 return( ret );
2115 }
2116 }
2117 else
2118#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2119#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2120 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2121 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2122 {
2123 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2124 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2125 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002126 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002127 if( ret == 0 )
2128 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002129 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002130 if( ret == 0 )
2131 {
2132 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2133 }
2134 else
2135 {
2136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002137 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002138 }
2139 }
2140 else
Hanno Becker09d23642019-07-22 17:18:18 +01002141 {
2142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2143 return( ret );
2144 }
2145 }
2146 else
2147#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2148#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2149 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2150 == MBEDTLS_KEY_EXCHANGE_RSA )
2151 {
2152 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002153 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002154 * ssl_rsa_generate_partial_pms(). Only the
2155 * PMS length needs to be set. */
2156 ssl->handshake->pmslen = 48;
2157 }
2158 else
2159#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2163 }
2164
2165 return( 0 );
2166}
2167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2169int 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 +02002170{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002171 unsigned char *p = ssl->handshake->premaster;
2172 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002173 const unsigned char *psk = ssl->conf->psk;
2174 size_t psk_len = ssl->conf->psk_len;
2175
2176 /* If the psk callback was called, use its result */
2177 if( ssl->handshake->psk != NULL )
2178 {
2179 psk = ssl->handshake->psk;
2180 psk_len = ssl->handshake->psk_len;
2181 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002182
2183 /*
2184 * PMS = struct {
2185 * opaque other_secret<0..2^16-1>;
2186 * opaque psk<0..2^16-1>;
2187 * };
2188 * with "other_secret" depending on the particular key exchange
2189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2191 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002193 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002195
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002196 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002197
2198 if( end < p || (size_t)( end - p ) < psk_len )
2199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2200
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002201 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002202 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002203 }
2204 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2206#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2207 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002208 {
2209 /*
2210 * other_secret already set by the ClientKeyExchange message,
2211 * and is 48 bytes long
2212 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002213 if( end - p < 2 )
2214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2215
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002216 *p++ = 0;
2217 *p++ = 48;
2218 p += 48;
2219 }
2220 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2222#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2223 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002224 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002225 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002226 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002227
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002228 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002229 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002230 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002231 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002232 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002235 return( ret );
2236 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002237 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002238 p += len;
2239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002241 }
2242 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2244#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2245 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002246 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002247 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002248 size_t zlen;
2249
Hanno Becker982da7e2019-09-02 09:47:39 +01002250#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002251 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2252 ssl->handshake->ecdh_privkey,
2253 p + 2 );
2254 if( ret == UECC_FAULT_DETECTED )
2255 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2256 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002257 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002258
2259 zlen = NUM_ECC_BYTES;
2260#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002262 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002263 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002264 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002267 return( ret );
2268 }
2269
Hanno Becker982da7e2019-09-02 09:47:39 +01002270 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2271 MBEDTLS_DEBUG_ECDH_Z );
2272#endif /* MBEDTLS_USE_TINYCRYPT */
2273
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002274 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002275 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002276 }
2277 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002282 }
2283
2284 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002285 if( end - p < 2 )
2286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002287
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002288 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002289
2290 if( end < p || (size_t)( end - p ) < psk_len )
2291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2292
Teppo Järvelin91d79382019-10-02 09:09:31 +03002293 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002294 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002295
2296 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2297
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002298 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2299
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002300 return( 0 );
2301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002305/*
2306 * SSLv3.0 MAC functions
2307 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002308#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002309static void ssl_mac( mbedtls_md_context_t *md_ctx,
2310 const unsigned char *secret,
2311 const unsigned char *buf, size_t len,
2312 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002313 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002314{
2315 unsigned char header[11];
2316 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002317 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2319 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002320
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002321 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002323 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002324 else
Paul Bakker68884e32013-01-07 18:20:04 +01002325 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
Teppo Järvelin91d79382019-10-02 09:09:31 +03002327 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002328 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002329 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002331 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 mbedtls_md_starts( md_ctx );
2333 mbedtls_md_update( md_ctx, secret, md_size );
2334 mbedtls_md_update( md_ctx, padding, padlen );
2335 mbedtls_md_update( md_ctx, header, 11 );
2336 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002337 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002339 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 mbedtls_md_starts( md_ctx );
2341 mbedtls_md_update( md_ctx, secret, md_size );
2342 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002343 mbedtls_md_update( md_ctx, out, md_size );
2344 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002347
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002348/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002349 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002350#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002351 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2352 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2353 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2354/* This function makes sure every byte in the memory region is accessed
2355 * (in ascending addresses order) */
2356static void ssl_read_memory( unsigned char *p, size_t len )
2357{
2358 unsigned char acc = 0;
2359 volatile unsigned char force;
2360
2361 for( ; len != 0; p++, len-- )
2362 acc ^= *p;
2363
2364 force = acc;
2365 (void) force;
2366}
2367#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2368
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002369/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002371 */
Hanno Becker3307b532017-12-27 21:37:21 +00002372
Hanno Beckera5a2b082019-05-15 14:03:01 +01002373#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002374/* This functions transforms a DTLS plaintext fragment and a record content
2375 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002376 *
2377 * struct {
2378 * opaque content[DTLSPlaintext.length];
2379 * ContentType real_type;
2380 * uint8 zeros[length_of_padding];
2381 * } DTLSInnerPlaintext;
2382 *
2383 * Input:
2384 * - `content`: The beginning of the buffer holding the
2385 * plaintext to be wrapped.
2386 * - `*content_size`: The length of the plaintext in Bytes.
2387 * - `max_len`: The number of Bytes available starting from
2388 * `content`. This must be `>= *content_size`.
2389 * - `rec_type`: The desired record content type.
2390 *
2391 * Output:
2392 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2393 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2394 *
2395 * Returns:
2396 * - `0` on success.
2397 * - A negative error code if `max_len` didn't offer enough space
2398 * for the expansion.
2399 */
2400static int ssl_cid_build_inner_plaintext( unsigned char *content,
2401 size_t *content_size,
2402 size_t remaining,
2403 uint8_t rec_type )
2404{
2405 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002406 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2407 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2408 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002409
2410 /* Write real content type */
2411 if( remaining == 0 )
2412 return( -1 );
2413 content[ len ] = rec_type;
2414 len++;
2415 remaining--;
2416
2417 if( remaining < pad )
2418 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002419 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002420 len += pad;
2421 remaining -= pad;
2422
2423 *content_size = len;
2424 return( 0 );
2425}
2426
Hanno Becker7dc25772019-05-20 15:08:01 +01002427/* This function parses a DTLSInnerPlaintext structure.
2428 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002429static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2430 size_t *content_size,
2431 uint8_t *rec_type )
2432{
2433 size_t remaining = *content_size;
2434
2435 /* Determine length of padding by skipping zeroes from the back. */
2436 do
2437 {
2438 if( remaining == 0 )
2439 return( -1 );
2440 remaining--;
2441 } while( content[ remaining ] == 0 );
2442
2443 *content_size = remaining;
2444 *rec_type = content[ remaining ];
2445
2446 return( 0 );
2447}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002448#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002449
Hanno Becker99abf512019-05-20 14:50:53 +01002450/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002451 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002452static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002453 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002454 mbedtls_record *rec )
2455{
Hanno Becker99abf512019-05-20 14:50:53 +01002456 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002457 *
2458 * additional_data = seq_num + TLSCompressed.type +
2459 * TLSCompressed.version + TLSCompressed.length;
2460 *
Hanno Becker99abf512019-05-20 14:50:53 +01002461 * For the CID extension, this is extended as follows
2462 * (quoting draft-ietf-tls-dtls-connection-id-05,
2463 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002464 *
2465 * additional_data = seq_num + DTLSPlaintext.type +
2466 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002467 * cid +
2468 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002469 * length_of_DTLSInnerPlaintext;
2470 */
2471
Teppo Järvelin91d79382019-10-02 09:09:31 +03002472 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002473 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002474 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002475
Hanno Beckera5a2b082019-05-15 14:03:01 +01002476#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002477 if( rec->cid_len != 0 )
2478 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002479 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002480 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002481 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2482 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002483 *add_data_len = 13 + 1 + rec->cid_len;
2484 }
2485 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002486#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002487 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002488 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002489 *add_data_len = 13;
2490 }
Hanno Becker3307b532017-12-27 21:37:21 +00002491}
2492
Hanno Becker611a83b2018-01-03 14:27:32 +00002493int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2494 mbedtls_ssl_transform *transform,
2495 mbedtls_record *rec,
2496 int (*f_rng)(void *, unsigned char *, size_t),
2497 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002499 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002500 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002501 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002502 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002503 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002504 size_t post_avail;
2505
2506 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002507#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002508 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002509 ((void) ssl);
2510#endif
2511
2512 /* The PRNG is used for dynamic IV generation that's used
2513 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2514#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2515 ( defined(MBEDTLS_AES_C) || \
2516 defined(MBEDTLS_ARIA_C) || \
2517 defined(MBEDTLS_CAMELLIA_C) ) && \
2518 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2519 ((void) f_rng);
2520 ((void) p_rng);
2521#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Hanno Becker3307b532017-12-27 21:37:21 +00002525 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002526 {
Hanno Becker3307b532017-12-27 21:37:21 +00002527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2529 }
Hanno Becker505089d2019-05-01 09:45:57 +01002530 if( rec == NULL
2531 || rec->buf == NULL
2532 || rec->buf_len < rec->data_offset
2533 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002534#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002535 || rec->cid_len != 0
2536#endif
2537 )
Hanno Becker3307b532017-12-27 21:37:21 +00002538 {
2539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002541 }
2542
Hanno Becker3307b532017-12-27 21:37:21 +00002543 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002544 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002546 data, rec->data_len );
2547
2548 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2549
2550 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2551 {
2552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2553 (unsigned) rec->data_len,
2554 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2556 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002557
Hanno Beckera5a2b082019-05-15 14:03:01 +01002558#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002559 /*
2560 * Add CID information
2561 */
2562 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002563 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2564 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002565 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002566
2567 if( rec->cid_len != 0 )
2568 {
2569 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002570 * Wrap plaintext into DTLSInnerPlaintext structure.
2571 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002572 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002573 * Note that this changes `rec->data_len`, and hence
2574 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002575 */
2576 if( ssl_cid_build_inner_plaintext( data,
2577 &rec->data_len,
2578 post_avail,
2579 rec->type ) != 0 )
2580 {
2581 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2582 }
2583
2584 rec->type = MBEDTLS_SSL_MSG_CID;
2585 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002586#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002587
Hanno Becker92c930f2019-04-29 17:31:37 +01002588 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2589
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002591 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002593#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 if( mode == MBEDTLS_MODE_STREAM ||
2595 ( mode == MBEDTLS_MODE_CBC
2596#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002597 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002598#endif
2599 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 {
Hanno Becker3307b532017-12-27 21:37:21 +00002601 if( post_avail < transform->maclen )
2602 {
2603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2604 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2605 }
2606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002608 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2609 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002610 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002611 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002612 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2613 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002614 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002615 }
2616 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002617#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2619 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002620 if( mbedtls_ssl_ver_geq(
2621 mbedtls_ssl_transform_get_minor_ver( transform ),
2622 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002623 {
Hanno Becker992b6872017-11-09 18:57:39 +00002624 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2625
Hanno Beckere83efe62019-04-29 13:52:53 +01002626 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002627
Hanno Becker3307b532017-12-27 21:37:21 +00002628 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002629 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002630 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2631 data, rec->data_len );
2632 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2633 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2634
Teppo Järvelin91d79382019-10-02 09:09:31 +03002635 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002636 }
2637 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002638#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2641 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002642 }
2643
Hanno Becker3307b532017-12-27 21:37:21 +00002644 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2645 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002646
Hanno Becker3307b532017-12-27 21:37:21 +00002647 rec->data_len += transform->maclen;
2648 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002649 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002650 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002651#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002653 /*
2654 * Encrypt
2655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2657 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002659 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002660 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002662 "including %d bytes of padding",
2663 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Hanno Becker3307b532017-12-27 21:37:21 +00002665 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2666 transform->iv_enc, transform->ivlen,
2667 data, rec->data_len,
2668 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002671 return( ret );
2672 }
2673
Hanno Becker3307b532017-12-27 21:37:21 +00002674 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 }
Paul Bakker68884e32013-01-07 18:20:04 +01002680 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002682
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002683#if defined(MBEDTLS_GCM_C) || \
2684 defined(MBEDTLS_CCM_C) || \
2685 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002687 mode == MBEDTLS_MODE_CCM ||
2688 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002689 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002690 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002691 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002692 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002693
Hanno Becker3307b532017-12-27 21:37:21 +00002694 /* Check that there's space for both the authentication tag
2695 * and the explicit IV before and after the record content. */
2696 if( post_avail < transform->taglen ||
2697 rec->data_offset < explicit_iv_len )
2698 {
2699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2700 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2701 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002702
Paul Bakker68884e32013-01-07 18:20:04 +01002703 /*
2704 * Generate IV
2705 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002706 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2707 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002708 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002709 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2710 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002711 explicit_iv_len );
2712 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002713 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002714 }
2715 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2716 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002717 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002718 unsigned char i;
2719
Teppo Järvelin91d79382019-10-02 09:09:31 +03002720 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002721
2722 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002723 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002724 }
2725 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002726 {
2727 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002730 }
2731
Hanno Beckere83efe62019-04-29 13:52:53 +01002732 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002733
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002734 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2735 iv, transform->ivlen );
2736 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002737 data - explicit_iv_len, explicit_iv_len );
2738 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002739 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002741 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002742 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002743
Paul Bakker68884e32013-01-07 18:20:04 +01002744 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002745 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002746 */
Hanno Becker3307b532017-12-27 21:37:21 +00002747
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002748 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002749 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002750 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002751 data, rec->data_len, /* source */
2752 data, &rec->data_len, /* destination */
2753 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002756 return( ret );
2757 }
2758
Hanno Becker3307b532017-12-27 21:37:21 +00002759 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2760 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002761
Hanno Becker3307b532017-12-27 21:37:21 +00002762 rec->data_len += transform->taglen + explicit_iv_len;
2763 rec->data_offset -= explicit_iv_len;
2764 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002765 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002766 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002767 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2769#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002770 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002773 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002774 size_t padlen, i;
2775 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002776
Hanno Becker3307b532017-12-27 21:37:21 +00002777 /* Currently we're always using minimal padding
2778 * (up to 255 bytes would be allowed). */
2779 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2780 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 padlen = 0;
2782
Hanno Becker3307b532017-12-27 21:37:21 +00002783 /* Check there's enough space in the buffer for the padding. */
2784 if( post_avail < padlen + 1 )
2785 {
2786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2787 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2788 }
2789
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002791 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002792
Hanno Becker3307b532017-12-27 21:37:21 +00002793 rec->data_len += padlen + 1;
2794 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002797 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002798 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2799 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002800 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002801 if( mbedtls_ssl_ver_geq(
2802 mbedtls_ssl_transform_get_minor_ver( transform ),
2803 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002804 {
Hanno Becker3307b532017-12-27 21:37:21 +00002805 if( f_rng == NULL )
2806 {
2807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2809 }
2810
2811 if( rec->data_offset < transform->ivlen )
2812 {
2813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2814 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2815 }
2816
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817 /*
2818 * Generate IV
2819 */
Hanno Becker3307b532017-12-27 21:37:21 +00002820 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002821 if( ret != 0 )
2822 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002823
Teppo Järvelin91d79382019-10-02 09:09:31 +03002824 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002825 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002826
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002827 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002831 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002832 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002833 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Hanno Becker3307b532017-12-27 21:37:21 +00002835 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2836 transform->iv_enc,
2837 transform->ivlen,
2838 data, rec->data_len,
2839 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002842 return( ret );
2843 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002844
Hanno Becker3307b532017-12-27 21:37:21 +00002845 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002849 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002852 if( mbedtls_ssl_ver_lt(
2853 mbedtls_ssl_transform_get_minor_ver( transform ),
2854 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002855 {
2856 /*
2857 * Save IV in SSL3 and TLS1
2858 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002859 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002860 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 }
Hanno Becker3307b532017-12-27 21:37:21 +00002862 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002863#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002864 {
2865 data -= transform->ivlen;
2866 rec->data_offset -= transform->ivlen;
2867 rec->data_len += transform->ivlen;
2868 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002871 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002872 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002873 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2874
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002875 /*
2876 * MAC(MAC_write_key, seq_num +
2877 * TLSCipherText.type +
2878 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002879 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002880 * IV + // except for TLS 1.0
2881 * ENC(content + padding + padding_length));
2882 */
Hanno Becker3307b532017-12-27 21:37:21 +00002883
2884 if( post_avail < transform->maclen)
2885 {
2886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2887 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2888 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002889
Hanno Beckere83efe62019-04-29 13:52:53 +01002890 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002893 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002894 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002895
Hanno Becker3307b532017-12-27 21:37:21 +00002896 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002897 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002898 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2899 data, rec->data_len );
2900 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2901 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002902
Teppo Järvelin91d79382019-10-02 09:09:31 +03002903 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002904
Hanno Becker3307b532017-12-27 21:37:21 +00002905 rec->data_len += transform->maclen;
2906 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002907 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002908 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002911 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002913 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002917 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002919 /* Make extra sure authentication was performed, exactly once */
2920 if( auth_done != 1 )
2921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2923 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002924 }
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002927
2928 return( 0 );
2929}
2930
Hanno Becker40478be2019-07-12 08:23:59 +01002931int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002932 mbedtls_ssl_transform *transform,
2933 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002934{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002935 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002937 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002938#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002939 size_t padlen = 0, correct = 1;
2940#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002941 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002942 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002943 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002944
Hanno Becker611a83b2018-01-03 14:27:32 +00002945#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002946 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002947 ((void) ssl);
2948#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002951 if( rec == NULL ||
2952 rec->buf == NULL ||
2953 rec->buf_len < rec->data_offset ||
2954 rec->buf_len - rec->data_offset < rec->data_len )
2955 {
2956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002958 }
2959
Hanno Becker4c6876b2017-12-27 21:28:58 +00002960 data = rec->buf + rec->data_offset;
2961 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Hanno Beckera5a2b082019-05-15 14:03:01 +01002963#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002964 /*
2965 * Match record's CID with incoming CID.
2966 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002967 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002968 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002969 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002970 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002971 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002972#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2975 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002976 {
2977 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002978 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2979 transform->iv_dec,
2980 transform->ivlen,
2981 data, rec->data_len,
2982 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002985 return( ret );
2986 }
2987
Hanno Becker4c6876b2017-12-27 21:28:58 +00002988 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002992 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 }
Paul Bakker68884e32013-01-07 18:20:04 +01002994 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002996#if defined(MBEDTLS_GCM_C) || \
2997 defined(MBEDTLS_CCM_C) || \
2998 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003000 mode == MBEDTLS_MODE_CCM ||
3001 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003002 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003003 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003004 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003005
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003006 /*
3007 * Prepare IV from explicit and implicit data.
3008 */
3009
3010 /* Check that there's enough space for the explicit IV
3011 * (at the beginning of the record) and the MAC (at the
3012 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003013 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003016 "+ taglen (%d)", rec->data_len,
3017 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003019 }
Paul Bakker68884e32013-01-07 18:20:04 +01003020
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003021#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003022 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3023 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003024 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003025
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003026 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003027 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003028 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003029 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003030 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003031 else
3032#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3033#if defined(MBEDTLS_CHACHAPOLY_C)
3034 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003035 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003036 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003037 unsigned char i;
3038
Teppo Järvelin91d79382019-10-02 09:09:31 +03003039 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003040
3041 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003042 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003043 }
3044 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003045#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003046 {
3047 /* Reminder if we ever add an AEAD mode with a different size */
3048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3049 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3050 }
3051
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003052 /* Group changes to data, data_len, and add_data, because
3053 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003054 data += explicit_iv_len;
3055 rec->data_offset += explicit_iv_len;
3056 rec->data_len -= explicit_iv_len + transform->taglen;
3057
Hanno Beckere83efe62019-04-29 13:52:53 +01003058 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003059 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003060 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003061
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003062 /* Because of the check above, we know that there are
3063 * explicit_iv_len Bytes preceeding data, and taglen
3064 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003065 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003066 * mbedtls_cipher_auth_decrypt() below. */
3067
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003068 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003069 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003070 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003071
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003072 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003073 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003074 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003075 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3076 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003077 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003078 data, rec->data_len,
3079 data, &olen,
3080 data + rec->data_len,
3081 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3086 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003087
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003088 return( ret );
3089 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003090 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003091
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003092 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003093 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3096 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003097 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003098 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3101#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003102 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003105 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003106
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 /*
Paul Bakker45829992013-01-03 14:52:21 +01003108 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003111 if( mbedtls_ssl_ver_geq(
3112 mbedtls_ssl_transform_get_minor_ver( transform ),
3113 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003114 {
3115 /* The ciphertext is prefixed with the CBC IV. */
3116 minlen += transform->ivlen;
3117 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003118#endif
Paul Bakker45829992013-01-03 14:52:21 +01003119
Hanno Becker4c6876b2017-12-27 21:28:58 +00003120 /* Size considerations:
3121 *
3122 * - The CBC cipher text must not be empty and hence
3123 * at least of size transform->ivlen.
3124 *
3125 * Together with the potential IV-prefix, this explains
3126 * the first of the two checks below.
3127 *
3128 * - The record must contain a MAC, either in plain or
3129 * encrypted, depending on whether Encrypt-then-MAC
3130 * is used or not.
3131 * - If it is, the message contains the IV-prefix,
3132 * the CBC ciphertext, and the MAC.
3133 * - If it is not, the padded plaintext, and hence
3134 * the CBC ciphertext, has at least length maclen + 1
3135 * because there is at least the padding length byte.
3136 *
3137 * As the CBC ciphertext is not empty, both cases give the
3138 * lower bound minlen + maclen + 1 on the record size, which
3139 * we test for in the second check below.
3140 */
3141 if( rec->data_len < minlen + transform->ivlen ||
3142 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003145 "+ 1 ) ( + expl IV )", rec->data_len,
3146 transform->ivlen,
3147 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003149 }
3150
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003151 /*
3152 * Authenticate before decrypt if enabled
3153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003155 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003156 {
Hanno Becker992b6872017-11-09 18:57:39 +00003157 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003160
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003161 /* Update data_len in tandem with add_data.
3162 *
3163 * The subtraction is safe because of the previous check
3164 * data_len >= minlen + maclen + 1.
3165 *
3166 * Afterwards, we know that data + data_len is followed by at
3167 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003168 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003169 *
3170 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003171 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003172 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003173
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003174 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003175 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3176 add_data_len );
3177 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3178 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003179 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3180 data, rec->data_len );
3181 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3182 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003183
Hanno Becker4c6876b2017-12-27 21:28:58 +00003184 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3185 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003186 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003187 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003188
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003189 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003190 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003191 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003195 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003196 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003197 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003199
3200 /*
3201 * Check length sanity
3202 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003203
3204 /* We know from above that data_len > minlen >= 0,
3205 * so the following check in particular implies that
3206 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003207 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003210 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003212 }
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003215 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003216 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003217 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003218 if( mbedtls_ssl_ver_geq(
3219 mbedtls_ssl_transform_get_minor_ver( transform ),
3220 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003221 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003222 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003223 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003224
Hanno Becker4c6876b2017-12-27 21:28:58 +00003225 data += transform->ivlen;
3226 rec->data_offset += transform->ivlen;
3227 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003228 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003230
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003231 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3232
Hanno Becker4c6876b2017-12-27 21:28:58 +00003233 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3234 transform->iv_dec, transform->ivlen,
3235 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003238 return( ret );
3239 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003240
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003241 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003242 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003246 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003249 if( mbedtls_ssl_ver_lt(
3250 mbedtls_ssl_transform_get_minor_ver( transform ),
3251 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003252 {
3253 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003254 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3255 * records is equivalent to CBC decryption of the concatenation
3256 * of the records; in other words, IVs are maintained across
3257 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003258 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003259 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003260 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003263
Hanno Becker4c6876b2017-12-27 21:28:58 +00003264 /* Safe since data_len >= minlen + maclen + 1, so after having
3265 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003266 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3267 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003268 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003269
Hanno Becker4c6876b2017-12-27 21:28:58 +00003270 if( auth_done == 1 )
3271 {
3272 correct *= ( rec->data_len >= padlen + 1 );
3273 padlen *= ( rec->data_len >= padlen + 1 );
3274 }
3275 else
Paul Bakker45829992013-01-03 14:52:21 +01003276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003278 if( rec->data_len < transform->maclen + padlen + 1 )
3279 {
3280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3281 rec->data_len,
3282 transform->maclen,
3283 padlen + 1 ) );
3284 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003285#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003286
3287 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3288 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Hanno Becker4c6876b2017-12-27 21:28:58 +00003291 padlen++;
3292
3293 /* Regardless of the validity of the padding,
3294 * we have data_len >= padlen here. */
3295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003297 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3298 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003300 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302#if defined(MBEDTLS_SSL_DEBUG_ALL)
3303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003304 "should be no more than %d",
3305 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003306#endif
Paul Bakker45829992013-01-03 14:52:21 +01003307 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 }
3309 }
3310 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3312#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3313 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003314 if( mbedtls_ssl_ver_gt(
3315 mbedtls_ssl_transform_get_minor_ver( transform ),
3316 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003318 /* The padding check involves a series of up to 256
3319 * consecutive memory reads at the end of the record
3320 * plaintext buffer. In order to hide the length and
3321 * validity of the padding, always perform exactly
3322 * `min(256,plaintext_len)` reads (but take into account
3323 * only the last `padlen` bytes for the padding check). */
3324 size_t pad_count = 0;
3325 size_t real_count = 0;
3326 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003327
Hanno Becker4c6876b2017-12-27 21:28:58 +00003328 /* Index of first padding byte; it has been ensured above
3329 * that the subtraction is safe. */
3330 size_t const padding_idx = rec->data_len - padlen;
3331 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3332 size_t const start_idx = rec->data_len - num_checks;
3333 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003334
Hanno Becker4c6876b2017-12-27 21:28:58 +00003335 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003336 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003337 real_count |= ( idx >= padding_idx );
3338 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003339 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003340 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003343 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003345#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003346 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003348 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3350 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3353 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003354 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003355
Hanno Becker4c6876b2017-12-27 21:28:58 +00003356 /* If the padding was found to be invalid, padlen == 0
3357 * and the subtraction is safe. If the padding was found valid,
3358 * padlen hasn't been changed and the previous assertion
3359 * data_len >= padlen still holds. */
3360 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003362 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003364 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3367 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003370#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003372 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003373#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003374
3375 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003376 * Authenticate if not done yet.
3377 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003379#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003380 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003381 {
Hanno Becker992b6872017-11-09 18:57:39 +00003382 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003383
Hanno Becker4c6876b2017-12-27 21:28:58 +00003384 /* If the initial value of padlen was such that
3385 * data_len < maclen + padlen + 1, then padlen
3386 * got reset to 1, and the initial check
3387 * data_len >= minlen + maclen + 1
3388 * guarantees that at this point we still
3389 * have at least data_len >= maclen.
3390 *
3391 * If the initial value of padlen was such that
3392 * data_len >= maclen + padlen + 1, then we have
3393 * subtracted either padlen + 1 (if the padding was correct)
3394 * or 0 (if the padding was incorrect) since then,
3395 * hence data_len >= maclen in any case.
3396 */
3397 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003398 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003401 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3402 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003403 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003404 ssl_mac( &transform->md_ctx_dec,
3405 transform->mac_dec,
3406 data, rec->data_len,
3407 rec->ctr, rec->type,
3408 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003409 }
3410 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3412#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3413 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003414 if( mbedtls_ssl_ver_gt(
3415 mbedtls_ssl_transform_get_minor_ver( transform ),
3416 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003417 {
3418 /*
3419 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003420 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003421 *
3422 * Known timing attacks:
3423 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3424 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003425 * To compensate for different timings for the MAC calculation
3426 * depending on how much padding was removed (which is determined
3427 * by padlen), process extra_run more blocks through the hash
3428 * function.
3429 *
3430 * The formula in the paper is
3431 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3432 * where L1 is the size of the header plus the decrypted message
3433 * plus CBC padding and L2 is the size of the header plus the
3434 * decrypted message. This is for an underlying hash function
3435 * with 64-byte blocks.
3436 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3437 * correctly. We round down instead of up, so -56 is the correct
3438 * value for our calculations instead of -55.
3439 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003440 * Repeat the formula rather than defining a block_size variable.
3441 * This avoids requiring division by a variable at runtime
3442 * (which would be marginally less efficient and would require
3443 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003444 */
3445 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003446 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003447
3448 /*
3449 * The next two sizes are the minimum and maximum values of
3450 * in_msglen over all padlen values.
3451 *
3452 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003453 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003454 *
3455 * Note that max_len + maclen is never more than the buffer
3456 * length, as we previously did in_msglen -= maclen too.
3457 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003458 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003459 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3460
Hanno Becker4c6876b2017-12-27 21:28:58 +00003461 memset( tmp, 0, sizeof( tmp ) );
3462
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003463 switch( mbedtls_md_get_type(
3464 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003465 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003466#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3467 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003468 case MBEDTLS_MD_MD5:
3469 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003470 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003471 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003472 extra_run =
3473 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3474 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003475 break;
3476#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003477#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003478 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003479 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003480 extra_run =
3481 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3482 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003483 break;
3484#endif
3485 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3488 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003489
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003490 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003491
Hanno Beckere83efe62019-04-29 13:52:53 +01003492 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3493 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003494 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3495 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003496 /* Make sure we access everything even when padlen > 0. This
3497 * makes the synchronisation requirements for just-in-time
3498 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003499 ssl_read_memory( data + rec->data_len, padlen );
3500 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003501
3502 /* Call mbedtls_md_process at least once due to cache attacks
3503 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003504 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003505 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003506
Hanno Becker4c6876b2017-12-27 21:28:58 +00003507 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003508
3509 /* Make sure we access all the memory that could contain the MAC,
3510 * before we check it in the next code block. This makes the
3511 * synchronisation requirements for just-in-time Prime+Probe
3512 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003513 ssl_read_memory( data + min_len,
3514 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003515 }
3516 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003517#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3518 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3521 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003524#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003525 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3526 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003527#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003529 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003530 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532#if defined(MBEDTLS_SSL_DEBUG_ALL)
3533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003534#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003535 correct = 0;
3536 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003537 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003538 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003539
3540 /*
3541 * Finally check the correct flag
3542 */
3543 if( correct == 0 )
3544 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003545#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003546
3547 /* Make extra sure authentication was performed, exactly once */
3548 if( auth_done != 1 )
3549 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003552 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
Hanno Beckera5a2b082019-05-15 14:03:01 +01003554#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003555 if( rec->cid_len != 0 )
3556 {
3557 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3558 &rec->type );
3559 if( ret != 0 )
3560 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3561 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003562#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565
3566 return( 0 );
3567}
3568
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003569#undef MAC_NONE
3570#undef MAC_PLAINTEXT
3571#undef MAC_CIPHERTEXT
3572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003574/*
3575 * Compression/decompression functions
3576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003578{
3579 int ret;
3580 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003581 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003582 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003583 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003586
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003587 if( len_pre == 0 )
3588 return( 0 );
3589
Teppo Järvelin91d79382019-10-02 09:09:31 +03003590 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003593 ssl->out_msglen ) );
3594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003596 ssl->out_msg, ssl->out_msglen );
3597
Paul Bakker48916f92012-09-16 19:57:18 +00003598 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3599 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3600 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003601 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003602
Paul Bakker48916f92012-09-16 19:57:18 +00003603 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003604 if( ret != Z_OK )
3605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3607 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003608 }
3609
Angus Grattond8213d02016-05-25 20:56:48 +10003610 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003611 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003614 ssl->out_msglen ) );
3615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003616 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617 ssl->out_msg, ssl->out_msglen );
3618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003620
3621 return( 0 );
3622}
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003625{
3626 int ret;
3627 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003628 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003629 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003630 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003633
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003634 if( len_pre == 0 )
3635 return( 0 );
3636
Teppo Järvelin91d79382019-10-02 09:09:31 +03003637 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003640 ssl->in_msglen ) );
3641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003643 ssl->in_msg, ssl->in_msglen );
3644
Paul Bakker48916f92012-09-16 19:57:18 +00003645 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3646 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3647 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003648 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003649 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003650
Paul Bakker48916f92012-09-16 19:57:18 +00003651 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003652 if( ret != Z_OK )
3653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3655 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003656 }
3657
Angus Grattond8213d02016-05-25 20:56:48 +10003658 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003659 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003662 ssl->in_msglen ) );
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003665 ssl->in_msg, ssl->in_msglen );
3666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003668
3669 return( 0 );
3670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3674static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676#if defined(MBEDTLS_SSL_PROTO_DTLS)
3677static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003678{
3679 /* If renegotiation is not enforced, retransmit until we would reach max
3680 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003681 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003682 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003683 uint32_t ratio =
3684 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3685 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003686 unsigned char doublings = 1;
3687
3688 while( ratio != 0 )
3689 {
3690 ++doublings;
3691 ratio >>= 1;
3692 }
3693
3694 if( ++ssl->renego_records_seen > doublings )
3695 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003697 return( 0 );
3698 }
3699 }
3700
3701 return( ssl_write_hello_request( ssl ) );
3702}
3703#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003704#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003705
Paul Bakker5121ce52009-01-03 21:22:43 +00003706/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003707 * Fill the input message buffer by appending data to it.
3708 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003709 *
3710 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3711 * available (from this read and/or a previous one). Otherwise, an error code
3712 * is returned (possibly EOF or WANT_READ).
3713 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003714 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3715 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3716 * since we always read a whole datagram at once.
3717 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003718 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003719 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003722{
Paul Bakker23986e52011-04-24 08:57:21 +00003723 int ret;
3724 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003727
Hanno Beckera58a8962019-06-13 16:11:15 +01003728 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3729 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003732 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003734 }
3735
Angus Grattond8213d02016-05-25 20:56:48 +10003736 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003740 }
3741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003743 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003744 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003745 uint32_t timeout;
3746
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003747 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003748 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3749 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003750 {
3751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3752 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3754 }
3755
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003756 /*
3757 * The point is, we need to always read a full datagram at once, so we
3758 * sometimes read more then requested, and handle the additional data.
3759 * It could be the rest of the current record (while fetching the
3760 * header) and/or some other records in the same datagram.
3761 */
3762
3763 /*
3764 * Move to the next record in the already read datagram if applicable
3765 */
3766 if( ssl->next_record_offset != 0 )
3767 {
3768 if( ssl->in_left < ssl->next_record_offset )
3769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3771 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003772 }
3773
3774 ssl->in_left -= ssl->next_record_offset;
3775
3776 if( ssl->in_left != 0 )
3777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003779 ssl->next_record_offset ) );
3780 memmove( ssl->in_hdr,
3781 ssl->in_hdr + ssl->next_record_offset,
3782 ssl->in_left );
3783 }
3784
3785 ssl->next_record_offset = 0;
3786 }
3787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003789 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003790
3791 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003792 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003793 */
3794 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003797 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003798 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003799
3800 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003801 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003802 * are not at the beginning of a new record, the caller did something
3803 * wrong.
3804 */
3805 if( ssl->in_left != 0 )
3806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003809 }
3810
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003811 /*
3812 * Don't even try to read if time's out already.
3813 * This avoids by-passing the timer when repeatedly receiving messages
3814 * that will end up being dropped.
3815 */
3816 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003817 {
3818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003819 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003820 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003821 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003822 {
Angus Grattond8213d02016-05-25 20:56:48 +10003823 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003826 timeout = ssl->handshake->retransmit_timeout;
3827 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003828 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003831
Hanno Beckera58a8962019-06-13 16:11:15 +01003832 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3833 {
3834 ret = mbedtls_ssl_get_recv_timeout( ssl )
3835 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3836 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003837 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003838 {
3839 ret = mbedtls_ssl_get_recv( ssl )
3840 ( ssl->p_bio, ssl->in_hdr, len );
3841 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003844
3845 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003847 }
3848
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003849 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003852 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003855 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003856 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003859 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003860 }
3861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003865 return( ret );
3866 }
3867
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003868 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003869 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003871 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3872 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003874 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003875 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003878 return( ret );
3879 }
3880
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003881 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003882 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003883#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003884 }
3885
Paul Bakker5121ce52009-01-03 21:22:43 +00003886 if( ret < 0 )
3887 return( ret );
3888
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003889 ssl->in_left = ret;
3890 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003891 MBEDTLS_SSL_TRANSPORT_ELSE
3892#endif /* MBEDTLS_SSL_PROTO_DTLS */
3893#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003896 ssl->in_left, nb_want ) );
3897
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003898 while( ssl->in_left < nb_want )
3899 {
3900 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003901
3902 if( ssl_check_timer( ssl ) != 0 )
3903 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3904 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003905 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003906 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003907 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003908 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3909 ssl->in_hdr + ssl->in_left, len,
3910 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003911 }
3912 else
3913 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003914 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003915 ssl->in_hdr + ssl->in_left, len );
3916 }
3917 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003920 ssl->in_left, nb_want ) );
3921 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003922
3923 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003925
3926 if( ret < 0 )
3927 return( ret );
3928
mohammad160352aecb92018-03-28 23:41:40 -07003929 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003930 {
Darryl Green11999bb2018-03-13 15:22:58 +00003931 MBEDTLS_SSL_DEBUG_MSG( 1,
3932 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003933 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3935 }
3936
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003937 ssl->in_left += ret;
3938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003939 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003940#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003943
3944 return( 0 );
3945}
3946
3947/*
3948 * Flush any data not yet written
3949 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003951{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003952 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003953 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
Hanno Beckera58a8962019-06-13 16:11:15 +01003957 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003960 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003962 }
3963
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003964 /* Avoid incrementing counter if data is flushed */
3965 if( ssl->out_left == 0 )
3966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003968 return( 0 );
3969 }
3970
Paul Bakker5121ce52009-01-03 21:22:43 +00003971 while( ssl->out_left > 0 )
3972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003974 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003975
Hanno Becker2b1e3542018-08-06 11:19:13 +01003976 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003977 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003980
3981 if( ret <= 0 )
3982 return( ret );
3983
mohammad160352aecb92018-03-28 23:41:40 -07003984 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003985 {
Darryl Green11999bb2018-03-13 15:22:58 +00003986 MBEDTLS_SSL_DEBUG_MSG( 1,
3987 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003988 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3990 }
3991
Paul Bakker5121ce52009-01-03 21:22:43 +00003992 ssl->out_left -= ret;
3993 }
3994
Hanno Becker2b1e3542018-08-06 11:19:13 +01003995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003996 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003997 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003998 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003999 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004000 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01004001#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004002#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01004003 {
4004 ssl->out_hdr = ssl->out_buf + 8;
4005 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004006#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004007 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
4011 return( 0 );
4012}
4013
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004014/*
4015 * Functions to handle the DTLS retransmission state machine
4016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018/*
4019 * Append current handshake message to current outgoing flight
4020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4025 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4026 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004027
4028 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004029 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004030 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004032 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004033 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004034 }
4035
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004036 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004037 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004040 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004041 }
4042
4043 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004044 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004045 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004046 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004047 msg->next = NULL;
4048
4049 /* Append to the current flight */
4050 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004051 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004052 else
4053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004055 while( cur->next != NULL )
4056 cur = cur->next;
4057 cur->next = msg;
4058 }
4059
Hanno Becker3b235902018-08-06 09:54:53 +01004060 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004061 return( 0 );
4062}
4063
4064/*
4065 * Free the current flight of handshake messages
4066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004069 mbedtls_ssl_flight_item *cur = flight;
4070 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004071
4072 while( cur != NULL )
4073 {
4074 next = cur->next;
4075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004076 mbedtls_free( cur->p );
4077 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004078
4079 cur = next;
4080 }
4081}
4082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004083#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4084static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004085#endif
4086
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004087/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004088 * Swap transform_out and out_ctr with the alternative ones
4089 */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004090static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004092 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004093 unsigned char tmp_out_ctr[8];
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004094#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4095 int ret;
4096#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004097
4098 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004101 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004102 }
4103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004105
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004106 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004107 tmp_transform = ssl->transform_out;
4108 ssl->transform_out = ssl->handshake->alt_transform_out;
4109 ssl->handshake->alt_transform_out = tmp_transform;
4110
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004111 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004112 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4113 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4114 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004115
4116 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004117 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4120 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4125 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004126 }
4127 }
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004128#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
4129
4130 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004131}
4132
4133/*
4134 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004135 */
4136int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4137{
4138 int ret = 0;
4139
4140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4141
4142 ret = mbedtls_ssl_flight_transmit( ssl );
4143
4144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4145
4146 return( ret );
4147}
4148
4149/*
4150 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004151 *
4152 * Need to remember the current message in case flush_output returns
4153 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004154 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004155 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004156int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004157{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004158 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004162 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004164
4165 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004166 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004167 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4168 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004171 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004172
4173 while( ssl->handshake->cur_msg != NULL )
4174 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004175 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004176 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004177
Hanno Beckere1dcb032018-08-17 16:47:58 +01004178 int const is_finished =
4179 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4180 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4181
Hanno Becker04da1892018-08-14 13:22:10 +01004182 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4183 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4184
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004185 /* Swap epochs before sending Finished: we can't do it after
4186 * sending ChangeCipherSpec, in case write returns WANT_READ.
4187 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004188 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004189 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004191 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4192 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004193 }
4194
Hanno Becker67bc7c32018-08-06 11:33:50 +01004195 ret = ssl_get_remaining_payload_in_datagram( ssl );
4196 if( ret < 0 )
4197 return( ret );
4198 max_frag_len = (size_t) ret;
4199
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004200 /* CCS is copied as is, while HS messages may need fragmentation */
4201 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4202 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004203 if( max_frag_len == 0 )
4204 {
4205 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4206 return( ret );
4207
4208 continue;
4209 }
4210
Teppo Järvelin91d79382019-10-02 09:09:31 +03004211 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004212 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004213 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004214
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004215 /* Update position inside current message */
4216 ssl->handshake->cur_msg_p += cur->len;
4217 }
4218 else
4219 {
4220 const unsigned char * const p = ssl->handshake->cur_msg_p;
4221 const size_t hs_len = cur->len - 12;
4222 const size_t frag_off = p - ( cur->p + 12 );
4223 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004224 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004225
Hanno Beckere1dcb032018-08-17 16:47:58 +01004226 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004227 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004228 if( is_finished )
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004229 {
4230 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4231 return( ret );
4232 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004233
Hanno Becker67bc7c32018-08-06 11:33:50 +01004234 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4235 return( ret );
4236
4237 continue;
4238 }
4239 max_hs_frag_len = max_frag_len - 12;
4240
4241 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4242 max_hs_frag_len : rem_len;
4243
4244 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004245 {
4246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004247 (unsigned) cur_hs_frag_len,
4248 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004249 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004250
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004251 /* Messages are stored with handshake headers as if not fragmented,
4252 * copy beginning of headers then fill fragmentation fields.
4253 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004254 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004255
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004256 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4257 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4258 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004259
4260 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4261
Hanno Becker3f7b9732018-08-28 09:53:25 +01004262 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004263 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004264 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004265 ssl->out_msgtype = cur->type;
4266
4267 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004268 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004269 }
4270
4271 /* If done with the current message move to the next one if any */
4272 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4273 {
4274 if( cur->next != NULL )
4275 {
4276 ssl->handshake->cur_msg = cur->next;
4277 ssl->handshake->cur_msg_p = cur->next->p + 12;
4278 }
4279 else
4280 {
4281 ssl->handshake->cur_msg = NULL;
4282 ssl->handshake->cur_msg_p = NULL;
4283 }
4284 }
4285
4286 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004287 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004290 return( ret );
4291 }
4292 }
4293
Hanno Becker67bc7c32018-08-06 11:33:50 +01004294 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4295 return( ret );
4296
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004297 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4299 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004300 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004303 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4304 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004305
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004307
4308 return( 0 );
4309}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004310
4311/*
4312 * To be called when the last message of an incoming flight is received.
4313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004315{
4316 /* We won't need to resend that one any more */
4317 ssl_flight_free( ssl->handshake->flight );
4318 ssl->handshake->flight = NULL;
4319 ssl->handshake->cur_msg = NULL;
4320
4321 /* The next incoming flight will start with this msg_seq */
4322 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4323
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004324 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004325 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004326
Hanno Becker0271f962018-08-16 13:23:47 +01004327 /* Clear future message buffering structure. */
4328 ssl_buffering_free( ssl );
4329
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004330 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004331 ssl_set_timer( ssl, 0 );
4332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4334 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004336 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004337 }
4338 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004340}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004341
4342/*
4343 * To be called when the last message of an outgoing flight is send.
4344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004346{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004347 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004348 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4351 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004354 }
4355 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004359
Paul Bakker5121ce52009-01-03 21:22:43 +00004360/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004361 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004362 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004363
4364/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004365 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004366 *
4367 * - fill in handshake headers
4368 * - update handshake checksum
4369 * - DTLS: save message for resending
4370 * - then pass to the record layer
4371 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004372 * DTLS: except for HelloRequest, messages are only queued, and will only be
4373 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004374 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004375 * Inputs:
4376 * - ssl->out_msglen: 4 + actual handshake message len
4377 * (4 is the size of handshake headers for TLS)
4378 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4379 * - ssl->out_msg + 4: the handshake message body
4380 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004381 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004382 * - ssl->out_msglen: the length of the record contents
4383 * (including handshake headers but excluding record headers)
4384 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004385 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004386int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004387{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004388 int ret;
4389 const size_t hs_len = ssl->out_msglen - 4;
4390 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004391
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4393
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004394 /*
4395 * Sanity checks
4396 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004397 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004398 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4399 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004400 /* In SSLv3, the client might send a NoCertificate alert. */
4401#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004402 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004403 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004404 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4405 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004406#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4407 {
4408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4409 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4410 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004412
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004413 /* Whenever we send anything different from a
4414 * HelloRequest we should be in a handshake - double check. */
4415 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4416 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004417 ssl->handshake == NULL )
4418 {
4419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004424 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004425 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004426 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004427 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4429 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004430 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004431#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004432
Hanno Beckerb50a2532018-08-06 11:52:54 +01004433 /* Double-check that we did not exceed the bounds
4434 * of the outgoing record buffer.
4435 * This should never fail as the various message
4436 * writing functions must obey the bounds of the
4437 * outgoing record buffer, but better be safe.
4438 *
4439 * Note: We deliberately do not check for the MTU or MFL here.
4440 */
4441 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4442 {
4443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4444 "size %u, maximum %u",
4445 (unsigned) ssl->out_msglen,
4446 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4447 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4448 }
4449
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004450 /*
4451 * Fill handshake headers
4452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004455 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004456
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004457 /*
4458 * DTLS has additional fields in the Handshake layer,
4459 * between the length field and the actual payload:
4460 * uint16 message_seq;
4461 * uint24 fragment_offset;
4462 * uint24 fragment_length;
4463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004465 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004466 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004467 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004468 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004469 {
4470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4471 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004472 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004473 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4475 }
4476
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004477 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004478 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004479
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004480 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004481 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004482 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004483 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4484 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004485 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004486 }
4487 else
4488 {
4489 ssl->out_msg[4] = 0;
4490 ssl->out_msg[5] = 0;
4491 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004492
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004493 /* Handshake hashes are computed without fragmentation,
4494 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004495 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004496 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004497 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004499
Hanno Becker0207e532018-08-28 10:28:28 +01004500 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004501 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004502 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004503 }
4504
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004505 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004507 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004508 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4509 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004510 {
4511 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004514 return( ret );
4515 }
4516 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004517 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004518#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004519 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004520 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004521 {
4522 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4523 return( ret );
4524 }
4525 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004526
4527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4528
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004529 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004530}
4531
4532/*
4533 * Record layer functions
4534 */
4535
4536/*
4537 * Write current record.
4538 *
4539 * Uses:
4540 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4541 * - ssl->out_msglen: length of the record content (excl headers)
4542 * - ssl->out_msg: record content
4543 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004544int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004545{
4546 int ret, done = 0;
4547 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004548 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004549
4550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004553 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004555 {
4556 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004559 return( ret );
4560 }
4561
4562 len = ssl->out_msglen;
4563 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4567 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 ret = mbedtls_ssl_hw_record_write( ssl );
4572 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4575 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004576 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004577
4578 if( ret == 0 )
4579 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004580 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004582 if( !done )
4583 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004584 unsigned i;
4585 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004586 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004587
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004588 /* Skip writing the record content type to after the encryption,
4589 * as it may change when using the CID extension. */
4590
Hanno Becker2881d802019-05-22 14:44:53 +01004591 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4592 mbedtls_ssl_get_minor_ver( ssl ),
4593 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004594
Teppo Järvelin91d79382019-10-02 09:09:31 +03004595 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004596 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004597
Paul Bakker48916f92012-09-16 19:57:18 +00004598 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004599 {
Hanno Becker3307b532017-12-27 21:37:21 +00004600 mbedtls_record rec;
4601
4602 rec.buf = ssl->out_iv;
4603 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4604 ( ssl->out_iv - ssl->out_buf );
4605 rec.data_len = ssl->out_msglen;
4606 rec.data_offset = ssl->out_msg - rec.buf;
4607
Teppo Järvelin91d79382019-10-02 09:09:31 +03004608 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004609 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4610 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004611 ssl->conf->transport, rec.ver );
4612 rec.type = ssl->out_msgtype;
4613
Hanno Beckera5a2b082019-05-15 14:03:01 +01004614#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004615 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004616 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004617#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004618
Hanno Becker611a83b2018-01-03 14:27:32 +00004619 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004620 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004621 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004623 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004624 return( ret );
4625 }
4626
Hanno Becker3307b532017-12-27 21:37:21 +00004627 if( rec.data_offset != 0 )
4628 {
4629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4631 }
4632
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004633 /* Update the record content type and CID. */
4634 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004635#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004636 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4637 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004638#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004639 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004640 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004641 encrypted_fi = 1;
4642 }
4643
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004644 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004645 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4646 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004647 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004648 }
4649
Hanno Becker43395762019-05-03 14:46:38 +01004650 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004651
4652#if defined(MBEDTLS_SSL_PROTO_DTLS)
4653 /* In case of DTLS, double-check that we don't exceed
4654 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004655 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004656 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004657 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004658 if( ret < 0 )
4659 return( ret );
4660
4661 if( protected_record_size > (size_t) ret )
4662 {
4663 /* Should never happen */
4664 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4665 }
4666 }
4667#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004668
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004669 /* Now write the potentially updated record content type. */
4670 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004673 "version = [%d:%d], msglen = %d",
4674 ssl->out_hdr[0], ssl->out_hdr[1],
4675 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004678 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004679
4680 ssl->out_left += protected_record_size;
4681 ssl->out_hdr += protected_record_size;
4682 ssl_update_out_pointers( ssl, ssl->transform_out );
4683
Hanno Becker04484622018-08-06 09:49:38 +01004684 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4685 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4686 break;
4687
4688 /* The loop goes to its end iff the counter is wrapping */
4689 if( i == ssl_ep_len( ssl ) )
4690 {
4691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4692 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 }
4695
Hanno Becker67bc7c32018-08-06 11:33:50 +01004696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004697 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004698 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004699 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004700 size_t remaining;
4701 ret = ssl_get_remaining_payload_in_datagram( ssl );
4702 if( ret < 0 )
4703 {
4704 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4705 ret );
4706 return( ret );
4707 }
4708
4709 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004710 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004711 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004712 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004713 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004714 else
4715 {
Hanno Becker513815a2018-08-20 11:56:09 +01004716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004717 }
4718 }
4719#endif /* MBEDTLS_SSL_PROTO_DTLS */
4720
4721 if( ( flush == SSL_FORCE_FLUSH ) &&
4722 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004725 return( ret );
4726 }
4727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004729
4730 return( 0 );
4731}
4732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004734
4735static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4736{
4737 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004738 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4739 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004740 {
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04004741 return( PROPER_HS_FRAGMENT );
Hanno Beckere25e3b72018-08-16 09:30:53 +01004742 }
4743 return( 0 );
4744}
Hanno Becker44650b72018-08-16 12:51:11 +01004745
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004746static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004747{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004748 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004749}
4750
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004751static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004752{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004753 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004754}
4755
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004756static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004757{
4758 uint32_t msg_len, frag_off, frag_len;
4759
4760 msg_len = ssl_get_hs_total_len( ssl );
4761 frag_off = ssl_get_hs_frag_off( ssl );
4762 frag_len = ssl_get_hs_frag_len( ssl );
4763
4764 if( frag_off > msg_len )
4765 return( -1 );
4766
4767 if( frag_len > msg_len - frag_off )
4768 return( -1 );
4769
4770 if( frag_len + 12 > ssl->in_msglen )
4771 return( -1 );
4772
4773 return( 0 );
4774}
4775
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004776/*
4777 * Mark bits in bitmask (used for DTLS HS reassembly)
4778 */
4779static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4780{
4781 unsigned int start_bits, end_bits;
4782
4783 start_bits = 8 - ( offset % 8 );
4784 if( start_bits != 8 )
4785 {
4786 size_t first_byte_idx = offset / 8;
4787
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004788 /* Special case */
4789 if( len <= start_bits )
4790 {
4791 for( ; len != 0; len-- )
4792 mask[first_byte_idx] |= 1 << ( start_bits - len );
4793
4794 /* Avoid potential issues with offset or len becoming invalid */
4795 return;
4796 }
4797
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004798 offset += start_bits; /* Now offset % 8 == 0 */
4799 len -= start_bits;
4800
4801 for( ; start_bits != 0; start_bits-- )
4802 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4803 }
4804
4805 end_bits = len % 8;
4806 if( end_bits != 0 )
4807 {
4808 size_t last_byte_idx = ( offset + len ) / 8;
4809
4810 len -= end_bits; /* Now len % 8 == 0 */
4811
4812 for( ; end_bits != 0; end_bits-- )
4813 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4814 }
4815
4816 memset( mask + offset / 8, 0xFF, len / 8 );
4817}
4818
4819/*
4820 * Check that bitmask is full
4821 */
4822static int ssl_bitmask_check( unsigned char *mask, size_t len )
4823{
4824 size_t i;
4825
4826 for( i = 0; i < len / 8; i++ )
4827 if( mask[i] != 0xFF )
4828 return( -1 );
4829
4830 for( i = 0; i < len % 8; i++ )
4831 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4832 return( -1 );
4833
4834 return( 0 );
4835}
4836
Hanno Becker56e205e2018-08-16 09:06:12 +01004837/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004838static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004839 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004840{
Hanno Becker56e205e2018-08-16 09:06:12 +01004841 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004842
Hanno Becker56e205e2018-08-16 09:06:12 +01004843 alloc_len = 12; /* Handshake header */
4844 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004845
Hanno Beckerd07df862018-08-16 09:14:58 +01004846 if( add_bitmap )
4847 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004848
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004849 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004850}
Hanno Becker56e205e2018-08-16 09:06:12 +01004851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004853
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004854static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004855{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004856 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004857}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004858
Simon Butcher99000142016-10-13 17:21:01 +01004859int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004864 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004866 }
4867
Hanno Becker12555c62018-08-16 12:47:53 +01004868 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004871 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004872 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004874#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004875 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004876 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004877 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004878 unsigned int recv_msg_seq = (unsigned int)
4879 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004880
Hanno Becker44650b72018-08-16 12:51:11 +01004881 if( ssl_check_hs_header( ssl ) != 0 )
4882 {
4883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4884 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4885 }
4886
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004887 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004888 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4889 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4890 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4891 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004892 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004893 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4894 {
4895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4896 recv_msg_seq,
4897 ssl->handshake->in_msg_seq ) );
4898 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4899 }
4900
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004901 /* Retransmit only on last message from previous flight, to avoid
4902 * too many retransmissions.
4903 * Besides, No sane server ever retransmits HelloVerifyRequest */
4904 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004908 "message_seq = %d, start_of_flight = %d",
4909 recv_msg_seq,
4910 ssl->handshake->in_flight_start_seq ) );
4911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004915 return( ret );
4916 }
4917 }
4918 else
4919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004921 "message_seq = %d, expected = %d",
4922 recv_msg_seq,
4923 ssl->handshake->in_msg_seq ) );
4924 }
4925
Hanno Becker90333da2017-10-10 11:27:13 +01004926 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004927 }
4928 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004929
Hanno Becker6d97ef52018-08-16 13:09:04 +01004930 /* Message reassembly is handled alongside buffering of future
4931 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004932 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004933 * handshake logic layer. */
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04004934 if( ssl_hs_is_proper_fragment( ssl ) == PROPER_HS_FRAGMENT )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004937 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004938 }
4939 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004940 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004942#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004943 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004944 /* With TLS we don't handle fragmentation (for now) */
4945 if( ssl->in_msglen < ssl->in_hslen )
4946 {
4947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4948 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4949 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004950 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004951#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004952
Simon Butcher99000142016-10-13 17:21:01 +01004953 return( 0 );
4954}
4955
4956void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4957{
Hanno Becker0271f962018-08-16 13:23:47 +01004958 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004959
Hanno Becker0271f962018-08-16 13:23:47 +01004960 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004961 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004962
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004963 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004965 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004966 ssl->handshake != NULL )
4967 {
Hanno Becker0271f962018-08-16 13:23:47 +01004968 unsigned offset;
4969 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004970
Hanno Becker0271f962018-08-16 13:23:47 +01004971 /* Increment handshake sequence number */
4972 hs->in_msg_seq++;
4973
4974 /*
4975 * Clear up handshake buffering and reassembly structure.
4976 */
4977
4978 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004979 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004980
4981 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004982 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4983 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004984 offset++, hs_buf++ )
4985 {
4986 *hs_buf = *(hs_buf + 1);
4987 }
4988
4989 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004990 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004991 }
4992#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004993}
4994
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004995/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004996 * DTLS anti-replay: RFC 6347 4.1.2.6
4997 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004998 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4999 * Bit n is set iff record number in_window_top - n has been seen.
5000 *
5001 * Usually, in_window_top is the last record number seen and the lsb of
5002 * in_window is set. The only exception is the initial state (record number 0
5003 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5006static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005007{
5008 ssl->in_window_top = 0;
5009 ssl->in_window = 0;
5010}
5011
5012static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5013{
5014 return( ( (uint64_t) buf[0] << 40 ) |
5015 ( (uint64_t) buf[1] << 32 ) |
5016 ( (uint64_t) buf[2] << 24 ) |
5017 ( (uint64_t) buf[3] << 16 ) |
5018 ( (uint64_t) buf[4] << 8 ) |
5019 ( (uint64_t) buf[5] ) );
5020}
5021
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005022static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5023{
5024 int ret;
5025 unsigned char *original_in_ctr;
5026
5027 // save original in_ctr
5028 original_in_ctr = ssl->in_ctr;
5029
5030 // use counter from record
5031 ssl->in_ctr = record_in_ctr;
5032
5033 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5034
5035 // restore the counter
5036 ssl->in_ctr = original_in_ctr;
5037
5038 return ret;
5039}
5040
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005041/*
5042 * Return 0 if sequence number is acceptable, -1 otherwise
5043 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005044int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005045{
5046 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5047 uint64_t bit;
5048
Hanno Becker7f376f42019-06-12 16:20:48 +01005049 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5050 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5051 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005052 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005053 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005054
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005055 if( rec_seqnum > ssl->in_window_top )
5056 return( 0 );
5057
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005058 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005059
5060 if( bit >= 64 )
5061 return( -1 );
5062
5063 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5064 return( -1 );
5065
5066 return( 0 );
5067}
5068
5069/*
5070 * Update replay window on new validated record
5071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005073{
5074 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5075
Hanno Becker7f376f42019-06-12 16:20:48 +01005076 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5077 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5078 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005079 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005080 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005081
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005082 if( rec_seqnum > ssl->in_window_top )
5083 {
5084 /* Update window_top and the contents of the window */
5085 uint64_t shift = rec_seqnum - ssl->in_window_top;
5086
5087 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005088 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005089 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005090 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005091 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005092 ssl->in_window |= 1;
5093 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005094
5095 ssl->in_window_top = rec_seqnum;
5096 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005097 else
5098 {
5099 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005100 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005101
5102 if( bit < 64 ) /* Always true, but be extra sure */
5103 ssl->in_window |= (uint64_t) 1 << bit;
5104 }
5105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005107
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005108#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005109/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005110static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5111
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005112/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005113 * Without any SSL context, check if a datagram looks like a ClientHello with
5114 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005115 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005116 *
5117 * - if cookie is valid, return 0
5118 * - if ClientHello looks superficially valid but cookie is not,
5119 * fill obuf and set olen, then
5120 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5121 * - otherwise return a specific error code
5122 */
5123static int ssl_check_dtls_clihlo_cookie(
5124 mbedtls_ssl_cookie_write_t *f_cookie_write,
5125 mbedtls_ssl_cookie_check_t *f_cookie_check,
5126 void *p_cookie,
5127 const unsigned char *cli_id, size_t cli_id_len,
5128 const unsigned char *in, size_t in_len,
5129 unsigned char *obuf, size_t buf_len, size_t *olen )
5130{
5131 size_t sid_len, cookie_len;
5132 unsigned char *p;
5133
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005134 /*
5135 * Structure of ClientHello with record and handshake headers,
5136 * and expected values. We don't need to check a lot, more checks will be
5137 * done when actually parsing the ClientHello - skipping those checks
5138 * avoids code duplication and does not make cookie forging any easier.
5139 *
5140 * 0-0 ContentType type; copied, must be handshake
5141 * 1-2 ProtocolVersion version; copied
5142 * 3-4 uint16 epoch; copied, must be 0
5143 * 5-10 uint48 sequence_number; copied
5144 * 11-12 uint16 length; (ignored)
5145 *
5146 * 13-13 HandshakeType msg_type; (ignored)
5147 * 14-16 uint24 length; (ignored)
5148 * 17-18 uint16 message_seq; copied
5149 * 19-21 uint24 fragment_offset; copied, must be 0
5150 * 22-24 uint24 fragment_length; (ignored)
5151 *
5152 * 25-26 ProtocolVersion client_version; (ignored)
5153 * 27-58 Random random; (ignored)
5154 * 59-xx SessionID session_id; 1 byte len + sid_len content
5155 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5156 * ...
5157 *
5158 * Minimum length is 61 bytes.
5159 */
5160 if( in_len < 61 ||
5161 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5162 in[3] != 0 || in[4] != 0 ||
5163 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5164 {
5165 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5166 }
5167
5168 sid_len = in[59];
5169 if( sid_len > in_len - 61 )
5170 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5171
5172 cookie_len = in[60 + sid_len];
5173 if( cookie_len > in_len - 60 )
5174 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5175
5176 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5177 cli_id, cli_id_len ) == 0 )
5178 {
5179 /* Valid cookie */
5180 return( 0 );
5181 }
5182
5183 /*
5184 * If we get here, we've got an invalid cookie, let's prepare HVR.
5185 *
5186 * 0-0 ContentType type; copied
5187 * 1-2 ProtocolVersion version; copied
5188 * 3-4 uint16 epoch; copied
5189 * 5-10 uint48 sequence_number; copied
5190 * 11-12 uint16 length; olen - 13
5191 *
5192 * 13-13 HandshakeType msg_type; hello_verify_request
5193 * 14-16 uint24 length; olen - 25
5194 * 17-18 uint16 message_seq; copied
5195 * 19-21 uint24 fragment_offset; copied
5196 * 22-24 uint24 fragment_length; olen - 25
5197 *
5198 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5199 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5200 *
5201 * Minimum length is 28.
5202 */
5203 if( buf_len < 28 )
5204 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5205
5206 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005207 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005208 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5209 obuf[25] = 0xfe;
5210 obuf[26] = 0xff;
5211
5212 /* Generate and write actual cookie */
5213 p = obuf + 28;
5214 if( f_cookie_write( p_cookie,
5215 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5216 {
5217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5218 }
5219
5220 *olen = p - obuf;
5221
5222 /* Go back and fill length fields */
5223 obuf[27] = (unsigned char)( *olen - 28 );
5224
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005225 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005226 obuf[22] = obuf[14];
5227 obuf[23] = obuf[15];
5228 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005229
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005230 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005231
5232 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5233}
5234
5235/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005236 * Handle possible client reconnect with the same UDP quadruplet
5237 * (RFC 6347 Section 4.2.8).
5238 *
5239 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5240 * that looks like a ClientHello.
5241 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005242 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005243 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005244 * - if the input looks like a ClientHello with a valid cookie,
5245 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005246 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005247 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005248 *
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005249 * This function is called (through ssl_check_client_reconnect()) when an
5250 * unexpected record is found in ssl_get_next_record(), which will discard the
5251 * record if we return 0, and bubble up the return value otherwise (this
5252 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
5253 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005254 */
5255static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5256{
5257 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005258 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005259
Hanno Becker87b56262019-07-10 14:37:41 +01005260 if( ssl->conf->f_cookie_write == NULL ||
5261 ssl->conf->f_cookie_check == NULL )
5262 {
5263 /* If we can't use cookies to verify reachability of the peer,
5264 * drop the record. */
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
5266 "can't check reconnect validity" ) );
Hanno Becker87b56262019-07-10 14:37:41 +01005267 return( 0 );
5268 }
5269
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005270 ret = ssl_check_dtls_clihlo_cookie(
5271 ssl->conf->f_cookie_write,
5272 ssl->conf->f_cookie_check,
5273 ssl->conf->p_cookie,
5274 ssl->cli_id, ssl->cli_id_len,
5275 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005276 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005277
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005278 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5279
5280 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005281 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005282 int send_ret;
5283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
5284 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
5285 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08005286 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005287 * If the error is permanent we'll catch it later,
5288 * if it's not, then hopefully it'll work next time. */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005289 send_ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
5290 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_get_send", send_ret );
5291 (void) send_ret;
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005292 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005293 }
5294
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005295 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005296 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005298 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5299 {
5300 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5301 return( ret );
5302 }
5303
5304 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005305 }
5306
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005307 return( ret );
5308}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005309#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005310
Hanno Becker46483f12019-05-03 13:25:54 +01005311static int ssl_check_record_type( uint8_t record_type )
5312{
5313 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5314 record_type != MBEDTLS_SSL_MSG_ALERT &&
5315 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5316 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5317 {
5318 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5319 }
5320
5321 return( 0 );
5322}
5323
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005324/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005325 * ContentType type;
5326 * ProtocolVersion version;
5327 * uint16 epoch; // DTLS only
5328 * uint48 sequence_number; // DTLS only
5329 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005330 *
5331 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005332 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005333 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5334 *
5335 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005336 * 1. proceed with the record if this function returns 0
5337 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5338 * 3. return CLIENT_RECONNECT if this function return that value
5339 * 4. drop the whole datagram if this function returns anything else.
5340 * Point 2 is needed when the peer is resending, and we have already received
5341 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005342 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005343static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005344 unsigned char *buf,
5345 size_t len,
5346 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005347{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005348 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005349
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005350 size_t const rec_hdr_type_offset = 0;
5351 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005352
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005353 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5354 rec_hdr_type_len;
5355 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005356
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005357 size_t const rec_hdr_ctr_len = 8;
5358#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005359 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005360 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5361 rec_hdr_version_len;
5362
Hanno Beckera5a2b082019-05-15 14:03:01 +01005363#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005364 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5365 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005366 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005367#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5368#endif /* MBEDTLS_SSL_PROTO_DTLS */
5369
5370 size_t rec_hdr_len_offset; /* To be determined */
5371 size_t const rec_hdr_len_len = 2;
5372
5373 /*
5374 * Check minimum lengths for record header.
5375 */
5376
5377#if defined(MBEDTLS_SSL_PROTO_DTLS)
5378 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5379 {
5380 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5381 }
5382 MBEDTLS_SSL_TRANSPORT_ELSE
5383#endif /* MBEDTLS_SSL_PROTO_DTLS */
5384#if defined(MBEDTLS_SSL_PROTO_TLS)
5385 {
5386 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5387 }
5388#endif /* MBEDTLS_SSL_PROTO_DTLS */
5389
5390 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5391 {
5392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5393 (unsigned) len,
5394 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5395 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5396 }
5397
5398 /*
5399 * Parse and validate record content type
5400 */
5401
5402 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005403
5404 /* Check record content type */
5405#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5406 rec->cid_len = 0;
5407
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005408 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005409 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5410 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005411 {
5412 /* Shift pointers to account for record header including CID
5413 * struct {
5414 * ContentType special_type = tls12_cid;
5415 * ProtocolVersion version;
5416 * uint16 epoch;
5417 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005418 * opaque cid[cid_length]; // Additional field compared to
5419 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005420 * uint16 length;
5421 * opaque enc_content[DTLSCiphertext.length];
5422 * } DTLSCiphertext;
5423 */
5424
5425 /* So far, we only support static CID lengths
5426 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005427 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5428 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005429
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005430 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005431 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5433 (unsigned) len,
5434 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005435 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005436 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005437
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005438 /* configured CID len is guaranteed at most 255, see
5439 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5440 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005441 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5442 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005443 }
5444 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005445#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005446 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005447 if( ssl_check_record_type( rec->type ) )
5448 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5450 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005451 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5452 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005453 }
5454
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005455 /*
5456 * Parse and validate record version
5457 */
5458
Hanno Becker8061c6e2019-07-26 08:07:03 +01005459 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5460 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005461 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5462 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005463 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005464
Hanno Becker2881d802019-05-22 14:44:53 +01005465 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5468 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005469 }
5470
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005471 if( mbedtls_ssl_ver_gt( minor_ver,
5472 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5475 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005476 }
5477
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005478 /*
5479 * Parse/Copy record sequence number.
5480 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005481
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005482#if defined(MBEDTLS_SSL_PROTO_DTLS)
5483 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5484 {
5485 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005486 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5487 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005488 rec_hdr_ctr_len );
5489 }
5490 MBEDTLS_SSL_TRANSPORT_ELSE
5491#endif /* MBEDTLS_SSL_PROTO_DTLS */
5492#if defined(MBEDTLS_SSL_PROTO_TLS)
5493 {
5494 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005495 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5496 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005497 }
5498#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005499
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005500 /*
5501 * Parse record length.
5502 */
5503
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005504 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005505 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005506 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5507
Hanno Becker8b09b732019-05-08 12:03:28 +01005508 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005509 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005510 rec->type,
5511 major_ver, minor_ver, rec->data_len ) );
5512
5513 rec->buf = buf;
5514 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005515
Hanno Beckerec014082019-07-26 08:20:27 +01005516 if( rec->data_len == 0 )
5517 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5518
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005519 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005520 * DTLS-related tests.
5521 * Check epoch before checking length constraint because
5522 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5523 * message gets duplicated before the corresponding Finished message,
5524 * the second ChangeCipherSpec should be discarded because it belongs
5525 * to an old epoch, but not because its length is shorter than
5526 * the minimum record length for packets using the new record transform.
5527 * Note that these two kinds of failures are handled differently,
5528 * as an unexpected record is silently skipped but an invalid
5529 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005530 */
5531#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005532 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005533 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005534 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005535
Hanno Beckere0452772019-07-10 17:12:07 +01005536 /* Check that the datagram is large enough to contain a record
5537 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005538 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005539 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5541 (unsigned) len,
5542 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005543 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5544 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005545
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005546 /* Records from other, non-matching epochs are silently discarded.
5547 * (The case of same-port Client reconnects must be considered in
5548 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005549 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005550 {
5551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5552 "expected %d, received %d",
5553 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005554
5555 /* Records from the next epoch are considered for buffering
5556 * (concretely: early Finished messages). */
5557 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5558 {
5559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5560 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5561 }
5562
Hanno Becker87b56262019-07-10 14:37:41 +01005563 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005564 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005565#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005566 /* For records from the correct epoch, check whether their
5567 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005568 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5569 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5572 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5573 }
5574#endif
5575 }
5576#endif /* MBEDTLS_SSL_PROTO_DTLS */
5577
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005578 return( 0 );
5579}
Paul Bakker5121ce52009-01-03 21:22:43 +00005580
Hanno Becker87b56262019-07-10 14:37:41 +01005581
5582#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5583static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5584{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005585 unsigned int rec_epoch = (unsigned int)
5586 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005587
5588 /*
5589 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5590 * access the first byte of record content (handshake type), as we
5591 * have an active transform (possibly iv_len != 0), so use the
5592 * fact that the record header len is 13 instead.
5593 */
5594 if( rec_epoch == 0 &&
5595 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5596 MBEDTLS_SSL_IS_SERVER &&
5597 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5598 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5599 ssl->in_left > 13 &&
5600 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5601 {
5602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5603 "from the same port" ) );
5604 return( ssl_handle_possible_reconnect( ssl ) );
5605 }
5606
5607 return( 0 );
5608}
5609#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5610
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005611/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005612 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005613 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005614static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5615 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005616{
5617 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005620 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5623 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 ret = mbedtls_ssl_hw_record_read( ssl );
5628 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5631 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005632 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005633
5634 if( ret == 0 )
5635 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005636 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005638 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005640 unsigned char const old_msg_type = rec->type;
5641
Hanno Becker611a83b2018-01-03 14:27:32 +00005642 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005643 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005646
Hanno Beckera5a2b082019-05-15 14:03:01 +01005647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005648 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005649 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5650 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005651 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005653 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005654 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005655#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005656
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 return( ret );
5658 }
5659
Hanno Becker106f3da2019-07-12 09:35:58 +01005660 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005661 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005662 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005663 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005664 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005665
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005667 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005668
Hanno Beckera5a2b082019-05-15 14:03:01 +01005669#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005670 /* We have already checked the record content type
5671 * in ssl_parse_record_header(), failing or silently
5672 * dropping the record in the case of an unknown type.
5673 *
5674 * Since with the use of CIDs, the record content type
5675 * might change during decryption, re-check the record
5676 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005677 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005678 {
5679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5681 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005682#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005683
Hanno Becker106f3da2019-07-12 09:35:58 +01005684 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005685 {
5686#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005687 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005688 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005689 {
5690 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5692 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5693 }
5694#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5695
5696 ssl->nb_zero++;
5697
5698 /*
5699 * Three or more empty messages may be a DoS attack
5700 * (excessive CPU consumption).
5701 */
5702 if( ssl->nb_zero > 3 )
5703 {
5704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005705 "messages, possible DoS attack" ) );
5706 /* Treat the records as if they were not properly authenticated,
5707 * thereby failing the connection if we see more than allowed
5708 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005709 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5710 }
5711 }
5712 else
5713 ssl->nb_zero = 0;
5714
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005715 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5716#if defined(MBEDTLS_SSL_PROTO_TLS)
5717 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005718 {
5719 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005720 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005721 if( ++ssl->in_ctr[i - 1] != 0 )
5722 break;
5723
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005724 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005725 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005726 {
5727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5728 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5729 }
5730 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005731#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005732
Paul Bakker5121ce52009-01-03 21:22:43 +00005733 }
5734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005736 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005739 }
5740#endif
5741
Hanno Beckerf0242852019-07-09 17:30:02 +01005742 /* Check actual (decrypted) record content length against
5743 * configured maximum. */
5744 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5745 {
5746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5747 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5748 }
5749
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005750 return( 0 );
5751}
5752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005754
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005755/*
5756 * Read a record.
5757 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005758 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5759 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5760 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005761 */
Hanno Becker1097b342018-08-15 14:09:41 +01005762
5763/* Helper functions for mbedtls_ssl_read_record(). */
5764static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005765static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5766static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005767
Hanno Becker327c93b2018-08-15 13:56:18 +01005768int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005769 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005770{
5771 int ret;
5772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005774
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005775 if( ssl->keep_current_message == 0 )
5776 {
5777 do {
Simon Butcher99000142016-10-13 17:21:01 +01005778
Hanno Becker26994592018-08-15 14:14:59 +01005779 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005780 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005781 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005782
Hanno Beckere74d5562018-08-15 14:26:08 +01005783 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005784 {
Hanno Becker40f50842018-08-15 14:48:01 +01005785#if defined(MBEDTLS_SSL_PROTO_DTLS)
5786 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005787
Hanno Becker40f50842018-08-15 14:48:01 +01005788 /* We only check for buffered messages if the
5789 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005790 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005791 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005792 {
Hanno Becker40f50842018-08-15 14:48:01 +01005793 if( ssl_load_buffered_message( ssl ) == 0 )
5794 have_buffered = 1;
5795 }
5796
5797 if( have_buffered == 0 )
5798#endif /* MBEDTLS_SSL_PROTO_DTLS */
5799 {
5800 ret = ssl_get_next_record( ssl );
5801 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5802 continue;
5803
5804 if( ret != 0 )
5805 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005806 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005807 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005808 return( ret );
5809 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005810 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005811 }
5812
5813 ret = mbedtls_ssl_handle_message_type( ssl );
5814
Hanno Becker40f50842018-08-15 14:48:01 +01005815#if defined(MBEDTLS_SSL_PROTO_DTLS)
5816 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5817 {
5818 /* Buffer future message */
5819 ret = ssl_buffer_message( ssl );
5820 if( ret != 0 )
5821 return( ret );
5822
5823 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5824 }
5825#endif /* MBEDTLS_SSL_PROTO_DTLS */
5826
Hanno Becker90333da2017-10-10 11:27:13 +01005827 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5828 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005829
5830 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005831 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005832 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005833 return( ret );
5834 }
5835
Hanno Becker327c93b2018-08-15 13:56:18 +01005836 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005837 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005838 {
5839 mbedtls_ssl_update_handshake_status( ssl );
5840 }
Simon Butcher99000142016-10-13 17:21:01 +01005841 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005842 else
Simon Butcher99000142016-10-13 17:21:01 +01005843 {
Hanno Becker02f59072018-08-15 14:00:24 +01005844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005845 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005846 }
5847
5848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5849
5850 return( 0 );
5851}
5852
Hanno Becker40f50842018-08-15 14:48:01 +01005853#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005854static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005855{
Hanno Becker40f50842018-08-15 14:48:01 +01005856 if( ssl->in_left > ssl->next_record_offset )
5857 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005858
Hanno Becker40f50842018-08-15 14:48:01 +01005859 return( 0 );
5860}
5861
5862static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5863{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005865 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005866 int ret = 0;
5867
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005868 if( hs == NULL )
5869 return( -1 );
5870
Hanno Beckere00ae372018-08-20 09:39:42 +01005871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5872
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005873 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5874 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5875 {
5876 /* Check if we have seen a ChangeCipherSpec before.
5877 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005878 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005879 {
5880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5881 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005882 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005883 }
5884
Hanno Becker39b8bc92018-08-28 17:17:13 +01005885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005886 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5887 ssl->in_msglen = 1;
5888 ssl->in_msg[0] = 1;
5889
5890 /* As long as they are equal, the exact value doesn't matter. */
5891 ssl->in_left = 0;
5892 ssl->next_record_offset = 0;
5893
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005894 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005895 goto exit;
5896 }
Hanno Becker37f95322018-08-16 13:55:32 +01005897
Hanno Beckerb8f50142018-08-28 10:01:34 +01005898#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005899 /* Debug only */
5900 {
5901 unsigned offset;
5902 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5903 {
5904 hs_buf = &hs->buffering.hs[offset];
5905 if( hs_buf->is_valid == 1 )
5906 {
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5908 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005909 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005910 }
5911 }
5912 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005913#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005914
5915 /* Check if we have buffered and/or fully reassembled the
5916 * next handshake message. */
5917 hs_buf = &hs->buffering.hs[0];
5918 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5919 {
5920 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005921 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005922
5923 /* Double-check that we haven't accidentally buffered
5924 * a message that doesn't fit into the input buffer. */
5925 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5926 {
5927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5929 }
5930
5931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5932 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5933 hs_buf->data, msg_len + 12 );
5934
5935 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5936 ssl->in_hslen = msg_len + 12;
5937 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005938 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005939
5940 ret = 0;
5941 goto exit;
5942 }
5943 else
5944 {
5945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5946 hs->in_msg_seq ) );
5947 }
5948
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005949 ret = -1;
5950
5951exit:
5952
5953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5954 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005955}
5956
Hanno Beckera02b0b42018-08-21 17:20:27 +01005957static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5958 size_t desired )
5959{
5960 int offset;
5961 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5963 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005964
Hanno Becker01315ea2018-08-21 17:22:17 +01005965 /* Get rid of future records epoch first, if such exist. */
5966 ssl_free_buffered_record( ssl );
5967
5968 /* Check if we have enough space available now. */
5969 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5970 hs->buffering.total_bytes_buffered ) )
5971 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005973 return( 0 );
5974 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005975
Hanno Becker4f432ad2018-08-28 10:02:32 +01005976 /* We don't have enough space to buffer the next expected handshake
5977 * message. Remove buffers used for future messages to gain space,
5978 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005979 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5980 offset >= 0; offset-- )
5981 {
5982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5983 offset ) );
5984
Hanno Beckerb309b922018-08-23 13:18:05 +01005985 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005986
5987 /* Check if we have enough space available now. */
5988 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5989 hs->buffering.total_bytes_buffered ) )
5990 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005992 return( 0 );
5993 }
5994 }
5995
5996 return( -1 );
5997}
5998
Hanno Becker40f50842018-08-15 14:48:01 +01005999static int ssl_buffer_message( mbedtls_ssl_context *ssl )
6000{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006001 int ret = 0;
6002 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6003
6004 if( hs == NULL )
6005 return( 0 );
6006
6007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
6008
6009 switch( ssl->in_msgtype )
6010 {
6011 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006013
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006014 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006015 break;
6016
6017 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006018 {
6019 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006020 unsigned recv_msg_seq = (unsigned)
6021 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006022
Hanno Becker37f95322018-08-16 13:55:32 +01006023 mbedtls_ssl_hs_buffer *hs_buf;
6024 size_t msg_len = ssl->in_hslen - 12;
6025
6026 /* We should never receive an old handshake
6027 * message - double-check nonetheless. */
6028 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6029 {
6030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6032 }
6033
6034 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6035 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6036 {
6037 /* Silently ignore -- message too far in the future */
6038 MBEDTLS_SSL_DEBUG_MSG( 2,
6039 ( "Ignore future HS message with sequence number %u, "
6040 "buffering window %u - %u",
6041 recv_msg_seq, ssl->handshake->in_msg_seq,
6042 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6043
6044 goto exit;
6045 }
6046
6047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6048 recv_msg_seq, recv_msg_seq_offset ) );
6049
6050 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6051
6052 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006053 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006054 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006055 size_t reassembly_buf_sz;
6056
Hanno Becker37f95322018-08-16 13:55:32 +01006057 hs_buf->is_fragmented =
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04006058 ( ssl_hs_is_proper_fragment( ssl ) == PROPER_HS_FRAGMENT );
Hanno Becker37f95322018-08-16 13:55:32 +01006059
6060 /* We copy the message back into the input buffer
6061 * after reassembly, so check that it's not too large.
6062 * This is an implementation-specific limitation
6063 * and not one from the standard, hence it is not
6064 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006065 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006066 {
6067 /* Ignore message */
6068 goto exit;
6069 }
6070
Hanno Beckere0b150f2018-08-21 15:51:03 +01006071 /* Check if we have enough space to buffer the message. */
6072 if( hs->buffering.total_bytes_buffered >
6073 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6074 {
6075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6077 }
6078
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006079 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6080 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006081
6082 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6083 hs->buffering.total_bytes_buffered ) )
6084 {
6085 if( recv_msg_seq_offset > 0 )
6086 {
6087 /* If we can't buffer a future message because
6088 * of space limitations -- ignore. */
6089 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",
6090 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6091 (unsigned) hs->buffering.total_bytes_buffered ) );
6092 goto exit;
6093 }
Hanno Beckere1801392018-08-21 16:51:05 +01006094 else
6095 {
6096 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",
6097 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6098 (unsigned) hs->buffering.total_bytes_buffered ) );
6099 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006100
Hanno Beckera02b0b42018-08-21 17:20:27 +01006101 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006102 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006103 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",
6104 (unsigned) msg_len,
6105 (unsigned) reassembly_buf_sz,
6106 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006107 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006108 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6109 goto exit;
6110 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006111 }
6112
6113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6114 msg_len ) );
6115
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006116 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6117 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006118 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006119 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006120 goto exit;
6121 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006122 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006123
6124 /* Prepare final header: copy msg_type, length and message_seq,
6125 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006126 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006127 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006128 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006129
6130 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006131
6132 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006133 }
6134 else
6135 {
6136 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006137 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 +01006138 {
6139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6140 /* Ignore */
6141 goto exit;
6142 }
6143 }
6144
Hanno Becker4422bbb2018-08-20 09:40:19 +01006145 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006146 {
6147 size_t frag_len, frag_off;
6148 unsigned char * const msg = hs_buf->data + 12;
6149
6150 /*
6151 * Check and copy current fragment
6152 */
6153
6154 /* Validation of header fields already done in
6155 * mbedtls_ssl_prepare_handshake_record(). */
6156 frag_off = ssl_get_hs_frag_off( ssl );
6157 frag_len = ssl_get_hs_frag_len( ssl );
6158
6159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6160 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006161 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006162
6163 if( hs_buf->is_fragmented )
6164 {
6165 unsigned char * const bitmask = msg + msg_len;
6166 ssl_bitmask_set( bitmask, frag_off, frag_len );
6167 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6168 msg_len ) == 0 );
6169 }
6170 else
6171 {
6172 hs_buf->is_complete = 1;
6173 }
6174
6175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6176 hs_buf->is_complete ? "" : "not yet " ) );
6177 }
6178
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006179 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006180 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006181
6182 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006183 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006184 break;
6185 }
6186
6187exit:
6188
6189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6190 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006191}
6192#endif /* MBEDTLS_SSL_PROTO_DTLS */
6193
Hanno Becker1097b342018-08-15 14:09:41 +01006194static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006195{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006196 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006197 * Consume last content-layer message and potentially
6198 * update in_msglen which keeps track of the contents'
6199 * consumption state.
6200 *
6201 * (1) Handshake messages:
6202 * Remove last handshake message, move content
6203 * and adapt in_msglen.
6204 *
6205 * (2) Alert messages:
6206 * Consume whole record content, in_msglen = 0.
6207 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006208 * (3) Change cipher spec:
6209 * Consume whole record content, in_msglen = 0.
6210 *
6211 * (4) Application data:
6212 * Don't do anything - the record layer provides
6213 * the application data as a stream transport
6214 * and consumes through mbedtls_ssl_read only.
6215 *
6216 */
6217
6218 /* Case (1): Handshake messages */
6219 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006220 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006221 /* Hard assertion to be sure that no application data
6222 * is in flight, as corrupting ssl->in_msglen during
6223 * ssl->in_offt != NULL is fatal. */
6224 if( ssl->in_offt != NULL )
6225 {
6226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6228 }
6229
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006230 /*
6231 * Get next Handshake message in the current record
6232 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006233
Hanno Becker4a810fb2017-05-24 16:27:30 +01006234 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006235 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006236 * current handshake content: If DTLS handshake
6237 * fragmentation is used, that's the fragment
6238 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006239 * size here is faulty and should be changed at
6240 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006241 * (2) While it doesn't seem to cause problems, one
6242 * has to be very careful not to assume that in_hslen
6243 * is always <= in_msglen in a sensible communication.
6244 * Again, it's wrong for DTLS handshake fragmentation.
6245 * The following check is therefore mandatory, and
6246 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006247 * Additionally, ssl->in_hslen might be arbitrarily out of
6248 * bounds after handling a DTLS message with an unexpected
6249 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006250 */
6251 if( ssl->in_hslen < ssl->in_msglen )
6252 {
6253 ssl->in_msglen -= ssl->in_hslen;
6254 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6255 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006256
Hanno Becker4a810fb2017-05-24 16:27:30 +01006257 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6258 ssl->in_msg, ssl->in_msglen );
6259 }
6260 else
6261 {
6262 ssl->in_msglen = 0;
6263 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006264
Hanno Becker4a810fb2017-05-24 16:27:30 +01006265 ssl->in_hslen = 0;
6266 }
6267 /* Case (4): Application data */
6268 else if( ssl->in_offt != NULL )
6269 {
6270 return( 0 );
6271 }
6272 /* Everything else (CCS & Alerts) */
6273 else
6274 {
6275 ssl->in_msglen = 0;
6276 }
6277
Hanno Becker1097b342018-08-15 14:09:41 +01006278 return( 0 );
6279}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006280
Hanno Beckere74d5562018-08-15 14:26:08 +01006281static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6282{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006283 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006284 return( 1 );
6285
6286 return( 0 );
6287}
6288
Hanno Becker5f066e72018-08-16 14:56:31 +01006289#if defined(MBEDTLS_SSL_PROTO_DTLS)
6290
6291static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6292{
6293 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6294 if( hs == NULL )
6295 return;
6296
Hanno Becker01315ea2018-08-21 17:22:17 +01006297 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006298 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006299 hs->buffering.total_bytes_buffered -=
6300 hs->buffering.future_record.len;
6301
6302 mbedtls_free( hs->buffering.future_record.data );
6303 hs->buffering.future_record.data = NULL;
6304 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006305}
6306
6307static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6308{
6309 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6310 unsigned char * rec;
6311 size_t rec_len;
6312 unsigned rec_epoch;
6313
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006314 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006315 return( 0 );
6316
6317 if( hs == NULL )
6318 return( 0 );
6319
Hanno Becker5f066e72018-08-16 14:56:31 +01006320 rec = hs->buffering.future_record.data;
6321 rec_len = hs->buffering.future_record.len;
6322 rec_epoch = hs->buffering.future_record.epoch;
6323
6324 if( rec == NULL )
6325 return( 0 );
6326
Hanno Becker4cb782d2018-08-20 11:19:05 +01006327 /* Only consider loading future records if the
6328 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006329 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006330 return( 0 );
6331
Hanno Becker5f066e72018-08-16 14:56:31 +01006332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6333
6334 if( rec_epoch != ssl->in_epoch )
6335 {
6336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6337 goto exit;
6338 }
6339
6340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6341
6342 /* Double-check that the record is not too large */
6343 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6344 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6345 {
6346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6347 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6348 }
6349
Teppo Järvelin91d79382019-10-02 09:09:31 +03006350 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006351 ssl->in_left = rec_len;
6352 ssl->next_record_offset = 0;
6353
6354 ssl_free_buffered_record( ssl );
6355
6356exit:
6357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6358 return( 0 );
6359}
6360
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006361static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6362 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006363{
6364 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006365
6366 /* Don't buffer future records outside handshakes. */
6367 if( hs == NULL )
6368 return( 0 );
6369
6370 /* Only buffer handshake records (we are only interested
6371 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006372 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006373 return( 0 );
6374
6375 /* Don't buffer more than one future epoch record. */
6376 if( hs->buffering.future_record.data != NULL )
6377 return( 0 );
6378
Hanno Becker01315ea2018-08-21 17:22:17 +01006379 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006380 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006381 hs->buffering.total_bytes_buffered ) )
6382 {
6383 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 +01006384 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006385 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006386 return( 0 );
6387 }
6388
Hanno Becker5f066e72018-08-16 14:56:31 +01006389 /* Buffer record */
6390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6391 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006392 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006393
6394 /* ssl_parse_record_header() only considers records
6395 * of the next epoch as candidates for buffering. */
6396 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006397 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006398
6399 hs->buffering.future_record.data =
6400 mbedtls_calloc( 1, hs->buffering.future_record.len );
6401 if( hs->buffering.future_record.data == NULL )
6402 {
6403 /* If we run out of RAM trying to buffer a
6404 * record from the next epoch, just ignore. */
6405 return( 0 );
6406 }
6407
Teppo Järvelin91d79382019-10-02 09:09:31 +03006408 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006409
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006410 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006411 return( 0 );
6412}
6413
6414#endif /* MBEDTLS_SSL_PROTO_DTLS */
6415
Hanno Beckere74d5562018-08-15 14:26:08 +01006416static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006417{
6418 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006419 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006420
Hanno Becker5f066e72018-08-16 14:56:31 +01006421#if defined(MBEDTLS_SSL_PROTO_DTLS)
6422 /* We might have buffered a future record; if so,
6423 * and if the epoch matches now, load it.
6424 * On success, this call will set ssl->in_left to
6425 * the length of the buffered record, so that
6426 * the calls to ssl_fetch_input() below will
6427 * essentially be no-ops. */
6428 ret = ssl_load_buffered_record( ssl );
6429 if( ret != 0 )
6430 return( ret );
6431#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006432
Hanno Becker8b09b732019-05-08 12:03:28 +01006433 /* Ensure that we have enough space available for the default form
6434 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6435 * with no space for CIDs counted in). */
6436 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6437 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006440 return( ret );
6441 }
6442
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006443 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6444 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006447 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006448 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006449 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6450 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006451 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006452 if( ret != 0 )
6453 return( ret );
6454
6455 /* Fall through to handling of unexpected records */
6456 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6457 }
6458
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006459 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6460 {
Hanno Becker87b56262019-07-10 14:37:41 +01006461#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006462 /* Reset in pointers to default state for TLS/DTLS records,
6463 * assuming no CID and no offset between record content and
6464 * record plaintext. */
6465 ssl_update_in_pointers( ssl );
6466
Hanno Becker69412452019-07-12 08:33:49 +01006467 /* Setup internal message pointers from record structure. */
6468 ssl->in_msgtype = rec.type;
6469#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6470 ssl->in_len = ssl->in_cid + rec.cid_len;
6471#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006472 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006473 ssl->in_msglen = rec.data_len;
6474
Hanno Becker87b56262019-07-10 14:37:41 +01006475 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02006476 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker87b56262019-07-10 14:37:41 +01006477 if( ret != 0 )
6478 return( ret );
6479#endif
6480
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006481 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006482 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006483
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6485 "(header)" ) );
6486 }
6487 else
6488 {
6489 /* Skip invalid record and the rest of the datagram */
6490 ssl->next_record_offset = 0;
6491 ssl->in_left = 0;
6492
6493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6494 "(header)" ) );
6495 }
6496
6497 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006498 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006499 }
Hanno Becker87b56262019-07-10 14:37:41 +01006500 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006501#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006502 {
6503 return( ret );
6504 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006505 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006508 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006509 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006510 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006511 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006512 if( ssl->next_record_offset < ssl->in_left )
6513 {
6514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6515 }
6516 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006517 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006518#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006519#if defined(MBEDTLS_SSL_PROTO_TLS)
6520 {
Hanno Beckere0452772019-07-10 17:12:07 +01006521 /*
6522 * Fetch record contents from underlying transport.
6523 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006524 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006525 if( ret != 0 )
6526 {
6527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6528 return( ret );
6529 }
6530
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006531 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006532 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006533#endif /* MBEDTLS_SSL_PROTO_TLS */
6534
6535 /*
6536 * Decrypt record contents.
6537 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006538
Hanno Beckera89610a2019-07-11 13:07:45 +01006539 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006542 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006543 {
6544 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006545 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006546 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006547 /* Except when waiting for Finished as a bad mac here
6548 * probably means something went wrong in the handshake
6549 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6550 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6551 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6552 {
6553#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6554 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6555 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006556 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006557 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6558 }
6559#endif
6560 return( ret );
6561 }
6562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006563#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006564 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6565 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6568 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006569 }
6570#endif
6571
Hanno Becker4a810fb2017-05-24 16:27:30 +01006572 /* As above, invalid records cause
6573 * dismissal of the whole datagram. */
6574
6575 ssl->next_record_offset = 0;
6576 ssl->in_left = 0;
6577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006579 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006580 }
6581
6582 return( ret );
6583 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006584 MBEDTLS_SSL_TRANSPORT_ELSE
6585#endif /* MBEDTLS_SSL_PROTO_DTLS */
6586#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006587 {
6588 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006589#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6590 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006591 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006592 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006594 }
6595#endif
6596 return( ret );
6597 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006598#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006599 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006600
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006601
6602 /* Reset in pointers to default state for TLS/DTLS records,
6603 * assuming no CID and no offset between record content and
6604 * record plaintext. */
6605 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006606#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6607 ssl->in_len = ssl->in_cid + rec.cid_len;
6608#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006609 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006610
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006611 /* The record content type may change during decryption,
6612 * so re-read it. */
6613 ssl->in_msgtype = rec.type;
6614 /* Also update the input buffer, because unfortunately
6615 * the server-side ssl_parse_client_hello() reparses the
6616 * record header when receiving a ClientHello initiating
6617 * a renegotiation. */
6618 ssl->in_hdr[0] = rec.type;
6619 ssl->in_msg = rec.buf + rec.data_offset;
6620 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006621 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006622
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006623#if defined(MBEDTLS_ZLIB_SUPPORT)
6624 if( ssl->transform_in != NULL &&
6625 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6626 {
6627 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6628 {
6629 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6630 return( ret );
6631 }
6632
6633 /* Check actual (decompress) record content length against
6634 * configured maximum. */
6635 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6636 {
6637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6638 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6639 }
6640 }
6641#endif /* MBEDTLS_ZLIB_SUPPORT */
6642
Simon Butcher99000142016-10-13 17:21:01 +01006643 return( 0 );
6644}
6645
6646int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6647{
6648 int ret;
6649
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006650 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006651 * Handle particular types of records
6652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 {
Simon Butcher99000142016-10-13 17:21:01 +01006655 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6656 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006657 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006658 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006659 }
6660
Hanno Beckere678eaa2018-08-21 14:57:46 +01006661 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006662 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006663 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006664 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6666 ssl->in_msglen ) );
6667 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006668 }
6669
Hanno Beckere678eaa2018-08-21 14:57:46 +01006670 if( ssl->in_msg[0] != 1 )
6671 {
6672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6673 ssl->in_msg[0] ) );
6674 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6675 }
6676
6677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006678 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006679 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6680 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6681 {
6682 if( ssl->handshake == NULL )
6683 {
6684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6685 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6686 }
6687
6688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6689 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6690 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006691#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006692 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006694 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006695 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006696 if( ssl->in_msglen != 2 )
6697 {
6698 /* Note: Standard allows for more than one 2 byte alert
6699 to be packed in a single message, but Mbed TLS doesn't
6700 currently support this. */
6701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6702 ssl->in_msglen ) );
6703 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6704 }
6705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006707 ssl->in_msg[0], ssl->in_msg[1] ) );
6708
6709 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006710 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006712 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006715 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006716 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006717 }
6718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6720 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6723 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006724 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006725
6726#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6727 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6728 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6729 {
Hanno Becker90333da2017-10-10 11:27:13 +01006730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006731 /* Will be handled when trying to parse ServerHello */
6732 return( 0 );
6733 }
6734#endif
6735
6736#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006737 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006738 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6739 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006740 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6741 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6742 {
6743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6744 /* Will be handled in mbedtls_ssl_parse_certificate() */
6745 return( 0 );
6746 }
6747#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6748
6749 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006750 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 }
6752
Hanno Beckerc76c6192017-06-06 10:03:17 +01006753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006754 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006755 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006756 /* Drop unexpected ApplicationData records,
6757 * except at the beginning of renegotiations */
6758 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6759 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6760#if defined(MBEDTLS_SSL_RENEGOTIATION)
6761 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6762 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006763#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006764 )
6765 {
6766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6767 return( MBEDTLS_ERR_SSL_NON_FATAL );
6768 }
6769
6770 if( ssl->handshake != NULL &&
6771 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6772 {
6773 ssl_handshake_wrapup_free_hs_transform( ssl );
6774 }
6775 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006776#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006777
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 return( 0 );
6779}
6780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006782{
6783 int ret;
6784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6786 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6787 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006788 {
6789 return( ret );
6790 }
6791
6792 return( 0 );
6793}
6794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006796 unsigned char level,
6797 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006798{
6799 int ret;
6800
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006801 if( ssl == NULL || ssl->conf == NULL )
6802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006808 ssl->out_msglen = 2;
6809 ssl->out_msg[0] = level;
6810 ssl->out_msg[1] = message;
6811
Hanno Becker67bc7c32018-08-06 11:33:50 +01006812 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006815 return( ret );
6816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006818
6819 return( 0 );
6820}
6821
Hanno Becker17572472019-02-08 07:19:04 +00006822#if defined(MBEDTLS_X509_CRT_PARSE_C)
6823static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6824{
6825#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6826 if( session->peer_cert != NULL )
6827 {
6828 mbedtls_x509_crt_free( session->peer_cert );
6829 mbedtls_free( session->peer_cert );
6830 session->peer_cert = NULL;
6831 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006832#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006833 if( session->peer_cert_digest != NULL )
6834 {
6835 /* Zeroization is not necessary. */
6836 mbedtls_free( session->peer_cert_digest );
6837 session->peer_cert_digest = NULL;
6838 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6839 session->peer_cert_digest_len = 0;
6840 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006841#else
6842 ((void) session);
6843#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006844}
6845#endif /* MBEDTLS_X509_CRT_PARSE_C */
6846
Paul Bakker5121ce52009-01-03 21:22:43 +00006847/*
6848 * Handshake functions
6849 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006850#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006851/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006853{
Hanno Beckerdf645962019-06-26 13:02:22 +01006854 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6855 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858
Hanno Becker5097cba2019-02-05 13:36:46 +00006859 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006862 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6863 {
6864 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6865 }
6866 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6867 {
6868 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6869 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006870 else
6871 {
6872 ssl->state = MBEDTLS_SSL_INVALID;
6873 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006874 return( 0 );
6875 }
6876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006879}
6880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006882{
Hanno Beckerdf645962019-06-26 13:02:22 +01006883 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6884 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006887
Hanno Becker5097cba2019-02-05 13:36:46 +00006888 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006891 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6892 {
6893 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6894 }
6895 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6896 {
6897 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6898 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006899 else
6900 {
6901 ssl->state = MBEDTLS_SSL_INVALID;
6902 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006903 return( 0 );
6904 }
6905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006908}
Gilles Peskinef9828522017-05-03 12:28:43 +02006909
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006910#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006911/* Some certificate support -> implement write and parse */
6912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006914{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006916 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006918 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6919 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006922
Hanno Becker5097cba2019-02-05 13:36:46 +00006923 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006926 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6927 {
6928 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6929 }
6930 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6931 {
6932 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6933 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006934 else
6935 {
6936 ssl->state = MBEDTLS_SSL_INVALID;
6937 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006938 return( 0 );
6939 }
6940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006942 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6943 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006944 {
6945 if( ssl->client_auth == 0 )
6946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006948 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6949 {
6950 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6951 }
6952 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6953 {
6954 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6955 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006956 else
6957 {
6958 ssl->state = MBEDTLS_SSL_INVALID;
6959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006960 return( 0 );
6961 }
6962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006963#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006964 /*
6965 * If using SSLv3 and got no cert, send an Alert message
6966 * (otherwise an empty Certificate message will be sent).
6967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006969 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006970 {
6971 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6973 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6974 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006977 goto write_msg;
6978 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981#endif /* MBEDTLS_SSL_CLI_C */
6982#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006983 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6988 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006989 }
6990 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006991#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006993 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006994
6995 /*
6996 * 0 . 0 handshake type
6997 * 1 . 3 handshake length
6998 * 4 . 6 length of all certs
6999 * 7 . 9 length of cert. 1
7000 * 10 . n-1 peer certificate
7001 * n . n+2 length of cert. 2
7002 * n+3 . ... upper level cert, etc.
7003 */
7004 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007006
Paul Bakker29087132010-03-21 21:03:34 +00007007 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007008 {
7009 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10007010 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00007011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10007013 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007015 }
7016
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007017 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007018
Teppo Järvelin91d79382019-10-02 09:09:31 +03007019 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007020 i += n; crt = crt->next;
7021 }
7022
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007023 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007024
7025 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7027 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007028
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007029#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007030write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007032
Jarno Lamsa2b205162019-11-12 15:36:21 +02007033 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7034 {
7035 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7036 }
7037 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7038 {
7039 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7040 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007041 else
7042 {
7043 ssl->state = MBEDTLS_SSL_INVALID;
7044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007045
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007046 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007047 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007049 return( ret );
7050 }
7051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007053
Paul Bakkered27a042013-04-18 22:46:23 +02007054 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007055}
7056
Hanno Becker285ff0c2019-01-31 07:44:03 +00007057#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007058
7059#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007060static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7061 unsigned char *crt_buf,
7062 size_t crt_buf_len )
7063{
7064 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7065
7066 if( peer_crt == NULL )
7067 return( -1 );
7068
7069 if( peer_crt->raw.len != crt_buf_len )
7070 return( -1 );
7071
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007072 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007073}
Hanno Becker5882dd02019-06-06 16:25:57 +01007074#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007075static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7076 unsigned char *crt_buf,
7077 size_t crt_buf_len )
7078{
7079 int ret;
7080 unsigned char const * const peer_cert_digest =
7081 ssl->session->peer_cert_digest;
7082 mbedtls_md_type_t const peer_cert_digest_type =
7083 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007084 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007085 mbedtls_md_info_from_type( peer_cert_digest_type );
7086 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7087 size_t digest_len;
7088
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007089 if( peer_cert_digest == NULL ||
7090 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7091 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007092 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007093 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007094
7095 digest_len = mbedtls_md_get_size( digest_info );
7096 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7097 return( -1 );
7098
7099 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7100 if( ret != 0 )
7101 return( -1 );
7102
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007103 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007104}
Hanno Becker5882dd02019-06-06 16:25:57 +01007105#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007106#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007107
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007108/*
7109 * Once the certificate message is read, parse it into a cert chain and
7110 * perform basic checks, but leave actual verification to the caller
7111 */
Hanno Becker35e41772019-02-05 15:37:23 +00007112static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7113 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007114{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007115 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007116#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7117 int crt_cnt=0;
7118#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007119 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007120 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007125 mbedtls_ssl_pend_fatal_alert( ssl,
7126 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007128 }
7129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7131 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007134 mbedtls_ssl_pend_fatal_alert( ssl,
7135 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007137 }
7138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007140
Paul Bakker5121ce52009-01-03 21:22:43 +00007141 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007143 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007144 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007145
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007146 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007150 mbedtls_ssl_pend_fatal_alert( ssl,
7151 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007153 }
7154
Hanno Becker33c3dc82019-01-30 14:46:46 +00007155 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7156 i += 3;
7157
Hanno Becker33c3dc82019-01-30 14:46:46 +00007158 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007159 while( i < ssl->in_hslen )
7160 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007161 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007162 if ( i + 3 > ssl->in_hslen ) {
7163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007164 mbedtls_ssl_pend_fatal_alert( ssl,
7165 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007166 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7167 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007168 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7169 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007170 if( ssl->in_msg[i] != 0 )
7171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007173 mbedtls_ssl_pend_fatal_alert( ssl,
7174 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007176 }
7177
Hanno Becker33c3dc82019-01-30 14:46:46 +00007178 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007179 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007180 i += 3;
7181
7182 if( n < 128 || i + n > ssl->in_hslen )
7183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007185 mbedtls_ssl_pend_fatal_alert( ssl,
7186 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007188 }
7189
Hanno Becker33c3dc82019-01-30 14:46:46 +00007190 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007191#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7192 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007193 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7194 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007195 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007196 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007197 /* During client-side renegotiation, check that the server's
7198 * end-CRTs hasn't changed compared to the initial handshake,
7199 * mitigating the triple handshake attack. On success, reuse
7200 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7202 if( ssl_check_peer_crt_unchanged( ssl,
7203 &ssl->in_msg[i],
7204 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007205 {
Hanno Becker35e41772019-02-05 15:37:23 +00007206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007207 mbedtls_ssl_pend_fatal_alert( ssl,
7208 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007209 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007210 }
Hanno Becker35e41772019-02-05 15:37:23 +00007211
7212 /* Now we can safely free the original chain. */
7213 ssl_clear_peer_cert( ssl->session );
7214 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007215#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7216
Hanno Becker33c3dc82019-01-30 14:46:46 +00007217 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007218#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007219 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007220#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007221 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007222 * it in-place from the input buffer instead of making a copy. */
7223 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7224#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007225 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007226 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007227 case 0: /*ok*/
7228 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7229 /* Ignore certificate with an unknown algorithm: maybe a
7230 prior certificate was already trusted. */
7231 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007232
Hanno Becker33c3dc82019-01-30 14:46:46 +00007233 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7234 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7235 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007236
Hanno Becker33c3dc82019-01-30 14:46:46 +00007237 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7238 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7239 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007240
Hanno Becker33c3dc82019-01-30 14:46:46 +00007241 default:
7242 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7243 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007244 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007245 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7246 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007247 }
7248
7249 i += n;
7250 }
7251
Hanno Becker35e41772019-02-05 15:37:23 +00007252 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007253 return( 0 );
7254}
7255
Hanno Beckerb8a08572019-02-05 12:49:06 +00007256#if defined(MBEDTLS_SSL_SRV_C)
7257static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7258{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007259 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007260 return( -1 );
7261
7262#if defined(MBEDTLS_SSL_PROTO_SSL3)
7263 /*
7264 * Check if the client sent an empty certificate
7265 */
Hanno Becker2881d802019-05-22 14:44:53 +01007266 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007267 {
7268 if( ssl->in_msglen == 2 &&
7269 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7270 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7271 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7272 {
7273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7274 return( 0 );
7275 }
7276
7277 return( -1 );
7278 }
7279#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7280
7281#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7282 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7283 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7284 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7285 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007286 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 +00007287 {
7288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7289 return( 0 );
7290 }
7291
Hanno Beckerb8a08572019-02-05 12:49:06 +00007292#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7293 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007294
7295 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007296}
7297#endif /* MBEDTLS_SSL_SRV_C */
7298
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007299/* Check if a certificate message is expected.
7300 * Return either
7301 * - SSL_CERTIFICATE_EXPECTED, or
7302 * - SSL_CERTIFICATE_SKIP
7303 * indicating whether a Certificate message is expected or not.
7304 */
7305#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007306#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007307static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7308 int authmode )
7309{
Hanno Becker473f98f2019-06-26 10:27:32 +01007310 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007311 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007312
7313 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7314 return( SSL_CERTIFICATE_SKIP );
7315
7316#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007317 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007318 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007319 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7320 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7321 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007322 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007323 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007324
7325 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7326 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007327 ssl->session_negotiate->verify_result =
7328 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7329 return( SSL_CERTIFICATE_SKIP );
7330 }
7331 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007332#else
7333 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007334#endif /* MBEDTLS_SSL_SRV_C */
7335
7336 return( SSL_CERTIFICATE_EXPECTED );
7337}
7338
Hanno Becker3cf50612019-02-05 14:36:34 +00007339static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007340 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007341 mbedtls_x509_crt *chain,
7342 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007343{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007344 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007345 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007346 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007347 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007348 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007349 mbedtls_x509_crt *ca_chain;
7350 mbedtls_x509_crl *ca_crl;
7351
7352 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007353 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007354 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007355 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007356
Jarno Lamsae1621d42019-12-19 08:58:56 +02007357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007358#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7359 if( ssl->handshake->sni_ca_chain != NULL )
7360 {
7361 ca_chain = ssl->handshake->sni_ca_chain;
7362 ca_crl = ssl->handshake->sni_ca_crl;
7363 }
7364 else
7365#endif
7366 {
7367 ca_chain = ssl->conf->ca_chain;
7368 ca_crl = ssl->conf->ca_crl;
7369 }
7370
7371 /*
7372 * Main check: verify certificate
7373 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007374 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007375 chain,
7376 ca_chain, ca_crl,
7377 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007378#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007379 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007380#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007381 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007382#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7383 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7384#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7385 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007386
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007387 if( verify_ret == 0 )
7388 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007389 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007390 if( verify_ret == 0 )
7391 {
7392 flow_counter++;
7393 }
7394 else
7395 {
7396 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7397 }
7398 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007399 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007400 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007401 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007402 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007403 }
7404
7405#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007406 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007407 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7408#endif
7409
7410 /*
7411 * Secondary checks: always done, but change 'ret' only if it was 0
7412 */
7413
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007414#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007415 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007416#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007417 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007418#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007419 mbedtls_pk_context *pk;
7420 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7421 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007422 {
7423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007424 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007425 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007426
7427 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007428 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007429 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007430 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007431 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007432
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007433 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007434#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007435
7436 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007437 {
7438 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7439
7440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007441 if( verify_ret == 0 )
7442 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007443 flow_counter++;
7444 }
7445 if( ret == 0 )
7446 {
7447 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007448 }
7449 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007450#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007451
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007452 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007453 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007454 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7455 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007456 &ssl->session_negotiate->verify_result );
7457 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007458 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007459 flow_counter++;
7460 }
7461 else
7462 {
7463 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007465 if( verify_ret == 0 )
7466 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007467 }
7468
7469 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7470 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7471 * with details encoded in the verification flags. All other kinds
7472 * of error codes, including those from the user provided f_vrfy
7473 * functions, are treated as fatal and lead to a failure of
7474 * ssl_parse_certificate even if verification was optional. */
7475 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007476 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7477 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007478 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007479 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007480 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7481 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7482 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7483 {
7484 verify_ret = 0;
7485 flow_counter++;
7486 }
7487 else
7488 {
7489 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7490 }
7491 } else {
7492 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007493 }
7494
7495 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7496 {
7497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007498 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007499 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007500 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007501 else
7502 {
7503 flow_counter++;
7504 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007505
Hanno Becker8c13ee62019-02-26 16:48:17 +00007506 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007507 {
7508 uint8_t alert;
7509
7510 /* The certificate may have been rejected for several reasons.
7511 Pick one and send the corresponding alert. Which alert to send
7512 may be a subject of debate in some cases. */
7513 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7514 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7515 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7516 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7517 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7518 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7519 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7520 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7521 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7522 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7523 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7524 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7525 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7526 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7527 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7528 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7529 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7530 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7531 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7532 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7533 else
7534 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007535 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007536 }
7537
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007538 if( verify_ret == 0 &&
7539#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7540 flow_counter == 5 )
7541#else
7542 flow_counter == 4 )
7543#endif
7544 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007545 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007546 if( verify_ret == 0 &&
7547#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7548 flow_counter == 5 )
7549#else
7550 flow_counter == 4 )
7551#endif
7552 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007554 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7555 }
7556 else
7557 {
7558 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7559 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007560 } else {
7561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007562 }
7563
Hanno Becker3cf50612019-02-05 14:36:34 +00007564#if defined(MBEDTLS_DEBUG_C)
7565 if( ssl->session_negotiate->verify_result != 0 )
7566 {
7567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7568 ssl->session_negotiate->verify_result ) );
7569 }
7570 else
7571 {
7572 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7573 }
7574#endif /* MBEDTLS_DEBUG_C */
7575
Hanno Becker8c13ee62019-02-26 16:48:17 +00007576 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007577}
7578
Hanno Becker34106f62019-02-08 14:59:05 +00007579#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007580
7581#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007582static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7583 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007584{
7585 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007586 /* Remember digest of the peer's end-CRT. */
7587 ssl->session_negotiate->peer_cert_digest =
7588 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7589 if( ssl->session_negotiate->peer_cert_digest == NULL )
7590 {
7591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7592 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007593 mbedtls_ssl_pend_fatal_alert( ssl,
7594 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007595
7596 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7597 }
7598
7599 ret = mbedtls_md( mbedtls_md_info_from_type(
7600 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7601 start, len,
7602 ssl->session_negotiate->peer_cert_digest );
7603
7604 ssl->session_negotiate->peer_cert_digest_type =
7605 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7606 ssl->session_negotiate->peer_cert_digest_len =
7607 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7608
7609 return( ret );
7610}
Hanno Becker5882dd02019-06-06 16:25:57 +01007611#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007612
7613static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7614 unsigned char *start, size_t len )
7615{
7616 unsigned char *end = start + len;
7617 int ret;
7618
7619 /* Make a copy of the peer's raw public key. */
7620 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7621 ret = mbedtls_pk_parse_subpubkey( &start, end,
7622 &ssl->handshake->peer_pubkey );
7623 if( ret != 0 )
7624 {
7625 /* We should have parsed the public key before. */
7626 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7627 }
7628
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007629 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007630 return( 0 );
7631}
7632#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7633
Hanno Becker3cf50612019-02-05 14:36:34 +00007634int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7635{
7636 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007637 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007638#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7639 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7640 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007641 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007642#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007643 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007644#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007645 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007646 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007647
7648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7649
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007650 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7651 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007652 {
7653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007654 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007655 }
7656
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007657#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7658 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007659 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007660 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007661 chain = ssl->handshake->ecrs_peer_cert;
7662 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007663 goto crt_verify;
7664 }
7665#endif
7666
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007667 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007668 {
7669 /* mbedtls_ssl_read_record may have sent an alert already. We
7670 let it decide whether to alert. */
7671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007672 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007673 }
7674
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007675#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007676 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7677 {
7678 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007679
Hanno Beckerb8a08572019-02-05 12:49:06 +00007680 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007681 ret = 0;
7682 else
7683 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007684
Hanno Becker613d4902019-02-05 13:11:17 +00007685 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007686 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007687#endif /* MBEDTLS_SSL_SRV_C */
7688
Hanno Becker35e41772019-02-05 15:37:23 +00007689 /* Clear existing peer CRT structure in case we tried to
7690 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007691 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007692
7693 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7694 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007695 {
7696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7697 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007698 mbedtls_ssl_pend_fatal_alert( ssl,
7699 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007700
Hanno Beckere4aeb762019-02-05 17:19:52 +00007701 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7702 goto exit;
7703 }
7704 mbedtls_x509_crt_init( chain );
7705
7706 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007707 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007708 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007709
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007710#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7711 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007712 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007713
7714crt_verify:
7715 if( ssl->handshake->ecrs_enabled)
7716 rs_ctx = &ssl->handshake->ecrs_ctx;
7717#endif
7718
Hanno Becker3cf50612019-02-05 14:36:34 +00007719 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007720 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007721 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007722 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007723
Hanno Becker3008d282019-02-05 17:02:28 +00007724#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007725 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007726 size_t pk_len;
7727 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007728
Hanno Becker34106f62019-02-08 14:59:05 +00007729 /* We parse the CRT chain without copying, so
7730 * these pointers point into the input buffer,
7731 * and are hence still valid after freeing the
7732 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007733
Hanno Becker5882dd02019-06-06 16:25:57 +01007734#if defined(MBEDTLS_SSL_RENEGOTIATION)
7735 unsigned char *crt_start;
7736 size_t crt_len;
7737
Hanno Becker34106f62019-02-08 14:59:05 +00007738 crt_start = chain->raw.p;
7739 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007740#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007741
Hanno Becker8c13ee62019-02-26 16:48:17 +00007742 pk_start = chain->cache->pk_raw.p;
7743 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007744
7745 /* Free the CRT structures before computing
7746 * digest and copying the peer's public key. */
7747 mbedtls_x509_crt_free( chain );
7748 mbedtls_free( chain );
7749 chain = NULL;
7750
Hanno Becker5882dd02019-06-06 16:25:57 +01007751#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007752 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007753 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007754 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007755#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007756
Hanno Becker34106f62019-02-08 14:59:05 +00007757 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007758 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007759 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007760 }
Hanno Becker34106f62019-02-08 14:59:05 +00007761#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7762 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007763 ssl->session_negotiate->peer_cert = chain;
7764 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007765#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007768
Hanno Becker613d4902019-02-05 13:11:17 +00007769exit:
7770
Hanno Beckere4aeb762019-02-05 17:19:52 +00007771 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007772 {
7773 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7774 {
7775 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7776 }
7777 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7778 {
7779 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7780 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007781 else
7782 {
7783 ssl->state = MBEDTLS_SSL_INVALID;
7784 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007785 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007786
7787#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7788 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7789 {
7790 ssl->handshake->ecrs_peer_cert = chain;
7791 chain = NULL;
7792 }
7793#endif
7794
7795 if( chain != NULL )
7796 {
7797 mbedtls_x509_crt_free( chain );
7798 mbedtls_free( chain );
7799 }
7800
Paul Bakker5121ce52009-01-03 21:22:43 +00007801 return( ret );
7802}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007803#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007806{
7807 int ret;
7808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007812 ssl->out_msglen = 1;
7813 ssl->out_msg[0] = 1;
7814
Jarno Lamsa2b205162019-11-12 15:36:21 +02007815 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7816 {
7817 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7818 }
7819 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7820 {
7821 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7822 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007823 else
7824 {
7825 ssl->state = MBEDTLS_SSL_INVALID;
7826 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007827
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007828 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007831 return( ret );
7832 }
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007835
7836 return( 0 );
7837}
7838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007840{
7841 int ret;
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007844
Hanno Becker327c93b2018-08-15 13:56:18 +01007845 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007848 return( ret );
7849 }
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007854 mbedtls_ssl_pend_fatal_alert( ssl,
7855 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007857 }
7858
Hanno Beckere678eaa2018-08-21 14:57:46 +01007859 /* CCS records are only accepted if they have length 1 and content '1',
7860 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007861
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007862 /*
7863 * Switch to our negotiated transform and session parameters for inbound
7864 * data.
7865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007867 ssl->transform_in = ssl->transform_negotiate;
7868 ssl->session_in = ssl->session_negotiate;
7869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007871 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007874 ssl_dtls_replay_reset( ssl );
7875#endif
7876
7877 /* Increment epoch */
7878 if( ++ssl->in_epoch == 0 )
7879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007881 /* This is highly unlikely to happen for legitimate reasons, so
7882 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007884 }
7885 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007886 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007888#if defined(MBEDTLS_SSL_PROTO_TLS)
7889 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007890 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007891 }
7892#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007893
Hanno Beckerf5970a02019-05-08 09:38:41 +01007894 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7897 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007902 mbedtls_ssl_pend_fatal_alert( ssl,
7903 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007905 }
7906 }
7907#endif
7908
Jarno Lamsa2b205162019-11-12 15:36:21 +02007909 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7910 {
7911 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7912 }
7913 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7914 {
7915 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7916 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007917 else
7918 {
7919 ssl->state = MBEDTLS_SSL_INVALID;
7920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007923
7924 return( 0 );
7925}
7926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007928{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7930 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007931 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007932 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7935#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007936 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007939 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007942}
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007947
7948 /*
7949 * Free our handshake params
7950 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007951 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007953 ssl->handshake = NULL;
7954
7955 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007956 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007957 */
7958 if( ssl->transform )
7959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 mbedtls_ssl_transform_free( ssl->transform );
7961 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007962 }
7963 ssl->transform = ssl->transform_negotiate;
7964 ssl->transform_negotiate = NULL;
7965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007967}
7968
Jarno Lamsae1621d42019-12-19 08:58:56 +02007969int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007970{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007971 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007972
7973#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007974 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007975 ? ssl->handshake->sni_authmode
7976 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7977#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007978 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007979#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007980#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7981 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7982 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986#if defined(MBEDTLS_SSL_RENEGOTIATION)
7987 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007990 ssl->renego_records_seen = 0;
7991 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007992#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007993
7994 /*
7995 * Free the previous session and switch in the current one
7996 */
Paul Bakker0a597072012-09-25 21:55:46 +00007997 if( ssl->session )
7998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01008000 /* RFC 7366 3.1: keep the EtM state */
8001 ssl->session_negotiate->encrypt_then_mac =
8002 ssl->session->encrypt_then_mac;
8003#endif
8004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 mbedtls_ssl_session_free( ssl->session );
8006 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00008007 }
8008 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00008009 ssl->session_negotiate = NULL;
8010
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008011#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00008012 /*
8013 * Add cache entry
8014 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008015 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02008016 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008017 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008018 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008019 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008021 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008022#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008023
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008024 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8025 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8026#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8027 crt_expected == SSL_CERTIFICATE_SKIP )
8028#else
8029 1 )
8030#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008031 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008032 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008033 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8034 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8035#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8036 crt_expected == SSL_CERTIFICATE_SKIP )
8037#else
8038 1 )
8039#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008040 {
8041 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8042 }
8043 else
8044 {
8045 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8046 goto cleanup;
8047 }
8048 }
8049
Jarno Lamsae1621d42019-12-19 08:58:56 +02008050#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008051 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008052 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008053 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008054 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008055 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008056 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008057 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008058 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008059 }
8060 else
8061 {
8062 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008063 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008064 }
8065 }
8066#endif
8067
8068 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8069 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008070 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008071 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8072 {
8073 ret = 0;
8074 }
8075 else
8076 {
8077 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008078 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008079 }
8080 }
8081 else
8082 {
8083 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008084 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008085 }
8086
Jarno Lamsa06164052019-12-19 14:40:36 +02008087 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8088 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8089 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8090 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008091 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008092 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8093 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8094 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8095 {
8096 ret = 0;
8097 }
8098 else
8099 {
8100 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8101 goto cleanup;
8102 }
8103 }
8104 else
8105 {
8106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8109 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8110 }
8111
8112cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008113#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008114 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008115 ssl->handshake->flight != NULL )
8116 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008117 /* Cancel handshake timer */
8118 ssl_set_timer( ssl, 0 );
8119
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008120 /* Keep last flight around in case we need to resend it:
8121 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008123 }
8124 else
8125#endif
8126 ssl_handshake_wrapup_free_hs_transform( ssl );
8127
Jarno Lamsa2b205162019-11-12 15:36:21 +02008128 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008131 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008132}
8133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008134int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008135{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008136 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008139
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008140 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008141
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008142 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8143 mbedtls_ssl_suite_get_mac(
8144 mbedtls_ssl_ciphersuite_from_id(
8145 mbedtls_ssl_session_get_ciphersuite(
8146 ssl->session_negotiate ) ) ),
8147 ssl, ssl->out_msg + 4,
8148 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008149
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008150 /*
8151 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8152 * may define some other value. Currently (early 2016), no defined
8153 * ciphersuite does this (and this is unlikely to change as activity has
8154 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8155 */
Hanno Becker2881d802019-05-22 14:44:53 +01008156 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008159 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008160 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008161#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008162
Paul Bakker5121ce52009-01-03 21:22:43 +00008163 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8165 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008166
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008167#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008168 /*
8169 * In case of session resuming, invert the client and server
8170 * ChangeCipherSpec messages order.
8171 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008172 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008175 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8176 MBEDTLS_SSL_IS_CLIENT )
8177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008179 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008181#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008182 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8183 MBEDTLS_SSL_IS_SERVER )
8184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008186 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008187#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008188 }
8189 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008190#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008191 {
8192 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8193 {
8194 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8195 }
8196 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8197 {
8198 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8199 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008200 else
8201 {
8202 ssl->state = MBEDTLS_SSL_INVALID;
8203 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008204 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008205
Paul Bakker48916f92012-09-16 19:57:18 +00008206 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008207 * Switch to our negotiated transform and session parameters for outbound
8208 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008213 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008214 {
8215 unsigned char i;
8216
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008217 /* Remember current epoch settings for resending */
8218 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008219 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008220
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008221 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008222 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008223
8224 /* Increment epoch */
8225 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008226 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008227 break;
8228
8229 /* The loop goes to its end iff the counter is wrapping */
8230 if( i == 0 )
8231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8233 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008234 }
8235 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008236 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008237#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008238#if defined(MBEDTLS_SSL_PROTO_TLS)
8239 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008240 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008241 }
8242#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008243
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008244 ssl->transform_out = ssl->transform_negotiate;
8245 ssl->session_out = ssl->session_negotiate;
8246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8248 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8253 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008254 }
8255 }
8256#endif
8257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008259 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008261#endif
8262
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008263 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008264 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008266 return( ret );
8267 }
8268
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008269#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008270 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008271 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8272 {
8273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8274 return( ret );
8275 }
8276#endif
8277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008279
8280 return( 0 );
8281}
8282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008284#define SSL_MAX_HASH_LEN 36
8285#else
8286#define SSL_MAX_HASH_LEN 12
8287#endif
8288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008290{
Paul Bakker23986e52011-04-24 08:57:21 +00008291 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008292 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008293 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008296
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008297 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8298 mbedtls_ssl_suite_get_mac(
8299 mbedtls_ssl_ciphersuite_from_id(
8300 mbedtls_ssl_session_get_ciphersuite(
8301 ssl->session_negotiate ) ) ),
8302 ssl, buf,
8303 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008304
Hanno Becker327c93b2018-08-15 13:56:18 +01008305 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008308 return( ret );
8309 }
8310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008314 mbedtls_ssl_pend_fatal_alert( ssl,
8315 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008317 }
8318
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008319 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008321 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008322 hash_len = 36;
8323 else
8324#endif
8325 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8328 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008331 mbedtls_ssl_pend_fatal_alert( ssl,
8332 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008334 }
8335
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008336 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008337 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008340 mbedtls_ssl_pend_fatal_alert( ssl,
8341 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008343 }
8344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008346 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008347 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008348#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008349
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008350#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008351 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008354 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008356#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008357#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008358 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008360#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008361 }
8362 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008363#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008364 {
8365 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8366 {
8367 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8368 }
8369 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8370 {
8371 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8372 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008373 else
8374 {
8375 ssl->state = MBEDTLS_SSL_INVALID;
8376 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008380 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008381 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008382#endif
8383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008385
8386 return( 0 );
8387}
8388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008389static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008390{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8394 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8395 mbedtls_md5_init( &handshake->fin_md5 );
8396 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008397 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8398 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8401#if defined(MBEDTLS_SHA256_C)
8402 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008403 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008405#if defined(MBEDTLS_SHA512_C)
8406 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008407 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008408#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008410
Hanno Becker7e5437a2017-04-28 17:15:26 +01008411#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8412 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8413 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8414#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416#if defined(MBEDTLS_DHM_C)
8417 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419#if defined(MBEDTLS_ECDH_C)
8420 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008421#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008422#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008423 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008424#if defined(MBEDTLS_SSL_CLI_C)
8425 handshake->ecjpake_cache = NULL;
8426 handshake->ecjpake_cache_len = 0;
8427#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008428#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008429
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008430#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008431 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008432#endif
8433
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008434#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8435 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8436#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008437
8438#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8439 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8440 mbedtls_pk_init( &handshake->peer_pubkey );
8441#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008442}
8443
Hanno Becker611a83b2018-01-03 14:27:32 +00008444void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008445{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8449 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008450
Hanno Becker92231322018-01-03 15:32:51 +00008451#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452 mbedtls_md_init( &transform->md_ctx_enc );
8453 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008454#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008455}
8456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008458{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008459 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008460}
8461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008462static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008463{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008464 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008465 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008467 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008469 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008470 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008471
8472 /*
8473 * Either the pointers are now NULL or cleared properly and can be freed.
8474 * Now allocate missing structures.
8475 */
8476 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008477 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008478 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008479 }
Paul Bakker48916f92012-09-16 19:57:18 +00008480
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008481 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008482 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008483 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008484 }
Paul Bakker48916f92012-09-16 19:57:18 +00008485
Paul Bakker82788fb2014-10-20 13:59:19 +02008486 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008487 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008488 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008489 }
Paul Bakker48916f92012-09-16 19:57:18 +00008490
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008491 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008492 if( ssl->handshake == NULL ||
8493 ssl->transform_negotiate == NULL ||
8494 ssl->session_negotiate == NULL )
8495 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008498 mbedtls_free( ssl->handshake );
8499 mbedtls_free( ssl->transform_negotiate );
8500 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008501
8502 ssl->handshake = NULL;
8503 ssl->transform_negotiate = NULL;
8504 ssl->session_negotiate = NULL;
8505
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008506 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008507 }
8508
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008509 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008511 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008512 ssl_handshake_params_init( ssl->handshake );
8513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008515 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008516 {
8517 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008518
Hanno Becker2d9623f2019-06-13 12:07:05 +01008519 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008520 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8521 else
8522 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8523 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008524#endif
8525
Paul Bakker48916f92012-09-16 19:57:18 +00008526 return( 0 );
8527}
8528
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008529#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008530/* Dummy cookie callbacks for defaults */
8531static int ssl_cookie_write_dummy( void *ctx,
8532 unsigned char **p, unsigned char *end,
8533 const unsigned char *cli_id, size_t cli_id_len )
8534{
8535 ((void) ctx);
8536 ((void) p);
8537 ((void) end);
8538 ((void) cli_id);
8539 ((void) cli_id_len);
8540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008541 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008542}
8543
8544static int ssl_cookie_check_dummy( void *ctx,
8545 const unsigned char *cookie, size_t cookie_len,
8546 const unsigned char *cli_id, size_t cli_id_len )
8547{
8548 ((void) ctx);
8549 ((void) cookie);
8550 ((void) cookie_len);
8551 ((void) cli_id);
8552 ((void) cli_id_len);
8553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008555}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008556#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008557
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008558/* Once ssl->out_hdr as the address of the beginning of the
8559 * next outgoing record is set, deduce the other pointers.
8560 *
8561 * Note: For TLS, we save the implicit record sequence number
8562 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8563 * and the caller has to make sure there's space for this.
8564 */
8565
8566static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8567 mbedtls_ssl_transform *transform )
8568{
8569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008570 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008571 {
8572 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008573#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008574 ssl->out_cid = ssl->out_ctr + 8;
8575 ssl->out_len = ssl->out_cid;
8576 if( transform != NULL )
8577 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008578#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008579 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008580#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008581 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008582 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008583 MBEDTLS_SSL_TRANSPORT_ELSE
8584#endif /* MBEDTLS_SSL_PROTO_DTLS */
8585#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008586 {
8587 ssl->out_ctr = ssl->out_hdr - 8;
8588 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008589#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008590 ssl->out_cid = ssl->out_len;
8591#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008592 ssl->out_iv = ssl->out_hdr + 5;
8593 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008594#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008595
8596 /* Adjust out_msg to make space for explicit IV, if used. */
8597 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008598 mbedtls_ssl_ver_geq(
8599 mbedtls_ssl_get_minor_ver( ssl ),
8600 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008601 {
8602 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8603 }
8604 else
8605 ssl->out_msg = ssl->out_iv;
8606}
8607
8608/* Once ssl->in_hdr as the address of the beginning of the
8609 * next incoming record is set, deduce the other pointers.
8610 *
8611 * Note: For TLS, we save the implicit record sequence number
8612 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8613 * and the caller has to make sure there's space for this.
8614 */
8615
Hanno Beckerf5970a02019-05-08 09:38:41 +01008616static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008617{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008618 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008619 * of unprotected TLS/DTLS records, with ssl->in_msg
8620 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008621 *
8622 * When decrypting a protected record, ssl->in_msg
8623 * will be shifted to point to the beginning of the
8624 * record plaintext.
8625 */
8626
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008627#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008628 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008629 {
Hanno Becker70e79282019-05-03 14:34:53 +01008630 /* This sets the header pointers to match records
8631 * without CID. When we receive a record containing
8632 * a CID, the fields are shifted accordingly in
8633 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008634 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008635#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008636 ssl->in_cid = ssl->in_ctr + 8;
8637 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008638#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008639 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008640#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008641 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008642 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008643 MBEDTLS_SSL_TRANSPORT_ELSE
8644#endif /* MBEDTLS_SSL_PROTO_DTLS */
8645#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008646 {
8647 ssl->in_ctr = ssl->in_hdr - 8;
8648 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008649#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008650 ssl->in_cid = ssl->in_len;
8651#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008652 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008653 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008654#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008655}
8656
Paul Bakker5121ce52009-01-03 21:22:43 +00008657/*
8658 * Initialize an SSL context
8659 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008660void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8661{
8662 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8663}
8664
8665/*
8666 * Setup an SSL context
8667 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008668
8669static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8670{
8671 /* Set the incoming and outgoing record pointers. */
8672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008673 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008674 {
8675 ssl->out_hdr = ssl->out_buf;
8676 ssl->in_hdr = ssl->in_buf;
8677 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008678 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008679#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008680#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008681 {
8682 ssl->out_hdr = ssl->out_buf + 8;
8683 ssl->in_hdr = ssl->in_buf + 8;
8684 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008685#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008686
8687 /* Derive other internal pointers. */
8688 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008689 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008690}
8691
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008692int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008693 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008694{
Paul Bakker48916f92012-09-16 19:57:18 +00008695 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008696
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008697 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008698
Hanno Beckeref982d52019-07-23 15:56:18 +01008699#if defined(MBEDTLS_USE_TINYCRYPT)
8700 uECC_set_rng( &uecc_rng_wrapper );
8701#endif
8702
Paul Bakker62f2dee2012-09-28 07:31:51 +00008703 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008704 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008705 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008706
8707 /* Set to NULL in case of an error condition */
8708 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008709
Angus Grattond8213d02016-05-25 20:56:48 +10008710 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8711 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008712 {
Angus Grattond8213d02016-05-25 20:56:48 +10008713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008714 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008715 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008716 }
8717
8718 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8719 if( ssl->out_buf == NULL )
8720 {
8721 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008722 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008723 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008724 }
8725
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008726 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008727
Paul Bakker48916f92012-09-16 19:57:18 +00008728 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008729 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008730
Hanno Beckerc8f52992019-07-25 11:15:08 +01008731 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008732
Paul Bakker5121ce52009-01-03 21:22:43 +00008733 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008734
8735error:
8736 mbedtls_free( ssl->in_buf );
8737 mbedtls_free( ssl->out_buf );
8738
8739 ssl->conf = NULL;
8740
8741 ssl->in_buf = NULL;
8742 ssl->out_buf = NULL;
8743
8744 ssl->in_hdr = NULL;
8745 ssl->in_ctr = NULL;
8746 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008747 ssl->in_msg = NULL;
8748
8749 ssl->out_hdr = NULL;
8750 ssl->out_ctr = NULL;
8751 ssl->out_len = NULL;
8752 ssl->out_iv = NULL;
8753 ssl->out_msg = NULL;
8754
k-stachowiak9f7798e2018-07-31 16:52:32 +02008755 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008756}
8757
8758/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008759 * Reset an initialized and used SSL context for re-use while retaining
8760 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008761 *
8762 * If partial is non-zero, keep data in the input buffer and client ID.
8763 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008764 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008765static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008766{
Paul Bakker48916f92012-09-16 19:57:18 +00008767 int ret;
8768
Hanno Becker7e772132018-08-10 12:38:21 +01008769#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8770 !defined(MBEDTLS_SSL_SRV_C)
8771 ((void) partial);
8772#endif
8773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008774 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008775
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008776 /* Cancel any possibly running timer */
8777 ssl_set_timer( ssl, 0 );
8778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779#if defined(MBEDTLS_SSL_RENEGOTIATION)
8780 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008781 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008782
8783 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008784 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8785 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008787 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008788
Paul Bakker7eb013f2011-10-06 12:37:39 +00008789 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008790 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008791
8792 ssl->in_msgtype = 0;
8793 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008794#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008795 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008796 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008797#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008798#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008799 ssl_dtls_replay_reset( ssl );
8800#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008801
8802 ssl->in_hslen = 0;
8803 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008804
8805 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008806
8807 ssl->out_msgtype = 0;
8808 ssl->out_msglen = 0;
8809 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008810#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8811 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008812 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008813#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008814
Hanno Becker19859472018-08-06 09:40:20 +01008815 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8816
Paul Bakker48916f92012-09-16 19:57:18 +00008817 ssl->transform_in = NULL;
8818 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008819
Hanno Becker78640902018-08-13 16:35:15 +01008820 ssl->session_in = NULL;
8821 ssl->session_out = NULL;
8822
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008823 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008824
8825#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008826 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008827#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8828 {
8829 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008830 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008831 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8834 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8837 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8840 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008841 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008842 }
8843#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008844
Paul Bakker48916f92012-09-16 19:57:18 +00008845 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 mbedtls_ssl_transform_free( ssl->transform );
8848 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008849 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008850 }
Paul Bakker48916f92012-09-16 19:57:18 +00008851
Paul Bakkerc0463502013-02-14 11:19:38 +01008852 if( ssl->session )
8853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854 mbedtls_ssl_session_free( ssl->session );
8855 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008856 ssl->session = NULL;
8857 }
8858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008860 ssl->alpn_chosen = NULL;
8861#endif
8862
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008863#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008864#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008865 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008866#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008867 {
8868 mbedtls_free( ssl->cli_id );
8869 ssl->cli_id = NULL;
8870 ssl->cli_id_len = 0;
8871 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008872#endif
8873
Paul Bakker48916f92012-09-16 19:57:18 +00008874 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8875 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008876
8877 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008878}
8879
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008880/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008881 * Reset an initialized and used SSL context for re-use while retaining
8882 * all application-set variables, function pointers and data.
8883 */
8884int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8885{
8886 return( ssl_session_reset_int( ssl, 0 ) );
8887}
8888
8889/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008890 * SSL set accessors
8891 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008892#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008893void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008894{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008895 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008896}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008897#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008898
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008899void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008900{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008901 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008902}
8903
Hanno Becker7f376f42019-06-12 16:20:48 +01008904#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8905 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008906void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008907{
Hanno Becker7f376f42019-06-12 16:20:48 +01008908 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008909}
Hanno Becker7f376f42019-06-12 16:20:48 +01008910#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008911
Hanno Beckerde671542019-06-12 16:30:46 +01008912#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8913 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8914void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8915 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008916{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008917 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008918}
Hanno Beckerde671542019-06-12 16:30:46 +01008919#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008921#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008922
Hanno Becker1841b0a2018-08-24 11:13:57 +01008923void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8924 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008925{
8926 ssl->disable_datagram_packing = !allow_packing;
8927}
8928
Hanno Becker1f835fa2019-06-13 10:14:59 +01008929#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8930 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008931void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8932 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008933{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008934 conf->hs_timeout_min = min;
8935 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008936}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008937#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8938 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8939void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8940 uint32_t min, uint32_t max )
8941{
8942 ((void) conf);
8943 ((void) min);
8944 ((void) max);
8945}
8946#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8947 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8948
8949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008950
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008951void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008952{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008953#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8954 conf->authmode = authmode;
8955#else
8956 ((void) conf);
8957 ((void) authmode);
8958#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008959}
8960
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008961#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8962 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008963void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008964 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008965 void *p_vrfy )
8966{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008967 conf->f_vrfy = f_vrfy;
8968 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008969}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008970#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008971
Hanno Beckerece325c2019-06-13 15:39:27 +01008972#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008973void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008974 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008975 void *p_rng )
8976{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008977 conf->f_rng = f_rng;
8978 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008979}
Hanno Beckerece325c2019-06-13 15:39:27 +01008980#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008981
Hanno Becker14a4a442019-07-02 17:00:34 +01008982#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008983void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008984 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008985 void *p_dbg )
8986{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008987 conf->f_dbg = f_dbg;
8988 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008989}
Hanno Becker14a4a442019-07-02 17:00:34 +01008990#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008991
Hanno Beckera58a8962019-06-13 16:11:15 +01008992#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8993 !defined(MBEDTLS_SSL_CONF_SEND) && \
8994 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008996 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008997 mbedtls_ssl_send_t *f_send,
8998 mbedtls_ssl_recv_t *f_recv,
8999 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009000{
Hanno Beckera58a8962019-06-13 16:11:15 +01009001 ssl->p_bio = p_bio;
9002 ssl->f_send = f_send;
9003 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009004 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009005}
Hanno Beckera58a8962019-06-13 16:11:15 +01009006#else
9007void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
9008 void *p_bio )
9009{
9010 ssl->p_bio = p_bio;
9011}
9012#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009013
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02009014#if defined(MBEDTLS_SSL_PROTO_DTLS)
9015void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
9016{
9017 ssl->mtu = mtu;
9018}
9019#endif
9020
Hanno Becker1f835fa2019-06-13 10:14:59 +01009021#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009022void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009023{
9024 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009025}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009026#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009027
Hanno Becker0ae6b242019-06-13 16:45:36 +01009028#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9029 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009030void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9031 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009032 mbedtls_ssl_set_timer_t *f_set_timer,
9033 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009034{
9035 ssl->p_timer = p_timer;
9036 ssl->f_set_timer = f_set_timer;
9037 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009038 /* Make sure we start with no timer running */
9039 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009040}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009041#else
9042void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9043 void *p_timer )
9044{
9045 ssl->p_timer = p_timer;
9046 /* Make sure we start with no timer running */
9047 ssl_set_timer( ssl, 0 );
9048}
9049#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009050
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009051#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009052void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009053 void *p_cache,
9054 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9055 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009056{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009057 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009058 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009059 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009060}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009061#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009062
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009063#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009064int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009065{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009066 int ret;
9067
9068 if( ssl == NULL ||
9069 session == NULL ||
9070 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009071 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009074 }
9075
Hanno Becker58fccf22019-02-06 14:30:46 +00009076 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9077 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009078 return( ret );
9079
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009080 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009081
9082 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009083}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009084#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009085
Hanno Becker73f4cb12019-06-27 13:51:07 +01009086#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009087void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009088 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009089{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009090 conf->ciphersuite_list[0] = ciphersuites;
9091 conf->ciphersuite_list[1] = ciphersuites;
9092 conf->ciphersuite_list[2] = ciphersuites;
9093 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009094}
9095
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009096void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009097 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009098 int major, int minor )
9099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009101 return;
9102
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009103 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9104 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9105 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009106 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009107 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009108
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009109 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9110 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009111}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009112#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009115void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009116 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009117{
9118 conf->cert_profile = profile;
9119}
9120
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009121/* Append a new keycert entry to a (possibly empty) list */
9122static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9123 mbedtls_x509_crt *cert,
9124 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009125{
niisato8ee24222018-06-25 19:05:48 +09009126 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009127
niisato8ee24222018-06-25 19:05:48 +09009128 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9129 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009130 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009131
niisato8ee24222018-06-25 19:05:48 +09009132 new_cert->cert = cert;
9133 new_cert->key = key;
9134 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009135
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009136 /* Update head is the list was null, else add to the end */
9137 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009138 {
niisato8ee24222018-06-25 19:05:48 +09009139 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009140 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009141 else
9142 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009143 mbedtls_ssl_key_cert *cur = *head;
9144 while( cur->next != NULL )
9145 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009146 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009147 }
9148
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009149 return( 0 );
9150}
9151
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009152int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009153 mbedtls_x509_crt *own_cert,
9154 mbedtls_pk_context *pk_key )
9155{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009156 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009157}
9158
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009159void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009160 mbedtls_x509_crt *ca_chain,
9161 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009162{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009163 conf->ca_chain = ca_chain;
9164 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009166#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009167
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009168#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9169int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9170 mbedtls_x509_crt *own_cert,
9171 mbedtls_pk_context *pk_key )
9172{
9173 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9174 own_cert, pk_key ) );
9175}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009176
9177void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9178 mbedtls_x509_crt *ca_chain,
9179 mbedtls_x509_crl *ca_crl )
9180{
9181 ssl->handshake->sni_ca_chain = ca_chain;
9182 ssl->handshake->sni_ca_crl = ca_crl;
9183}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009184
9185void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9186 int authmode )
9187{
9188 ssl->handshake->sni_authmode = authmode;
9189}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009190#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9191
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009192#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009193/*
9194 * Set EC J-PAKE password for current handshake
9195 */
9196int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9197 const unsigned char *pw,
9198 size_t pw_len )
9199{
9200 mbedtls_ecjpake_role role;
9201
Janos Follath8eb64132016-06-03 15:40:57 +01009202 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9204
Hanno Becker2d9623f2019-06-13 12:07:05 +01009205 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009206 role = MBEDTLS_ECJPAKE_SERVER;
9207 else
9208 role = MBEDTLS_ECJPAKE_CLIENT;
9209
9210 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9211 role,
9212 MBEDTLS_MD_SHA256,
9213 MBEDTLS_ECP_DP_SECP256R1,
9214 pw, pw_len ) );
9215}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009216#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009218#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009219int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009220 const unsigned char *psk, size_t psk_len,
9221 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009222{
Paul Bakker6db455e2013-09-18 17:29:31 +02009223 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009226 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009228
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009229 /* Identity len will be encoded on two bytes */
9230 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009231 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009232 {
9233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9234 }
9235
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009236 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009237 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009238 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009239
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009240 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009241 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009242 conf->psk_len = 0;
9243 }
9244 if( conf->psk_identity != NULL )
9245 {
9246 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009247 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009248 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009249 }
9250
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009251 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9252 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009253 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009254 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009255 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009256 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009257 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009258 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009259 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009260
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009261 conf->psk_len = psk_len;
9262 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009263
Teppo Järvelin91d79382019-10-02 09:09:31 +03009264 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9265 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009266
9267 return( 0 );
9268}
9269
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009270int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9271 const unsigned char *psk, size_t psk_len )
9272{
9273 if( psk == NULL || ssl->handshake == NULL )
9274 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9275
9276 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9278
9279 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009280 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009281 mbedtls_platform_zeroize( ssl->handshake->psk,
9282 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009283 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009284 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009285 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009286
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009287 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009288 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009289
9290 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009291 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009292
9293 return( 0 );
9294}
9295
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009296void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009298 size_t),
9299 void *p_psk )
9300{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009301 conf->f_psk = f_psk;
9302 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009303}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009305
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009306#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009307
9308#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009309int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009310{
9311 int ret;
9312
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009313 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9314 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9315 {
9316 mbedtls_mpi_free( &conf->dhm_P );
9317 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009318 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009320
9321 return( 0 );
9322}
Hanno Becker470a8c42017-10-04 15:28:46 +01009323#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009324
Hanno Beckera90658f2017-10-04 15:29:08 +01009325int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9326 const unsigned char *dhm_P, size_t P_len,
9327 const unsigned char *dhm_G, size_t G_len )
9328{
9329 int ret;
9330
9331 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9332 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9333 {
9334 mbedtls_mpi_free( &conf->dhm_P );
9335 mbedtls_mpi_free( &conf->dhm_G );
9336 return( ret );
9337 }
9338
9339 return( 0 );
9340}
Paul Bakker5121ce52009-01-03 21:22:43 +00009341
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009342int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009343{
9344 int ret;
9345
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009346 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9347 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9348 {
9349 mbedtls_mpi_free( &conf->dhm_P );
9350 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009351 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009352 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009353
9354 return( 0 );
9355}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009356#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009357
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009358#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9359/*
9360 * Set the minimum length for Diffie-Hellman parameters
9361 */
9362void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9363 unsigned int bitlen )
9364{
9365 conf->dhm_min_bitlen = bitlen;
9366}
9367#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9368
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009369#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009370/*
9371 * Set allowed/preferred hashes for handshake signatures
9372 */
9373void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9374 const int *hashes )
9375{
Hanno Becker56595f42019-06-19 16:31:38 +01009376#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009377 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009378#else
9379 ((void) conf);
9380 ((void) hashes);
9381#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009382}
Hanno Becker947194e2017-04-07 13:25:49 +01009383#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009384
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009385#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009386#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009387/*
9388 * Set the allowed elliptic curves
9389 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009390void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009391 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009392{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009393 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009394}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009395#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009396#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009397
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009398#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009400{
Hanno Becker947194e2017-04-07 13:25:49 +01009401 /* Initialize to suppress unnecessary compiler warning */
9402 size_t hostname_len = 0;
9403
9404 /* Check if new hostname is valid before
9405 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009406 if( hostname != NULL )
9407 {
9408 hostname_len = strlen( hostname );
9409
9410 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9412 }
9413
9414 /* Now it's clear that we will overwrite the old hostname,
9415 * so we can free it safely */
9416
9417 if( ssl->hostname != NULL )
9418 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009419 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009420 mbedtls_free( ssl->hostname );
9421 }
9422
9423 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009424
Paul Bakker5121ce52009-01-03 21:22:43 +00009425 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009426 {
9427 ssl->hostname = NULL;
9428 }
9429 else
9430 {
9431 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009432 if( ssl->hostname == NULL )
9433 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009434
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009435 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9436 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009437
Hanno Becker947194e2017-04-07 13:25:49 +01009438 ssl->hostname[hostname_len] = '\0';
9439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009440
9441 return( 0 );
9442}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009443#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009444
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009445#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009446void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009447 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009448 const unsigned char *, size_t),
9449 void *p_sni )
9450{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009451 conf->f_sni = f_sni;
9452 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009454#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009457int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009458{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009459 size_t cur_len, tot_len;
9460 const char **p;
9461
9462 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009463 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9464 * MUST NOT be truncated."
9465 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009466 */
9467 tot_len = 0;
9468 for( p = protos; *p != NULL; p++ )
9469 {
9470 cur_len = strlen( *p );
9471 tot_len += cur_len;
9472
9473 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009474 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009475 }
9476
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009477 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009478
9479 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009480}
9481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009483{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009484 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009487
Hanno Becker33b9b252019-07-05 11:23:25 +01009488#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9489 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9490void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9491 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009492{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009493 conf->max_major_ver = major;
9494 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009495}
Hanno Becker33b9b252019-07-05 11:23:25 +01009496#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9497 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009498
Hanno Becker33b9b252019-07-05 11:23:25 +01009499#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9500 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9501void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9502 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009503{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009504 conf->min_major_ver = major;
9505 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009506}
Hanno Becker33b9b252019-07-05 11:23:25 +01009507#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9508 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009511void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009512{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009513 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009514}
9515#endif
9516
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009517#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009518void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9519 char cert_req_ca_list )
9520{
9521 conf->cert_req_ca_list = cert_req_ca_list;
9522}
9523#endif
9524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009526void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009527{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009528 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009529}
9530#endif
9531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009533#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009534void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009535{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009536 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009537}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009538#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9539#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009540void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009541 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009542{
9543 conf->enforce_extended_master_secret = ems_enf;
9544}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009545#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009546#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009547
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009548#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009549void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009551 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009552}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009553#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009556int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009559 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009562 }
9563
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009564 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009565
9566 return( 0 );
9567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009571void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009573 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009578void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009579{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009580 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009581}
9582#endif
9583
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009584#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009585void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009586{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009587 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009588}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009589#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009592void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009593{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009594 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009595}
9596
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009597void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009598{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009599 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009600}
9601
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009602void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009603 const unsigned char period[8] )
9604{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009605 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009610#if defined(MBEDTLS_SSL_CLI_C)
9611void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009612{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009613 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009614}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009615#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009616
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009617#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009618void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9619 mbedtls_ssl_ticket_write_t *f_ticket_write,
9620 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9621 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009622{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009623 conf->f_ticket_write = f_ticket_write;
9624 conf->f_ticket_parse = f_ticket_parse;
9625 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009626}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009627#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009629
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009630#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9631void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9632 mbedtls_ssl_export_keys_t *f_export_keys,
9633 void *p_export_keys )
9634{
9635 conf->f_export_keys = f_export_keys;
9636 conf->p_export_keys = p_export_keys;
9637}
9638#endif
9639
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009640#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009641void mbedtls_ssl_conf_async_private_cb(
9642 mbedtls_ssl_config *conf,
9643 mbedtls_ssl_async_sign_t *f_async_sign,
9644 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9645 mbedtls_ssl_async_resume_t *f_async_resume,
9646 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009647 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009648{
9649 conf->f_async_sign_start = f_async_sign;
9650 conf->f_async_decrypt_start = f_async_decrypt;
9651 conf->f_async_resume = f_async_resume;
9652 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009653 conf->p_async_config_data = async_config_data;
9654}
9655
Gilles Peskine8f97af72018-04-26 11:46:10 +02009656void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9657{
9658 return( conf->p_async_config_data );
9659}
9660
Gilles Peskine1febfef2018-04-30 11:54:39 +02009661void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009662{
9663 if( ssl->handshake == NULL )
9664 return( NULL );
9665 else
9666 return( ssl->handshake->user_async_ctx );
9667}
9668
Gilles Peskine1febfef2018-04-30 11:54:39 +02009669void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009670 void *ctx )
9671{
9672 if( ssl->handshake != NULL )
9673 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009674}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009675#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009676
Paul Bakker5121ce52009-01-03 21:22:43 +00009677/*
9678 * SSL get accessors
9679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009681{
9682 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9683}
9684
Hanno Becker8b170a02017-10-10 11:51:19 +01009685int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9686{
9687 /*
9688 * Case A: We're currently holding back
9689 * a message for further processing.
9690 */
9691
9692 if( ssl->keep_current_message == 1 )
9693 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009695 return( 1 );
9696 }
9697
9698 /*
9699 * Case B: Further records are pending in the current datagram.
9700 */
9701
9702#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009703 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009704 ssl->in_left > ssl->next_record_offset )
9705 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009707 return( 1 );
9708 }
9709#endif /* MBEDTLS_SSL_PROTO_DTLS */
9710
9711 /*
9712 * Case C: A handshake message is being processed.
9713 */
9714
Hanno Becker8b170a02017-10-10 11:51:19 +01009715 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9716 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009718 return( 1 );
9719 }
9720
9721 /*
9722 * Case D: An application data message is being processed
9723 */
9724 if( ssl->in_offt != NULL )
9725 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009726 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009727 return( 1 );
9728 }
9729
9730 /*
9731 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009732 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009733 * we implement support for multiple alerts in single records.
9734 */
9735
9736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9737 return( 0 );
9738}
9739
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009740uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009741{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009742 if( ssl->session != NULL )
9743 return( ssl->session->verify_result );
9744
9745 if( ssl->session_negotiate != NULL )
9746 return( ssl->session_negotiate->verify_result );
9747
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009748 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009749}
9750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009752{
Hanno Beckere02758c2019-06-26 15:31:31 +01009753 int suite;
9754
Paul Bakker926c8e42013-03-06 10:23:34 +01009755 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009756 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009757
Hanno Beckere02758c2019-06-26 15:31:31 +01009758 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9759 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009760}
9761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009763{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009765 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009766 {
Hanno Becker2881d802019-05-22 14:44:53 +01009767 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009770 return( "DTLSv1.0" );
9771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009773 return( "DTLSv1.2" );
9774
9775 default:
9776 return( "unknown (DTLS)" );
9777 }
9778 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009779 MBEDTLS_SSL_TRANSPORT_ELSE
9780#endif /* MBEDTLS_SSL_PROTO_DTLS */
9781#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009782 {
Hanno Becker2881d802019-05-22 14:44:53 +01009783 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009784 {
9785 case MBEDTLS_SSL_MINOR_VERSION_0:
9786 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009787
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009788 case MBEDTLS_SSL_MINOR_VERSION_1:
9789 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009790
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009791 case MBEDTLS_SSL_MINOR_VERSION_2:
9792 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009793
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009794 case MBEDTLS_SSL_MINOR_VERSION_3:
9795 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009796
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009797 default:
9798 return( "unknown" );
9799 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009800 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009801#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009802}
9803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009804int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009805{
Hanno Becker3136ede2018-08-17 15:28:19 +01009806 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009808
Hanno Becker43395762019-05-03 14:46:38 +01009809 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9810
Hanno Becker78640902018-08-13 16:35:15 +01009811 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009812 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814#if defined(MBEDTLS_ZLIB_SUPPORT)
9815 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9816 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009817#endif
9818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009820 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009821#if defined(MBEDTLS_GCM_C) || \
9822 defined(MBEDTLS_CCM_C) || \
9823 defined(MBEDTLS_CHACHAPOLY_C)
9824#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009826#endif
9827#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009829#endif
9830#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009831 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009832#endif
9833 transform_expansion =
9834 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009835 break;
9836
Hanno Beckera9d5c452019-07-25 16:47:12 +01009837#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9838 MBEDTLS_CHACHAPOLY_C */
9839
9840#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9841 case MBEDTLS_MODE_STREAM:
9842 transform_expansion = transform->maclen;
9843 break;
9844#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9845
9846#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009848 {
9849 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009850
9851 block_size = mbedtls_cipher_get_block_size(
9852 &transform->cipher_ctx_enc );
9853
Hanno Becker3136ede2018-08-17 15:28:19 +01009854 /* Expansion due to the addition of the MAC. */
9855 transform_expansion += transform->maclen;
9856
9857 /* Expansion due to the addition of CBC padding;
9858 * Theoretically up to 256 bytes, but we never use
9859 * more than the block size of the underlying cipher. */
9860 transform_expansion += block_size;
9861
9862 /* For TLS 1.1 or higher, an explicit IV is added
9863 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009864#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009865 if( mbedtls_ssl_ver_geq(
9866 mbedtls_ssl_get_minor_ver( ssl ),
9867 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9868 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009869 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009870 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009871#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009872
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009873 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009874 }
9875#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009876
9877 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009880 }
9881
Hanno Beckera5a2b082019-05-15 14:03:01 +01009882#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009883 if( transform->out_cid_len != 0 )
9884 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009885#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009886
Hanno Becker43395762019-05-03 14:46:38 +01009887 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009888}
9889
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009890#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9891size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9892{
9893 size_t max_len;
9894
9895 /*
9896 * Assume mfl_code is correct since it was checked when set
9897 */
Angus Grattond8213d02016-05-25 20:56:48 +10009898 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009899
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009900 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009901 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009902 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009903 {
Angus Grattond8213d02016-05-25 20:56:48 +10009904 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009905 }
9906
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009907 /* During a handshake, use the value being negotiated */
9908 if( ssl->session_negotiate != NULL &&
9909 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9910 {
9911 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9912 }
9913
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009914 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009915}
9916#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9917
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009918#if defined(MBEDTLS_SSL_PROTO_DTLS)
9919static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9920{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009921 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009922 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009923 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9924 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009925 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009926
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009927 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9928 return( ssl->mtu );
9929
9930 if( ssl->mtu == 0 )
9931 return( ssl->handshake->mtu );
9932
9933 return( ssl->mtu < ssl->handshake->mtu ?
9934 ssl->mtu : ssl->handshake->mtu );
9935}
9936#endif /* MBEDTLS_SSL_PROTO_DTLS */
9937
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009938int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9939{
9940 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9941
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009942#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9943 !defined(MBEDTLS_SSL_PROTO_DTLS)
9944 (void) ssl;
9945#endif
9946
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009947#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9948 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9949
9950 if( max_len > mfl )
9951 max_len = mfl;
9952#endif
9953
9954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009955 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009956 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009957 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009958 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9959 const size_t overhead = (size_t) ret;
9960
9961 if( ret < 0 )
9962 return( ret );
9963
9964 if( mtu <= overhead )
9965 {
9966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9967 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9968 }
9969
9970 if( max_len > mtu - overhead )
9971 max_len = mtu - overhead;
9972 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009973#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009974
Hanno Becker0defedb2018-08-10 12:35:02 +01009975#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9976 !defined(MBEDTLS_SSL_PROTO_DTLS)
9977 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009978#endif
9979
9980 return( (int) max_len );
9981}
9982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983#if defined(MBEDTLS_X509_CRT_PARSE_C)
9984const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009985{
9986 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009987 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009988
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009989#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009990 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009991#else
9992 return( NULL );
9993#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009994}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009998int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9999 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010000{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010001 if( ssl == NULL ||
10002 dst == NULL ||
10003 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +010010004 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010007 }
10008
Hanno Becker58fccf22019-02-06 14:30:46 +000010009 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010012
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +020010013const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
10014{
10015 if( ssl == NULL )
10016 return( NULL );
10017
10018 return( ssl->session );
10019}
10020
Paul Bakker5121ce52009-01-03 21:22:43 +000010021/*
Hanno Beckerb5352f02019-05-16 12:39:07 +010010022 * Define ticket header determining Mbed TLS version
10023 * and structure of the ticket.
10024 */
10025
Hanno Becker41527622019-05-16 12:50:45 +010010026/*
Hanno Becker26829e92019-05-28 14:30:45 +010010027 * Define bitflag determining compile-time settings influencing
10028 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010029 */
10030
Hanno Becker26829e92019-05-28 14:30:45 +010010031#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010032#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010033#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010034#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010035#endif /* MBEDTLS_HAVE_TIME */
10036
10037#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010038#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010039#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010040#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010041#endif /* MBEDTLS_X509_CRT_PARSE_C */
10042
10043#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010044#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010045#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010046#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010047#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10048
10049#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010050#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010051#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010052#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010053#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10054
10055#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010056#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010057#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010058#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010059#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10060
10061#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010062#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010063#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010064#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010065#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10066
Hanno Becker41527622019-05-16 12:50:45 +010010067#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10068#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10069#else
10070#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10071#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10072
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010073#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10074#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10075#else
10076#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10077#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10078
Hanno Becker88440552019-07-03 14:16:13 +010010079#if defined(MBEDTLS_ZLIB_SUPPORT)
10080#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10081#else
10082#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10083#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10084
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010085#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10086#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10087#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10088#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10089#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10090#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10091#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010092#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010093#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010094
Hanno Becker26829e92019-05-28 14:30:45 +010010095#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010096 ( (uint16_t) ( \
10097 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10098 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10099 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10100 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10101 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10102 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010103 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010104 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010105 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010106
Hanno Becker557fe9f2019-05-16 12:41:07 +010010107static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010108 MBEDTLS_VERSION_MAJOR,
10109 MBEDTLS_VERSION_MINOR,
10110 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010111 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10112 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010113};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010114
10115/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010116 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010117 * (in the presentation language of TLS, RFC 8446 section 3)
10118 *
Hanno Becker26829e92019-05-28 14:30:45 +010010119 * opaque mbedtls_version[3]; // major, minor, patch
10120 * opaque session_format[2]; // version-specific 16-bit field determining
10121 * // the format of the remaining
10122 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010123 *
10124 * Note: When updating the format, remember to keep
10125 * these version+format bytes.
10126 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010127 * // In this version, `session_format` determines
10128 * // the setting of those compile-time
10129 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010130 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010131 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010132 * uint8 ciphersuite[2]; // defined by the standard
10133 * uint8 compression; // 0 or 1
10134 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010135 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010136 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010137 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010138 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10139 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10140 * case disabled: uint8_t peer_cert_digest_type;
10141 * opaque peer_cert_digest<0..2^8-1>;
10142 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010143 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010144 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010145 * uint8 mfl_code; // up to 255 according to standard
10146 * uint8 trunc_hmac; // 0 or 1
10147 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010148 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010149 * The order is the same as in the definition of the structure, except
10150 * verify_result is put before peer_cert so that all mandatory fields come
10151 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010152 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010153static int ssl_session_save( const mbedtls_ssl_session *session,
10154 unsigned char omit_header,
10155 unsigned char *buf,
10156 size_t buf_len,
10157 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010158{
10159 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010160 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010161#if defined(MBEDTLS_HAVE_TIME)
10162 uint64_t start;
10163#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010164#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010165#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010166 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010167#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010168#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010169
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010170 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010171 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010172 /*
10173 * Add version identifier
10174 */
10175
10176 used += sizeof( ssl_serialized_session_header );
10177
10178 if( used <= buf_len )
10179 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010180 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010181 sizeof( ssl_serialized_session_header ) );
10182 p += sizeof( ssl_serialized_session_header );
10183 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010184 }
10185
10186 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010187 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010188 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010189#if defined(MBEDTLS_HAVE_TIME)
10190 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010191
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010192 if( used <= buf_len )
10193 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010194 start = (uint64_t) session->start;
10195
10196 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10197 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10198 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10199 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10200 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10201 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10202 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10203 *p++ = (unsigned char)( ( start ) & 0xFF );
10204 }
10205#endif /* MBEDTLS_HAVE_TIME */
10206
10207 /*
10208 * Basic mandatory fields
10209 */
Hanno Becker88440552019-07-03 14:16:13 +010010210 {
10211 size_t const ciphersuite_len = 2;
10212#if defined(MBEDTLS_ZLIB_SUPPORT)
10213 size_t const compression_len = 1;
10214#else
10215 size_t const compression_len = 0;
10216#endif
10217 size_t const id_len_len = 1;
10218 size_t const id_len = 32;
10219 size_t const master_len = 48;
10220 size_t const verif_result_len = 4;
10221
10222 size_t const basic_len =
10223 ciphersuite_len +
10224 compression_len +
10225 id_len_len +
10226 id_len +
10227 master_len +
10228 verif_result_len;
10229
10230 used += basic_len;
10231 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010232
10233 if( used <= buf_len )
10234 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010235 const int ciphersuite =
10236 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010237 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010238
Hanno Becker88440552019-07-03 14:16:13 +010010239#if defined(MBEDTLS_ZLIB_SUPPORT)
10240 *p++ = (unsigned char)(
10241 mbedtls_ssl_session_get_compression( session ) );
10242#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010243
10244 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010245 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10246 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010247 p += 32;
10248
Teppo Järvelin91d79382019-10-02 09:09:31 +030010249 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010250 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010251 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010252 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010253
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010254 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010255 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010256 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010257#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010258#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010259 if( session->peer_cert == NULL )
10260 cert_len = 0;
10261 else
10262 cert_len = session->peer_cert->raw.len;
10263
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010264 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010265
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010266 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010267 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010268 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010269
10270 if( session->peer_cert != NULL )
10271 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010272 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010273 p += cert_len;
10274 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010275 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010276
Hanno Becker5882dd02019-06-06 16:25:57 +010010277#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010278 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010279 if( session->peer_cert_digest != NULL )
10280 {
10281 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10282 if( used <= buf_len )
10283 {
10284 *p++ = (unsigned char) session->peer_cert_digest_type;
10285 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010286 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010287 session->peer_cert_digest_len );
10288 p += session->peer_cert_digest_len;
10289 }
10290 }
10291 else
10292 {
10293 used += 2;
10294 if( used <= buf_len )
10295 {
10296 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10297 *p++ = 0;
10298 }
10299 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010300#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010301#endif /* MBEDTLS_X509_CRT_PARSE_C */
10302
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010303 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010304 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010305 */
10306#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010307 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010308
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010309 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010310 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010311 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010312
10313 if( session->ticket != NULL )
10314 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010315 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010316 p += session->ticket_len;
10317 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010318
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010319 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010320 }
10321#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10322
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010323 /*
10324 * Misc extension-related info
10325 */
10326#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10327 used += 1;
10328
10329 if( used <= buf_len )
10330 *p++ = session->mfl_code;
10331#endif
10332
10333#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10334 used += 1;
10335
10336 if( used <= buf_len )
10337 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10338#endif
10339
10340#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10341 used += 1;
10342
10343 if( used <= buf_len )
10344 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10345#endif
10346
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010347 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010348 *olen = used;
10349
10350 if( used > buf_len )
10351 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010352
10353 return( 0 );
10354}
10355
10356/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010357 * Public wrapper for ssl_session_save()
10358 */
10359int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10360 unsigned char *buf,
10361 size_t buf_len,
10362 size_t *olen )
10363{
10364 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10365}
10366
10367/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010368 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010369 *
10370 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010371 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010372 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010373static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010374 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010375 const unsigned char *buf,
10376 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010377{
10378 const unsigned char *p = buf;
10379 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010380 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010381#if defined(MBEDTLS_HAVE_TIME)
10382 uint64_t start;
10383#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010384#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010385#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010386 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010387#endif
10388#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010389
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010390 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010391 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010392 /*
10393 * Check version identifier
10394 */
10395
10396 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10397 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10398
Teppo Järvelin0efac532019-10-04 13:21:08 +030010399 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010400 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010401 sizeof( ssl_serialized_session_header ) ) != 0 )
10402 {
10403 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10404 }
10405 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010406 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010407
10408 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010409 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010410 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010411#if defined(MBEDTLS_HAVE_TIME)
10412 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10414
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010415 start = ( (uint64_t) p[0] << 56 ) |
10416 ( (uint64_t) p[1] << 48 ) |
10417 ( (uint64_t) p[2] << 40 ) |
10418 ( (uint64_t) p[3] << 32 ) |
10419 ( (uint64_t) p[4] << 24 ) |
10420 ( (uint64_t) p[5] << 16 ) |
10421 ( (uint64_t) p[6] << 8 ) |
10422 ( (uint64_t) p[7] );
10423 p += 8;
10424
10425 session->start = (time_t) start;
10426#endif /* MBEDTLS_HAVE_TIME */
10427
10428 /*
10429 * Basic mandatory fields
10430 */
Hanno Becker88440552019-07-03 14:16:13 +010010431 {
10432 size_t const ciphersuite_len = 2;
10433#if defined(MBEDTLS_ZLIB_SUPPORT)
10434 size_t const compression_len = 1;
10435#else
10436 size_t const compression_len = 0;
10437#endif
10438 size_t const id_len_len = 1;
10439 size_t const id_len = 32;
10440 size_t const master_len = 48;
10441 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010442
Hanno Becker88440552019-07-03 14:16:13 +010010443 size_t const basic_len =
10444 ciphersuite_len +
10445 compression_len +
10446 id_len_len +
10447 id_len +
10448 master_len +
10449 verif_result_len;
10450
10451 if( basic_len > (size_t)( end - p ) )
10452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10453 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010454
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010455 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010456 p += 2;
10457
Hanno Becker73f4cb12019-06-27 13:51:07 +010010458#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010459 session->ciphersuite = ciphersuite;
10460#else
10461 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010462 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010463 {
10464 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10465 }
10466#endif
10467
Hanno Becker88440552019-07-03 14:16:13 +010010468#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010469 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010470#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010471
10472 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010473 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10474 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010475 p += 32;
10476
Teppo Järvelin91d79382019-10-02 09:09:31 +030010477 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010478 p += 48;
10479
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010480 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010481 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010482
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010483 /* Immediately clear invalid pointer values that have been read, in case
10484 * we exit early before we replaced them with valid ones. */
10485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010486#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010487 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010488#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010489 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010490#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010491#endif
10492#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10493 session->ticket = NULL;
10494#endif
10495
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010496 /*
10497 * Peer certificate
10498 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010499#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010500#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010501 if( 3 > (size_t)( end - p ) )
10502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10503
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010504 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10505
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010506 p += 3;
10507
10508 if( cert_len == 0 )
10509 {
10510 session->peer_cert = NULL;
10511 }
10512 else
10513 {
10514 int ret;
10515
10516 if( cert_len > (size_t)( end - p ) )
10517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10518
10519 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10520
10521 if( session->peer_cert == NULL )
10522 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10523
10524 mbedtls_x509_crt_init( session->peer_cert );
10525
10526 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10527 p, cert_len ) ) != 0 )
10528 {
10529 mbedtls_x509_crt_free( session->peer_cert );
10530 mbedtls_free( session->peer_cert );
10531 session->peer_cert = NULL;
10532 return( ret );
10533 }
10534
10535 p += cert_len;
10536 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010537#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010538 /* Deserialize CRT digest from the end of the ticket. */
10539 if( 2 > (size_t)( end - p ) )
10540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10541
10542 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10543 session->peer_cert_digest_len = (size_t) *p++;
10544
10545 if( session->peer_cert_digest_len != 0 )
10546 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010547 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010548 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010549 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10551 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10553
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010554 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10556
10557 session->peer_cert_digest =
10558 mbedtls_calloc( 1, session->peer_cert_digest_len );
10559 if( session->peer_cert_digest == NULL )
10560 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10561
Teppo Järvelin91d79382019-10-02 09:09:31 +030010562 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010563 session->peer_cert_digest_len );
10564 p += session->peer_cert_digest_len;
10565 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010566#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010567#endif /* MBEDTLS_X509_CRT_PARSE_C */
10568
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010569 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010570 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010571 */
10572#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10573 if( 3 > (size_t)( end - p ) )
10574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10575
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010576 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010577 p += 3;
10578
10579 if( session->ticket_len != 0 )
10580 {
10581 if( session->ticket_len > (size_t)( end - p ) )
10582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10583
10584 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10585 if( session->ticket == NULL )
10586 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10587
Teppo Järvelin91d79382019-10-02 09:09:31 +030010588 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010589 p += session->ticket_len;
10590 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010591
10592 if( 4 > (size_t)( end - p ) )
10593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10594
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010595 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010596 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010597#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10598
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010599 /*
10600 * Misc extension-related info
10601 */
10602#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10603 if( 1 > (size_t)( end - p ) )
10604 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10605
10606 session->mfl_code = *p++;
10607#endif
10608
10609#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10610 if( 1 > (size_t)( end - p ) )
10611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10612
10613 session->trunc_hmac = *p++;
10614#endif
10615
10616#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10617 if( 1 > (size_t)( end - p ) )
10618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10619
10620 session->encrypt_then_mac = *p++;
10621#endif
10622
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010623 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010624 if( p != end )
10625 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10626
10627 return( 0 );
10628}
10629
10630/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010631 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010632 */
10633int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10634 const unsigned char *buf,
10635 size_t len )
10636{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010637 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010638
10639 if( ret != 0 )
10640 mbedtls_ssl_session_free( session );
10641
10642 return( ret );
10643}
10644
10645/*
Paul Bakker1961b702013-01-25 14:49:24 +010010646 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010648int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010651
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010652 if( ssl == NULL || ssl->conf == NULL )
10653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010656 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010658#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010660 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010661 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010662#endif
10663
Hanno Beckerb82350b2019-07-26 07:24:05 +010010664 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010665 return( ret );
10666}
10667
10668/*
10669 * Perform the SSL handshake
10670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010671int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010672{
10673 int ret = 0;
10674
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010675 if( ssl == NULL || ssl->conf == NULL )
10676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010680 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010683
10684 if( ret != 0 )
10685 break;
10686 }
10687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010689
10690 return( ret );
10691}
10692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693#if defined(MBEDTLS_SSL_RENEGOTIATION)
10694#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010695/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010696 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010698static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010699{
10700 int ret;
10701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010703
10704 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10706 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010707
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010708 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010709 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010711 return( ret );
10712 }
10713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010715
10716 return( 0 );
10717}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010719
10720/*
10721 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722 * - any side: calling mbedtls_ssl_renegotiate(),
10723 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10724 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010725 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010726 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010727 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010730{
10731 int ret;
10732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010734
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010735 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10736 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010737
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010738 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10739 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010742 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010743 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010744 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10745 MBEDTLS_SSL_IS_SERVER )
10746 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010747 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010748 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010749 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010750 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010751 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010752 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010753 }
10754#endif
10755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10757 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010762 return( ret );
10763 }
10764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010766
10767 return( 0 );
10768}
10769
10770/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010771 * Renegotiate current connection on client,
10772 * or request renegotiation on server
10773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010777
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010778 if( ssl == NULL || ssl->conf == NULL )
10779 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010782 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010783 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010789
10790 /* Did we already try/start sending HelloRequest? */
10791 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010793
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010794 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010795 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010799 /*
10800 * On client, either start the renegotiation process or,
10801 * if already in progress, continue the handshake
10802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010803 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010805 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010807
10808 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010810 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010811 return( ret );
10812 }
10813 }
10814 else
10815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010819 return( ret );
10820 }
10821 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010822#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010823
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010824 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010825}
10826
10827/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010828 * Check record counters and renegotiate if they're above the limit.
10829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010831{
Andres AG2196c7f2016-12-15 17:01:16 +000010832 size_t ep_len = ssl_ep_len( ssl );
10833 int in_ctr_cmp;
10834 int out_ctr_cmp;
10835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10837 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010838 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010839 {
10840 return( 0 );
10841 }
10842
Teppo Järvelin0efac532019-10-04 13:21:08 +030010843 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010844 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010845 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010846 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010847 ssl->conf->renego_period + ep_len, 8 - ep_len );
10848
10849 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010850 {
10851 return( 0 );
10852 }
10853
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010855 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010858
10859/*
10860 * Receive application data decrypted from the SSL layer
10861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010862int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010863{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010864 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010865 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010866
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010867 if( ssl == NULL || ssl->conf == NULL )
10868 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010873 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010876 return( ret );
10877
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010878 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010879 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010880 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010881 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010882 return( ret );
10883 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010884 }
10885#endif
10886
Hanno Becker4a810fb2017-05-24 16:27:30 +010010887 /*
10888 * Check if renegotiation is necessary and/or handshake is
10889 * in process. If yes, perform/continue, and fall through
10890 * if an unexpected packet is received while the client
10891 * is waiting for the ServerHello.
10892 *
10893 * (There is no equivalent to the last condition on
10894 * the server-side as it is not treated as within
10895 * a handshake while waiting for the ClientHello
10896 * after a renegotiation request.)
10897 */
10898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010899#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010900 ret = ssl_check_ctr_renegotiate( ssl );
10901 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10902 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010904 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010905 return( ret );
10906 }
10907#endif
10908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010909 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010912 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10913 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010916 return( ret );
10917 }
10918 }
10919
Hanno Beckere41158b2017-10-23 13:30:32 +010010920 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010921 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010922 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010923 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010924 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10925 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010926 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010927 ssl_set_timer( ssl,
10928 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010929 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010930
Hanno Becker327c93b2018-08-15 13:56:18 +010010931 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010932 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010933 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10934 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010935
Hanno Becker4a810fb2017-05-24 16:27:30 +010010936 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10937 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010938 }
10939
10940 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010941 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010942 {
10943 /*
10944 * OpenSSL sends empty messages to randomize the IV
10945 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010946 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010948 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010949 return( 0 );
10950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010952 return( ret );
10953 }
10954 }
10955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010959
Hanno Becker4a810fb2017-05-24 16:27:30 +010010960 /*
10961 * - For client-side, expect SERVER_HELLO_REQUEST.
10962 * - For server-side, expect CLIENT_HELLO.
10963 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10964 */
10965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010967 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10968 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010969 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010970 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010973
10974 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010976 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010977 {
10978 continue;
10979 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010980 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010981#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010982#if defined(MBEDTLS_SSL_PROTO_TLS)
10983 {
10984 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10985 }
10986#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010987 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010988#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010989
Hanno Becker4a810fb2017-05-24 16:27:30 +010010990#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010991 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10992 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010996
10997 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010999 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010011000 {
11001 continue;
11002 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011003 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011004#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011005#if defined(MBEDTLS_SSL_PROTO_TLS)
11006 {
11007 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
11008 }
11009#endif
Paul Bakker48916f92012-09-16 19:57:18 +000011010 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011011#endif /* MBEDTLS_SSL_SRV_C */
11012
Hanno Becker21df7f92017-10-17 11:03:26 +010011013#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010011014 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011015 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
11016 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010011017 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011018 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
11019 {
11020 /*
11021 * Accept renegotiation request
11022 */
Paul Bakker48916f92012-09-16 19:57:18 +000011023
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011024 /* DTLS clients need to know renego is server-initiated */
11025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011026 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011027 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11028 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011029 {
11030 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11031 }
11032#endif
11033 ret = ssl_start_renegotiation( ssl );
11034 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11035 ret != 0 )
11036 {
11037 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11038 return( ret );
11039 }
11040 }
11041 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011042#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011043 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011044 /*
11045 * Refuse renegotiation
11046 */
11047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011050#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011051 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011052 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011053 /* SSLv3 does not have a "no_renegotiation" warning, so
11054 we send a fatal alert and abort the connection. */
11055 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11056 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11057 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011058 }
11059 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011060#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11061#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11062 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011063 if( mbedtls_ssl_ver_geq(
11064 mbedtls_ssl_get_minor_ver( ssl ),
11065 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011066 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011067 ret = mbedtls_ssl_send_alert_message( ssl,
11068 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11069 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11070 if( ret != 0 )
11071 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011072 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011073 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11075 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011079 }
Paul Bakker48916f92012-09-16 19:57:18 +000011080 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011081
Hanno Becker90333da2017-10-10 11:27:13 +010011082 /* At this point, we don't know whether the renegotiation has been
11083 * completed or not. The cases to consider are the following:
11084 * 1) The renegotiation is complete. In this case, no new record
11085 * has been read yet.
11086 * 2) The renegotiation is incomplete because the client received
11087 * an application data record while awaiting the ServerHello.
11088 * 3) The renegotiation is incomplete because the client received
11089 * a non-handshake, non-application data message while awaiting
11090 * the ServerHello.
11091 * In each of these case, looping will be the proper action:
11092 * - For 1), the next iteration will read a new record and check
11093 * if it's application data.
11094 * - For 2), the loop condition isn't satisfied as application data
11095 * is present, hence continue is the same as break
11096 * - For 3), the loop condition is satisfied and read_record
11097 * will re-deliver the message that was held back by the client
11098 * when expecting the ServerHello.
11099 */
11100 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011101 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011102#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011103 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011104 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011105 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011106 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011107 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011110 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011112 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011113 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011114 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11118 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011121 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011122 }
11123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11127 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011128 }
11129
11130 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011131
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011132 /* We're going to return something now, cancel timer,
11133 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011134 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011135 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011136
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011137#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011138 /* If we requested renego but received AppData, resend HelloRequest.
11139 * Do it now, after setting in_offt, to avoid taking this branch
11140 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011142 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11143 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011144 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011145 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011146 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011149 return( ret );
11150 }
11151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011152#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011153#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011154 }
11155
11156 n = ( len < ssl->in_msglen )
11157 ? len : ssl->in_msglen;
11158
Teppo Järvelin91d79382019-10-02 09:09:31 +030011159 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011160 ssl->in_msglen -= n;
11161
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011162 // clear incoming data after it's copied to buffer
11163 mbedtls_platform_memset(ssl->in_offt, 0, n);
11164
Paul Bakker5121ce52009-01-03 21:22:43 +000011165 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011166 {
11167 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011168 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011169 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011170 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011171 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011172 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011173 /* more data available */
11174 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011175 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011178
Paul Bakker23986e52011-04-24 08:57:21 +000011179 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011180}
11181
11182/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011183 * Send application data to be encrypted by the SSL layer, taking care of max
11184 * fragment length and buffer size.
11185 *
11186 * According to RFC 5246 Section 6.2.1:
11187 *
11188 * Zero-length fragments of Application data MAY be sent as they are
11189 * potentially useful as a traffic analysis countermeasure.
11190 *
11191 * Therefore, it is possible that the input message length is 0 and the
11192 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011193 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011194static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011195 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011196{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011197 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11198 const size_t max_len = (size_t) ret;
11199
11200 if( ret < 0 )
11201 {
11202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11203 return( ret );
11204 }
11205
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011206 if( len > max_len )
11207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011208#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011209 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011212 "maximum fragment length: %d > %d",
11213 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011215 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011216 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011217#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011218#if defined(MBEDTLS_SSL_PROTO_TLS)
11219 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011220 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011221 }
11222#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011223 }
Paul Bakker887bd502011-06-08 13:10:54 +000011224
Paul Bakker5121ce52009-01-03 21:22:43 +000011225 if( ssl->out_left != 0 )
11226 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011227 /*
11228 * The user has previously tried to send the data and
11229 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11230 * written. In this case, we expect the high-level write function
11231 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011233 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011236 return( ret );
11237 }
11238 }
Paul Bakker887bd502011-06-08 13:10:54 +000011239 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011240 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011241 /*
11242 * The user is trying to send a message the first time, so we need to
11243 * copy the data into the internal buffers and setup the data structure
11244 * to keep track of partial writes
11245 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011246 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011247 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011248 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011249
Hanno Becker67bc7c32018-08-06 11:33:50 +010011250 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011253 return( ret );
11254 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011255 }
11256
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011257 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011258}
11259
11260/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011261 * Write application data, doing 1/n-1 splitting if necessary.
11262 *
11263 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011264 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011265 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011267#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011268static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011269 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011270{
11271 int ret;
11272
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011273 if( ssl->conf->cbc_record_splitting ==
11274 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011275 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011276 mbedtls_ssl_ver_gt(
11277 mbedtls_ssl_get_minor_ver( ssl ),
11278 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011279 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11280 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011281 {
11282 return( ssl_write_real( ssl, buf, len ) );
11283 }
11284
11285 if( ssl->split_done == 0 )
11286 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011287 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011288 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011289 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011290 }
11291
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011292 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11293 return( ret );
11294 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011295
11296 return( ret + 1 );
11297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011298#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011299
11300/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011301 * Write application data (public-facing wrapper)
11302 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011303int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011304{
11305 int ret;
11306
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011308
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011309 if( ssl == NULL || ssl->conf == NULL )
11310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11311
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011312#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011313 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11314 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011315 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011316 return( ret );
11317 }
11318#endif
11319
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011320 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011321 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011322 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011323 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011325 return( ret );
11326 }
11327 }
11328
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011329#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011330 ret = ssl_write_split( ssl, buf, len );
11331#else
11332 ret = ssl_write_real( ssl, buf, len );
11333#endif
11334
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011336
11337 return( ret );
11338}
11339
11340/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011341 * Notify the peer that the connection is being closed
11342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011343int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011344{
11345 int ret;
11346
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011347 if( ssl == NULL || ssl->conf == NULL )
11348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011351
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011352 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011353 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011355 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011357 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11358 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11359 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011362 return( ret );
11363 }
11364 }
11365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011367
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011368 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011369}
11370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011371void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011372{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011373 if( transform == NULL )
11374 return;
11375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011376#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011377 deflateEnd( &transform->ctx_deflate );
11378 inflateEnd( &transform->ctx_inflate );
11379#endif
11380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011381 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11382 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011383
Hanno Becker92231322018-01-03 15:32:51 +000011384#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011385 mbedtls_md_free( &transform->md_ctx_enc );
11386 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011387#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011388
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011389 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011390}
11391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011392#if defined(MBEDTLS_X509_CRT_PARSE_C)
11393static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011395 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011396
11397 while( cur != NULL )
11398 {
11399 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011400 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011401 cur = next;
11402 }
11403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011404#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011405
Hanno Becker0271f962018-08-16 13:23:47 +010011406#if defined(MBEDTLS_SSL_PROTO_DTLS)
11407
11408static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11409{
11410 unsigned offset;
11411 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11412
11413 if( hs == NULL )
11414 return;
11415
Hanno Becker283f5ef2018-08-24 09:34:47 +010011416 ssl_free_buffered_record( ssl );
11417
Hanno Becker0271f962018-08-16 13:23:47 +010011418 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011419 ssl_buffering_free_slot( ssl, offset );
11420}
11421
11422static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11423 uint8_t slot )
11424{
11425 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11426 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011427
11428 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11429 return;
11430
Hanno Beckere605b192018-08-21 15:59:07 +010011431 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011432 {
Hanno Beckere605b192018-08-21 15:59:07 +010011433 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011434 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011435 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011436 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011437 }
11438}
11439
11440#endif /* MBEDTLS_SSL_PROTO_DTLS */
11441
Gilles Peskine9b562d52018-04-25 20:32:43 +020011442void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011443{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011444 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11445
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011446 if( handshake == NULL )
11447 return;
11448
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011449#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11450 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11451 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011452 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011453 handshake->async_in_progress = 0;
11454 }
11455#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11456
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011457#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11458 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11459 mbedtls_md5_free( &handshake->fin_md5 );
11460 mbedtls_sha1_free( &handshake->fin_sha1 );
11461#endif
11462#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11463#if defined(MBEDTLS_SHA256_C)
11464 mbedtls_sha256_free( &handshake->fin_sha256 );
11465#endif
11466#if defined(MBEDTLS_SHA512_C)
11467 mbedtls_sha512_free( &handshake->fin_sha512 );
11468#endif
11469#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011471#if defined(MBEDTLS_DHM_C)
11472 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011473#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011474#if defined(MBEDTLS_ECDH_C)
11475 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011476#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011477#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011478 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011479#if defined(MBEDTLS_SSL_CLI_C)
11480 mbedtls_free( handshake->ecjpake_cache );
11481 handshake->ecjpake_cache = NULL;
11482 handshake->ecjpake_cache_len = 0;
11483#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011484#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011485
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011486#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11487 if( handshake->psk != NULL )
11488 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011489 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011490 mbedtls_free( handshake->psk );
11491 }
11492#endif
11493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011494#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11495 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011496 /*
11497 * Free only the linked list wrapper, not the keys themselves
11498 * since the belong to the SNI callback
11499 */
11500 if( handshake->sni_key_cert != NULL )
11501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011502 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011503
11504 while( cur != NULL )
11505 {
11506 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011507 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011508 cur = next;
11509 }
11510 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011511#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011512
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011513#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011514 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011515 if( handshake->ecrs_peer_cert != NULL )
11516 {
11517 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11518 mbedtls_free( handshake->ecrs_peer_cert );
11519 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011520#endif
11521
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011522#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11523 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11524 mbedtls_pk_free( &handshake->peer_pubkey );
11525#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011527#if defined(MBEDTLS_SSL_PROTO_DTLS)
11528 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011529 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011530 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011531#endif
11532
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011533 mbedtls_platform_zeroize( handshake,
11534 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011535}
11536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011537void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011538{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011539 if( session == NULL )
11540 return;
11541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011542#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011543 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011544#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011545
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011546#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011547 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011548#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011549
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011550 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011551}
11552
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011553#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011554
11555#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11556#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11557#else
11558#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11559#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11560
11561#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11562#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11563#else
11564#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11565#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11566
11567#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11568#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11569#else
11570#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11571#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11572
11573#if defined(MBEDTLS_SSL_ALPN)
11574#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11575#else
11576#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11577#endif /* MBEDTLS_SSL_ALPN */
11578
11579#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11580#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11581#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11582#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11583
11584#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11585 ( (uint32_t) ( \
11586 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11587 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11588 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11589 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11590 0u ) )
11591
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011592static unsigned char ssl_serialized_context_header[] = {
11593 MBEDTLS_VERSION_MAJOR,
11594 MBEDTLS_VERSION_MINOR,
11595 MBEDTLS_VERSION_PATCH,
11596 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11597 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011598 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11599 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11600 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011601};
11602
Paul Bakker5121ce52009-01-03 21:22:43 +000011603/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011604 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011605 *
11606 * The format of the serialized data is:
11607 * (in the presentation language of TLS, RFC 8446 section 3)
11608 *
11609 * // header
11610 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011611 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011612 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011613 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011614 * Note: When updating the format, remember to keep these
11615 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011616 *
11617 * // session sub-structure
11618 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11619 * // transform sub-structure
11620 * uint8 random[64]; // ServerHello.random+ClientHello.random
11621 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11622 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11623 * // fields from ssl_context
11624 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11625 * uint64 in_window_top; // DTLS: last validated record seq_num
11626 * uint64 in_window; // DTLS: bitmask for replay protection
11627 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11628 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11629 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11630 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11631 *
11632 * Note that many fields of the ssl_context or sub-structures are not
11633 * serialized, as they fall in one of the following categories:
11634 *
11635 * 1. forced value (eg in_left must be 0)
11636 * 2. pointer to dynamically-allocated memory (eg session, transform)
11637 * 3. value can be re-derived from other data (eg session keys from MS)
11638 * 4. value was temporary (eg content of input buffer)
11639 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011640 */
11641int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11642 unsigned char *buf,
11643 size_t buf_len,
11644 size_t *olen )
11645{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011646 unsigned char *p = buf;
11647 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011648 size_t session_len;
11649 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011650
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011651 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011652 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11653 * this function's documentation.
11654 *
11655 * These are due to assumptions/limitations in the implementation. Some of
11656 * them are likely to stay (no handshake in progress) some might go away
11657 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011658 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011659 /* The initial handshake must be over */
11660 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011662 if( ssl->handshake != NULL )
11663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11664 /* Double-check that sub-structures are indeed ready */
11665 if( ssl->transform == NULL || ssl->session == NULL )
11666 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11667 /* There must be no pending incoming or outgoing data */
11668 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11670 if( ssl->out_left != 0 )
11671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11672 /* Protocol must be DLTS, not TLS */
11673 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11674 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11675 /* Version must be 1.2 */
11676 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11678 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11680 /* We must be using an AEAD ciphersuite */
11681 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11683 /* Renegotiation must not be enabled */
11684 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011686
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011687 /*
11688 * Version and format identifier
11689 */
11690 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011691
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011692 if( used <= buf_len )
11693 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011694 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011695 sizeof( ssl_serialized_context_header ) );
11696 p += sizeof( ssl_serialized_context_header );
11697 }
11698
11699 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011700 * Session (length + data)
11701 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011702 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011703 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11704 return( ret );
11705
11706 used += 4 + session_len;
11707 if( used <= buf_len )
11708 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011709 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011710
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011711 ret = ssl_session_save( ssl->session, 1,
11712 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011713 if( ret != 0 )
11714 return( ret );
11715
11716 p += session_len;
11717 }
11718
11719 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011720 * Transform
11721 */
11722 used += sizeof( ssl->transform->randbytes );
11723 if( used <= buf_len )
11724 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011725 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011726 sizeof( ssl->transform->randbytes ) );
11727 p += sizeof( ssl->transform->randbytes );
11728 }
11729
11730#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11731 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11732 if( used <= buf_len )
11733 {
11734 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011735 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11736 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011737 p += ssl->transform->in_cid_len;
11738
11739 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011740 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11741 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011742 p += ssl->transform->out_cid_len;
11743 }
11744#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11745
11746 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011747 * Saved fields from top-level ssl_context structure
11748 */
11749#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11750 used += 4;
11751 if( used <= buf_len )
11752 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011753 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011754 }
11755#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11756
11757#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11758 used += 16;
11759 if( used <= buf_len )
11760 {
11761 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11762 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11763 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11764 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11765 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11766 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11767 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11768 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11769
11770 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11771 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11772 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11773 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11774 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11775 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11776 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11777 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11778 }
11779#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11780
11781#if defined(MBEDTLS_SSL_PROTO_DTLS)
11782 used += 1;
11783 if( used <= buf_len )
11784 {
11785 *p++ = ssl->disable_datagram_packing;
11786 }
11787#endif /* MBEDTLS_SSL_PROTO_DTLS */
11788
11789 used += 8;
11790 if( used <= buf_len )
11791 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011792 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011793 p += 8;
11794 }
11795
11796#if defined(MBEDTLS_SSL_PROTO_DTLS)
11797 used += 2;
11798 if( used <= buf_len )
11799 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011800 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011801 }
11802#endif /* MBEDTLS_SSL_PROTO_DTLS */
11803
11804#if defined(MBEDTLS_SSL_ALPN)
11805 {
11806 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011807 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011808 : 0;
11809
11810 used += 1 + alpn_len;
11811 if( used <= buf_len )
11812 {
11813 *p++ = alpn_len;
11814
11815 if( ssl->alpn_chosen != NULL )
11816 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011817 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011818 p += alpn_len;
11819 }
11820 }
11821 }
11822#endif /* MBEDTLS_SSL_ALPN */
11823
11824 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011825 * Done
11826 */
11827 *olen = used;
11828
11829 if( used > buf_len )
11830 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011831
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011832 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11833
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011834 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011835}
11836
11837/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011838 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011839 *
11840 * This internal version is wrapped by a public function that cleans up in
11841 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011842 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011843static int ssl_context_load( mbedtls_ssl_context *ssl,
11844 const unsigned char *buf,
11845 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011846{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011847 const unsigned char *p = buf;
11848 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011849 size_t session_len;
11850 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011851
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011852 /*
11853 * The context should have been freshly setup or reset.
11854 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011855 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011856 * renegotiating, or if the user mistakenly loaded a session first.)
11857 */
11858 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11859 ssl->session != NULL )
11860 {
11861 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11862 }
11863
11864 /*
11865 * We can't check that the config matches the initial one, but we can at
11866 * least check it matches the requirements for serializing.
11867 */
11868 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011869 mbedtls_ssl_ver_lt(
11870 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11871 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11872 mbedtls_ssl_ver_gt(
11873 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11874 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11875 mbedtls_ssl_ver_lt(
11876 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11877 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11878 mbedtls_ssl_ver_gt(
11879 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11880 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011881 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011882 {
11883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11884 }
11885
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011886 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11887
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011888 /*
11889 * Check version identifier
11890 */
11891 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11893
Teppo Järvelin650343c2019-10-03 15:36:59 +030011894 // use regular memcmp as header is not that critical
11895 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011896 sizeof( ssl_serialized_context_header ) ) != 0 )
11897 {
11898 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11899 }
11900 p += sizeof( ssl_serialized_context_header );
11901
11902 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011903 * Session
11904 */
11905 if( (size_t)( end - p ) < 4 )
11906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11907
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011908 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011909 p += 4;
11910
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011911 /* This has been allocated by ssl_handshake_init(), called by
11912 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11913 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011914 ssl->session_in = ssl->session;
11915 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011916 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011917
11918 if( (size_t)( end - p ) < session_len )
11919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11920
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011921 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011922 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011923 {
11924 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011925 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011926 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011927
11928 p += session_len;
11929
11930 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011931 * Transform
11932 */
11933
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011934 /* This has been allocated by ssl_handshake_init(), called by
11935 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11936 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011937 ssl->transform_in = ssl->transform;
11938 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011939 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011940
11941 /* Read random bytes and populate structure */
11942 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11944
11945 ret = ssl_populate_transform( ssl->transform,
11946 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11947 ssl->session->master,
11948#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11950 ssl->session->encrypt_then_mac,
11951#endif
11952#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11953 ssl->session->trunc_hmac,
11954#endif
11955#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11956#if defined(MBEDTLS_ZLIB_SUPPORT)
11957 ssl->session->compression,
11958#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011959 p, /* currently pointing to randbytes */
11960 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11961 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11962 ssl );
11963 if( ret != 0 )
11964 return( ret );
11965
11966 p += sizeof( ssl->transform->randbytes );
11967
11968#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11969 /* Read connection IDs and store them */
11970 if( (size_t)( end - p ) < 1 )
11971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11972
11973 ssl->transform->in_cid_len = *p++;
11974
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011975 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11977
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011978 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11979 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011980 p += ssl->transform->in_cid_len;
11981
11982 ssl->transform->out_cid_len = *p++;
11983
11984 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11986
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011987 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11988 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011989 p += ssl->transform->out_cid_len;
11990#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11991
11992 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011993 * Saved fields from top-level ssl_context structure
11994 */
11995#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11996 if( (size_t)( end - p ) < 4 )
11997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11998
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011999 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012000 p += 4;
12001#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
12002
12003#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
12004 if( (size_t)( end - p ) < 16 )
12005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12006
12007 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
12008 ( (uint64_t) p[1] << 48 ) |
12009 ( (uint64_t) p[2] << 40 ) |
12010 ( (uint64_t) p[3] << 32 ) |
12011 ( (uint64_t) p[4] << 24 ) |
12012 ( (uint64_t) p[5] << 16 ) |
12013 ( (uint64_t) p[6] << 8 ) |
12014 ( (uint64_t) p[7] );
12015 p += 8;
12016
12017 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
12018 ( (uint64_t) p[1] << 48 ) |
12019 ( (uint64_t) p[2] << 40 ) |
12020 ( (uint64_t) p[3] << 32 ) |
12021 ( (uint64_t) p[4] << 24 ) |
12022 ( (uint64_t) p[5] << 16 ) |
12023 ( (uint64_t) p[6] << 8 ) |
12024 ( (uint64_t) p[7] );
12025 p += 8;
12026#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12027
12028#if defined(MBEDTLS_SSL_PROTO_DTLS)
12029 if( (size_t)( end - p ) < 1 )
12030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12031
12032 ssl->disable_datagram_packing = *p++;
12033#endif /* MBEDTLS_SSL_PROTO_DTLS */
12034
12035 if( (size_t)( end - p ) < 8 )
12036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12037
Teppo Järvelin91d79382019-10-02 09:09:31 +030012038 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012039 p += 8;
12040
12041#if defined(MBEDTLS_SSL_PROTO_DTLS)
12042 if( (size_t)( end - p ) < 2 )
12043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012044 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012045 p += 2;
12046#endif /* MBEDTLS_SSL_PROTO_DTLS */
12047
12048#if defined(MBEDTLS_SSL_ALPN)
12049 {
12050 uint8_t alpn_len;
12051 const char **cur;
12052
12053 if( (size_t)( end - p ) < 1 )
12054 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12055
12056 alpn_len = *p++;
12057
12058 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12059 {
12060 /* alpn_chosen should point to an item in the configured list */
12061 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12062 {
12063 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012064 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012065 {
12066 ssl->alpn_chosen = *cur;
12067 break;
12068 }
12069 }
12070 }
12071
12072 /* can only happen on conf mismatch */
12073 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12075
12076 p += alpn_len;
12077 }
12078#endif /* MBEDTLS_SSL_ALPN */
12079
12080 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012081 * Forced fields from top-level ssl_context structure
12082 *
12083 * Most of them already set to the correct value by mbedtls_ssl_init() and
12084 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12085 */
12086 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12087
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012088#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012089 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012090#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12091#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012092 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012093#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012094
Hanno Becker83985822019-08-30 10:42:49 +010012095 /* Adjust pointers for header fields of outgoing records to
12096 * the given transform, accounting for explicit IV and CID. */
12097 ssl_update_out_pointers( ssl, ssl->transform );
12098
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012099#if defined(MBEDTLS_SSL_PROTO_DTLS)
12100 ssl->in_epoch = 1;
12101#endif
12102
12103 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12104 * which we don't want - otherwise we'd end up freeing the wrong transform
12105 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12106 if( ssl->handshake != NULL )
12107 {
12108 mbedtls_ssl_handshake_free( ssl );
12109 mbedtls_free( ssl->handshake );
12110 ssl->handshake = NULL;
12111 }
12112
12113 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012114 * Done - should have consumed entire buffer
12115 */
12116 if( p != end )
12117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012118
12119 return( 0 );
12120}
12121
12122/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012123 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012124 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012125int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012126 const unsigned char *buf,
12127 size_t len )
12128{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012129 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012130
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012131 if( ret != 0 )
12132 mbedtls_ssl_free( context );
12133
12134 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012135}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012136#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012137
12138/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012139 * Free an SSL context
12140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012141void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012142{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012143 if( ssl == NULL )
12144 return;
12145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012147
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012148 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012149 {
Angus Grattond8213d02016-05-25 20:56:48 +100012150 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012151 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012152 }
12153
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012154 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012155 {
Angus Grattond8213d02016-05-25 20:56:48 +100012156 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012157 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012158 }
12159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012160#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012161 if( ssl->compress_buf != NULL )
12162 {
Angus Grattond8213d02016-05-25 20:56:48 +100012163 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012164 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012165 }
12166#endif
12167
Paul Bakker48916f92012-09-16 19:57:18 +000012168 if( ssl->transform )
12169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012170 mbedtls_ssl_transform_free( ssl->transform );
12171 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012172 }
12173
12174 if( ssl->handshake )
12175 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012176 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012177 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12178 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012180 mbedtls_free( ssl->handshake );
12181 mbedtls_free( ssl->transform_negotiate );
12182 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012183 }
12184
Paul Bakkerc0463502013-02-14 11:19:38 +010012185 if( ssl->session )
12186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012187 mbedtls_ssl_session_free( ssl->session );
12188 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012189 }
12190
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012191#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012192 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012193 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012194 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012195 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012196 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012197#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012199#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12200 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12203 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012204 }
12205#endif
12206
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012207#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012208 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012209#endif
12210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012212
Paul Bakker86f04f42013-02-14 11:20:09 +010012213 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012214 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012215}
12216
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012217/*
12218 * Initialze mbedtls_ssl_config
12219 */
12220void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12221{
12222 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012223
12224#if !defined(MBEDTLS_SSL_PROTO_TLS)
12225 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12226#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012227}
12228
Simon Butcherc97b6972015-12-27 23:48:17 +000012229#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012230#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012231static int ssl_preset_default_hashes[] = {
12232#if defined(MBEDTLS_SHA512_C)
12233 MBEDTLS_MD_SHA512,
12234 MBEDTLS_MD_SHA384,
12235#endif
12236#if defined(MBEDTLS_SHA256_C)
12237 MBEDTLS_MD_SHA256,
12238 MBEDTLS_MD_SHA224,
12239#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012240#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012241 MBEDTLS_MD_SHA1,
12242#endif
12243 MBEDTLS_MD_NONE
12244};
Simon Butcherc97b6972015-12-27 23:48:17 +000012245#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012246#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012247
Hanno Becker73f4cb12019-06-27 13:51:07 +010012248#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012249static int ssl_preset_suiteb_ciphersuites[] = {
12250 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12251 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12252 0
12253};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012254#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012255
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012256#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012257#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012258static int ssl_preset_suiteb_hashes[] = {
12259 MBEDTLS_MD_SHA256,
12260 MBEDTLS_MD_SHA384,
12261 MBEDTLS_MD_NONE
12262};
12263#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012264#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012265
Hanno Beckerc1096e72019-06-19 12:30:41 +010012266#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012267static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012268#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012269 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012270#endif
12271#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012272 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012273#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012274 MBEDTLS_ECP_DP_NONE
12275};
12276#endif
12277
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012278/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012279 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012280 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012281int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012282 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012283{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012284#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012285 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012286#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012287
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012288 /* Use the functions here so that they are covered in tests,
12289 * but otherwise access member directly for efficiency */
12290 mbedtls_ssl_conf_endpoint( conf, endpoint );
12291 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012292
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012293 /*
12294 * Things that are common to all presets
12295 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012296#if defined(MBEDTLS_SSL_CLI_C)
12297 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12298 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012299#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012300 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012301#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012302#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12303 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12304#endif
12305 }
12306#endif
12307
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012308#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012309 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012310#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012311
12312#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12313 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12314#endif
12315
12316#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012317#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012318 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012319#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12320#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012321 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012322 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012323#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012324#endif
12325
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012326#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12327 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12328#endif
12329
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012330#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012331 conf->f_cookie_write = ssl_cookie_write_dummy;
12332 conf->f_cookie_check = ssl_cookie_check_dummy;
12333#endif
12334
Hanno Becker7f376f42019-06-12 16:20:48 +010012335#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12336 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012337 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12338#endif
12339
Janos Follath088ce432017-04-10 12:42:31 +010012340#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012341#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012342 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012343#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12344#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012345
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012346#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012347#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012348 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012349#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12350#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012351 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012352#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12353#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012354
12355#if defined(MBEDTLS_SSL_RENEGOTIATION)
12356 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012357 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12358 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012359#endif
12360
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012361#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12362 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12363 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012364 const unsigned char dhm_p[] =
12365 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12366 const unsigned char dhm_g[] =
12367 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12368
Hanno Beckera90658f2017-10-04 15:29:08 +010012369 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12370 dhm_p, sizeof( dhm_p ),
12371 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012372 {
12373 return( ret );
12374 }
12375 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012376#endif
12377
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012378 /*
12379 * Preset-specific defaults
12380 */
12381 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012382 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012383 /*
12384 * NSA Suite B
12385 */
12386 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012387#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012388 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012389#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12390#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012391 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012392#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12393#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012394 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012395#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12396#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012397 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012398#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012399
Hanno Becker73f4cb12019-06-27 13:51:07 +010012400#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012401 conf->ciphersuite_list[0] =
12402 conf->ciphersuite_list[1] =
12403 conf->ciphersuite_list[2] =
12404 conf->ciphersuite_list[3] =
12405 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012406#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012407
12408#if defined(MBEDTLS_X509_CRT_PARSE_C)
12409 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012410#endif
12411
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012412#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012413#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012414 conf->sig_hashes = ssl_preset_suiteb_hashes;
12415#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012416#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012417
12418#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012419#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012420 conf->curve_list = ssl_preset_suiteb_curves;
12421#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012422#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012423 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012424
12425 /*
12426 * Default
12427 */
12428 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012429#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012430 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12431 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12432 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12433 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012434#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12435#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012436 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12437 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12438 MBEDTLS_SSL_MIN_MINOR_VERSION :
12439 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012440#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012441 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012442 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12443#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012444#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12445#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12446 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12447#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12448#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12449 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12450#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012451
Hanno Becker73f4cb12019-06-27 13:51:07 +010012452#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012453 conf->ciphersuite_list[0] =
12454 conf->ciphersuite_list[1] =
12455 conf->ciphersuite_list[2] =
12456 conf->ciphersuite_list[3] =
12457 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012458#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012459
12460#if defined(MBEDTLS_X509_CRT_PARSE_C)
12461 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12462#endif
12463
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012464#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012465#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012466 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012467#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012468#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012469
12470#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012471#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012472 conf->curve_list = mbedtls_ecp_grp_id_list();
12473#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012474#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012475
12476#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12477 conf->dhm_min_bitlen = 1024;
12478#endif
12479 }
12480
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012481 return( 0 );
12482}
12483
12484/*
12485 * Free mbedtls_ssl_config
12486 */
12487void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12488{
12489#if defined(MBEDTLS_DHM_C)
12490 mbedtls_mpi_free( &conf->dhm_P );
12491 mbedtls_mpi_free( &conf->dhm_G );
12492#endif
12493
12494#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12495 if( conf->psk != NULL )
12496 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012497 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012498 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012499 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012500 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012501 }
12502
12503 if( conf->psk_identity != NULL )
12504 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012505 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012506 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012507 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012508 conf->psk_identity_len = 0;
12509 }
12510#endif
12511
12512#if defined(MBEDTLS_X509_CRT_PARSE_C)
12513 ssl_key_cert_free( conf->key_cert );
12514#endif
12515
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012516 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012517}
12518
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012519#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012520 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12521 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012522/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012523 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012525unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012527#if defined(MBEDTLS_RSA_C)
12528 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12529 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012530#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012531#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012532 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12533 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012535 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012536}
12537
Hanno Becker7e5437a2017-04-28 17:15:26 +010012538unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12539{
12540 switch( type ) {
12541 case MBEDTLS_PK_RSA:
12542 return( MBEDTLS_SSL_SIG_RSA );
12543 case MBEDTLS_PK_ECDSA:
12544 case MBEDTLS_PK_ECKEY:
12545 return( MBEDTLS_SSL_SIG_ECDSA );
12546 default:
12547 return( MBEDTLS_SSL_SIG_ANON );
12548 }
12549}
12550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012551mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012552{
12553 switch( sig )
12554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012555#if defined(MBEDTLS_RSA_C)
12556 case MBEDTLS_SSL_SIG_RSA:
12557 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012558#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012559#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012560 case MBEDTLS_SSL_SIG_ECDSA:
12561 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012562#endif
12563 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012564 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012565 }
12566}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012567#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012568
Hanno Becker7e5437a2017-04-28 17:15:26 +010012569#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12570 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12571
12572/* Find an entry in a signature-hash set matching a given hash algorithm. */
12573mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12574 mbedtls_pk_type_t sig_alg )
12575{
12576 switch( sig_alg )
12577 {
12578 case MBEDTLS_PK_RSA:
12579 return( set->rsa );
12580 case MBEDTLS_PK_ECDSA:
12581 return( set->ecdsa );
12582 default:
12583 return( MBEDTLS_MD_NONE );
12584 }
12585}
12586
12587/* Add a signature-hash-pair to a signature-hash set */
12588void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12589 mbedtls_pk_type_t sig_alg,
12590 mbedtls_md_type_t md_alg )
12591{
12592 switch( sig_alg )
12593 {
12594 case MBEDTLS_PK_RSA:
12595 if( set->rsa == MBEDTLS_MD_NONE )
12596 set->rsa = md_alg;
12597 break;
12598
12599 case MBEDTLS_PK_ECDSA:
12600 if( set->ecdsa == MBEDTLS_MD_NONE )
12601 set->ecdsa = md_alg;
12602 break;
12603
12604 default:
12605 break;
12606 }
12607}
12608
12609/* Allow exactly one hash algorithm for each signature. */
12610void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12611 mbedtls_md_type_t md_alg )
12612{
12613 set->rsa = md_alg;
12614 set->ecdsa = md_alg;
12615}
12616
12617#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12618 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12619
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012620/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012621 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012623mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012624{
12625 switch( hash )
12626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012627#if defined(MBEDTLS_MD5_C)
12628 case MBEDTLS_SSL_HASH_MD5:
12629 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012630#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012631#if defined(MBEDTLS_SHA1_C)
12632 case MBEDTLS_SSL_HASH_SHA1:
12633 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012634#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012635#if defined(MBEDTLS_SHA256_C)
12636 case MBEDTLS_SSL_HASH_SHA224:
12637 return( MBEDTLS_MD_SHA224 );
12638 case MBEDTLS_SSL_HASH_SHA256:
12639 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012640#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012641#if defined(MBEDTLS_SHA512_C)
12642 case MBEDTLS_SSL_HASH_SHA384:
12643 return( MBEDTLS_MD_SHA384 );
12644 case MBEDTLS_SSL_HASH_SHA512:
12645 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012646#endif
12647 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012648 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012649 }
12650}
12651
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012652/*
12653 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12654 */
12655unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12656{
12657 switch( md )
12658 {
12659#if defined(MBEDTLS_MD5_C)
12660 case MBEDTLS_MD_MD5:
12661 return( MBEDTLS_SSL_HASH_MD5 );
12662#endif
12663#if defined(MBEDTLS_SHA1_C)
12664 case MBEDTLS_MD_SHA1:
12665 return( MBEDTLS_SSL_HASH_SHA1 );
12666#endif
12667#if defined(MBEDTLS_SHA256_C)
12668 case MBEDTLS_MD_SHA224:
12669 return( MBEDTLS_SSL_HASH_SHA224 );
12670 case MBEDTLS_MD_SHA256:
12671 return( MBEDTLS_SSL_HASH_SHA256 );
12672#endif
12673#if defined(MBEDTLS_SHA512_C)
12674 case MBEDTLS_MD_SHA384:
12675 return( MBEDTLS_SSL_HASH_SHA384 );
12676 case MBEDTLS_MD_SHA512:
12677 return( MBEDTLS_SSL_HASH_SHA512 );
12678#endif
12679 default:
12680 return( MBEDTLS_SSL_HASH_NONE );
12681 }
12682}
12683
Hanno Beckeree902df2019-08-23 13:47:47 +010012684#if defined(MBEDTLS_USE_TINYCRYPT)
12685/*
12686 * Check if a curve proposed by the peer is in our list.
12687 * Return 0 if we're willing to use it, -1 otherwise.
12688 */
12689int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12690 mbedtls_uecc_group_id grp_id )
12691{
12692 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12693 if( own_ec_id == grp_id )
12694 return( 0 );
12695 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12696
12697 return( -1 );
12698}
12699#endif /* MBEDTLS_USE_TINYCRYPT */
12700
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012701#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012702/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012703 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012704 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012705 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012706int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12707 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012708{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012709 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12710 if( own_ec_id == grp_id )
12711 return( 0 );
12712 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012713
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012714 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012715}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012716#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012717
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012718#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012719/*
12720 * Check if a hash proposed by the peer is in our list.
12721 * Return 0 if we're willing to use it, -1 otherwise.
12722 */
12723int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12724 mbedtls_md_type_t md )
12725{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012726 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12727 if( md_alg == md )
12728 return( 0 );
12729 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012730
12731 return( -1 );
12732}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012733#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012735#if defined(MBEDTLS_X509_CRT_PARSE_C)
12736int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012737 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012738 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012739 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012740{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012741 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012742#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012743 int usage = 0;
12744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012745#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012746 const char *ext_oid;
12747 size_t ext_len;
12748#endif
12749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012750#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12751 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012752 ((void) cert);
12753 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012754 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012755#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012757#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12758 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012759 {
12760 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012761 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012763 case MBEDTLS_KEY_EXCHANGE_RSA:
12764 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012765 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012766 break;
12767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012768 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12769 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12770 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12771 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
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_ECDH_RSA:
12775 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012776 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012777 break;
12778
12779 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012780 case MBEDTLS_KEY_EXCHANGE_NONE:
12781 case MBEDTLS_KEY_EXCHANGE_PSK:
12782 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12783 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012784 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012785 usage = 0;
12786 }
12787 }
12788 else
12789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012790 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12791 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012792 }
12793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012794 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012795 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012796 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012797 ret = -1;
12798 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012799#else
12800 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012801#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012803#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12804 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012806 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12807 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012808 }
12809 else
12810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012811 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12812 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012813 }
12814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012815 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012816 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012817 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012818 ret = -1;
12819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012820#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012821
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012822 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012824#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012825
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012826#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12827 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12828int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12829 unsigned char *output,
12830 unsigned char *data, size_t data_len )
12831{
12832 int ret = 0;
12833 mbedtls_md5_context mbedtls_md5;
12834 mbedtls_sha1_context mbedtls_sha1;
12835
12836 mbedtls_md5_init( &mbedtls_md5 );
12837 mbedtls_sha1_init( &mbedtls_sha1 );
12838
12839 /*
12840 * digitally-signed struct {
12841 * opaque md5_hash[16];
12842 * opaque sha_hash[20];
12843 * };
12844 *
12845 * md5_hash
12846 * MD5(ClientHello.random + ServerHello.random
12847 * + ServerParams);
12848 * sha_hash
12849 * SHA(ClientHello.random + ServerHello.random
12850 * + ServerParams);
12851 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012852 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012853 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012855 goto exit;
12856 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012857 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012858 ssl->handshake->randbytes, 64 ) ) != 0 )
12859 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012860 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_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, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012864 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012866 goto exit;
12867 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012868 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012869 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012871 goto exit;
12872 }
12873
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012874 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012875 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012876 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012877 goto exit;
12878 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012879 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012880 ssl->handshake->randbytes, 64 ) ) != 0 )
12881 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_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, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012886 data_len ) ) != 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_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012892 output + 16 ) ) != 0 )
12893 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012895 goto exit;
12896 }
12897
12898exit:
12899 mbedtls_md5_free( &mbedtls_md5 );
12900 mbedtls_sha1_free( &mbedtls_sha1 );
12901
12902 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012903 mbedtls_ssl_pend_fatal_alert( ssl,
12904 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012905
12906 return( ret );
12907
12908}
12909#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12910 MBEDTLS_SSL_PROTO_TLS1_1 */
12911
12912#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12913 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12914int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012915 unsigned char *hash, size_t *hashlen,
12916 unsigned char *data, size_t data_len,
12917 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012918{
12919 int ret = 0;
12920 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012921 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012922 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012923
12924 mbedtls_md_init( &ctx );
12925
12926 /*
12927 * digitally-signed struct {
12928 * opaque client_random[32];
12929 * opaque server_random[32];
12930 * ServerDHParams params;
12931 * };
12932 */
12933 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12934 {
12935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12936 goto exit;
12937 }
12938 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12939 {
12940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12941 goto exit;
12942 }
12943 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12944 {
12945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12946 goto exit;
12947 }
12948 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12949 {
12950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12951 goto exit;
12952 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012953 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012954 {
12955 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12956 goto exit;
12957 }
12958
12959exit:
12960 mbedtls_md_free( &ctx );
12961
12962 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012963 mbedtls_ssl_pend_fatal_alert( ssl,
12964 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012965
12966 return( ret );
12967}
12968#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12969 MBEDTLS_SSL_PROTO_TLS1_2 */
12970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012971#endif /* MBEDTLS_SSL_TLS_C */