blob: c79fe7d8667954048b200f804dfb18a6ee468520 [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 {
2032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2033 return( ret );
2034 }
2035 }
2036 else
Hanno Becker09d23642019-07-22 17:18:18 +01002037 {
2038 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
2039 return( ret );
2040 }
2041
2042 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
2043 }
2044 else
2045#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01002046#if defined(MBEDTLS_ECDH_C) && \
2047 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2048 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2049 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2050 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01002051 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2052 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2053 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2054 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2055 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2056 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2057 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2058 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2059 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002060 ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
Hanno Becker09d23642019-07-22 17:18:18 +01002061 &ssl->handshake->pmslen,
2062 ssl->handshake->premaster,
2063 MBEDTLS_MPI_MAX_SIZE,
2064 mbedtls_ssl_conf_get_frng( ssl->conf ),
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002065 mbedtls_ssl_conf_get_prng( ssl->conf ) );
2066 if( ret == 0 )
2067 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002068 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002069 if( ret == 0 )
2070 {
2071 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2072 }
2073 else
2074 {
2075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002076 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002077 }
2078 }
2079 else
Hanno Becker09d23642019-07-22 17:18:18 +01002080 {
2081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2082 return( ret );
2083 }
2084
2085 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2086 }
2087 else
2088#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2089 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2090 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2091 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2092#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2093 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2094 {
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002095 ret = mbedtls_ssl_psk_derive_premaster( ssl,
2096 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) );
2097 if( ret == 0 )
2098 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002099 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002100 if( ret == 0 )
2101 {
2102 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2103 }
2104 else
2105 {
2106 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002107 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002108 }
2109 }
2110 else
Hanno Becker09d23642019-07-22 17:18:18 +01002111 {
2112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2113 return( ret );
2114 }
2115 }
2116 else
2117#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2118#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2119 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2120 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2121 {
2122 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2123 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2124 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002125 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002126 if( ret == 0 )
2127 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02002128 mbedtls_platform_random_delay();
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002129 if( ret == 0 )
2130 {
2131 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2132 }
2133 else
2134 {
2135 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
Jarno Lamsa5aa4c072019-12-20 12:42:49 +02002136 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002137 }
2138 }
2139 else
Hanno Becker09d23642019-07-22 17:18:18 +01002140 {
2141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2142 return( ret );
2143 }
2144 }
2145 else
2146#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2147#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2148 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2149 == MBEDTLS_KEY_EXCHANGE_RSA )
2150 {
2151 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002152 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002153 * ssl_rsa_generate_partial_pms(). Only the
2154 * PMS length needs to be set. */
2155 ssl->handshake->pmslen = 48;
2156 }
2157 else
2158#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2162 }
2163
2164 return( 0 );
2165}
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2168int 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 +02002169{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002170 unsigned char *p = ssl->handshake->premaster;
2171 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002172 const unsigned char *psk = ssl->conf->psk;
2173 size_t psk_len = ssl->conf->psk_len;
2174
2175 /* If the psk callback was called, use its result */
2176 if( ssl->handshake->psk != NULL )
2177 {
2178 psk = ssl->handshake->psk;
2179 psk_len = ssl->handshake->psk_len;
2180 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002181
2182 /*
2183 * PMS = struct {
2184 * opaque other_secret<0..2^16-1>;
2185 * opaque psk<0..2^16-1>;
2186 * };
2187 * with "other_secret" depending on the particular key exchange
2188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2190 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002191 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002192 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002194
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002195 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002196
2197 if( end < p || (size_t)( end - p ) < psk_len )
2198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2199
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002200 mbedtls_platform_memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002201 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002202 }
2203 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2205#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2206 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002207 {
2208 /*
2209 * other_secret already set by the ClientKeyExchange message,
2210 * and is 48 bytes long
2211 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002212 if( end - p < 2 )
2213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2214
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002215 *p++ = 0;
2216 *p++ = 48;
2217 p += 48;
2218 }
2219 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2221#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2222 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002223 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002224 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002225 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002226
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002227 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002229 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002230 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002231 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002234 return( ret );
2235 }
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002236 p = mbedtls_platform_put_uint16_be( p, len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002237 p += len;
2238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002240 }
2241 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2243#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2244 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002245 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002246 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002247 size_t zlen;
2248
Hanno Becker982da7e2019-09-02 09:47:39 +01002249#if defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01002250 ret = uECC_shared_secret( ssl->handshake->ecdh_peerkey,
2251 ssl->handshake->ecdh_privkey,
2252 p + 2 );
2253 if( ret == UECC_FAULT_DETECTED )
2254 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
2255 if( ret != UECC_SUCCESS )
Hanno Becker982da7e2019-09-02 09:47:39 +01002256 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Hanno Becker982da7e2019-09-02 09:47:39 +01002257
2258 zlen = NUM_ECC_BYTES;
2259#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002261 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002262 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002263 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002266 return( ret );
2267 }
2268
Hanno Becker982da7e2019-09-02 09:47:39 +01002269 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2270 MBEDTLS_DEBUG_ECDH_Z );
2271#endif /* MBEDTLS_USE_TINYCRYPT */
2272
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002273 p = mbedtls_platform_put_uint16_be( p, zlen );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002274 p += zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002275 }
2276 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002281 }
2282
2283 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002284 if( end - p < 2 )
2285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002286
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03002287 p = mbedtls_platform_put_uint16_be( p, psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002288
2289 if( end < p || (size_t)( end - p ) < psk_len )
2290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2291
Teppo Järvelin91d79382019-10-02 09:09:31 +03002292 mbedtls_platform_memcpy( p, psk, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002293 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002294
2295 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2296
Jarno Lamsa67f0a1e2019-12-18 16:28:51 +02002297 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
2298
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002299 return( 0 );
2300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002304/*
2305 * SSLv3.0 MAC functions
2306 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002307#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002308static void ssl_mac( mbedtls_md_context_t *md_ctx,
2309 const unsigned char *secret,
2310 const unsigned char *buf, size_t len,
2311 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002312 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002313{
2314 unsigned char header[11];
2315 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002316 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2318 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002319
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002320 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002322 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002323 else
Paul Bakker68884e32013-01-07 18:20:04 +01002324 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
Teppo Järvelin91d79382019-10-02 09:09:31 +03002326 mbedtls_platform_memcpy( header, ctr, 8 );
Arto Kinnunen6e3f09b2019-09-06 17:37:01 +03002327 header[8] = (unsigned char) type;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002328 (void)mbedtls_platform_put_uint16_be( &header[9], len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002329
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002330 mbedtls_platform_memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 mbedtls_md_starts( md_ctx );
2332 mbedtls_md_update( md_ctx, secret, md_size );
2333 mbedtls_md_update( md_ctx, padding, padlen );
2334 mbedtls_md_update( md_ctx, header, 11 );
2335 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002336 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002338 mbedtls_platform_memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339 mbedtls_md_starts( md_ctx );
2340 mbedtls_md_update( md_ctx, secret, md_size );
2341 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002342 mbedtls_md_update( md_ctx, out, md_size );
2343 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002346
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002347/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002348 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002349#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002350 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2351 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2352 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2353/* This function makes sure every byte in the memory region is accessed
2354 * (in ascending addresses order) */
2355static void ssl_read_memory( unsigned char *p, size_t len )
2356{
2357 unsigned char acc = 0;
2358 volatile unsigned char force;
2359
2360 for( ; len != 0; p++, len-- )
2361 acc ^= *p;
2362
2363 force = acc;
2364 (void) force;
2365}
2366#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2367
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002368/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002370 */
Hanno Becker3307b532017-12-27 21:37:21 +00002371
Hanno Beckera5a2b082019-05-15 14:03:01 +01002372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002373/* This functions transforms a DTLS plaintext fragment and a record content
2374 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002375 *
2376 * struct {
2377 * opaque content[DTLSPlaintext.length];
2378 * ContentType real_type;
2379 * uint8 zeros[length_of_padding];
2380 * } DTLSInnerPlaintext;
2381 *
2382 * Input:
2383 * - `content`: The beginning of the buffer holding the
2384 * plaintext to be wrapped.
2385 * - `*content_size`: The length of the plaintext in Bytes.
2386 * - `max_len`: The number of Bytes available starting from
2387 * `content`. This must be `>= *content_size`.
2388 * - `rec_type`: The desired record content type.
2389 *
2390 * Output:
2391 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2392 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2393 *
2394 * Returns:
2395 * - `0` on success.
2396 * - A negative error code if `max_len` didn't offer enough space
2397 * for the expansion.
2398 */
2399static int ssl_cid_build_inner_plaintext( unsigned char *content,
2400 size_t *content_size,
2401 size_t remaining,
2402 uint8_t rec_type )
2403{
2404 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002405 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2406 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2407 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002408
2409 /* Write real content type */
2410 if( remaining == 0 )
2411 return( -1 );
2412 content[ len ] = rec_type;
2413 len++;
2414 remaining--;
2415
2416 if( remaining < pad )
2417 return( -1 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02002418 mbedtls_platform_memset( content + len, 0, pad );
Hanno Becker92c930f2019-04-29 17:31:37 +01002419 len += pad;
2420 remaining -= pad;
2421
2422 *content_size = len;
2423 return( 0 );
2424}
2425
Hanno Becker7dc25772019-05-20 15:08:01 +01002426/* This function parses a DTLSInnerPlaintext structure.
2427 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002428static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2429 size_t *content_size,
2430 uint8_t *rec_type )
2431{
2432 size_t remaining = *content_size;
2433
2434 /* Determine length of padding by skipping zeroes from the back. */
2435 do
2436 {
2437 if( remaining == 0 )
2438 return( -1 );
2439 remaining--;
2440 } while( content[ remaining ] == 0 );
2441
2442 *content_size = remaining;
2443 *rec_type = content[ remaining ];
2444
2445 return( 0 );
2446}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002447#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002448
Hanno Becker99abf512019-05-20 14:50:53 +01002449/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002450 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002451static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002452 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002453 mbedtls_record *rec )
2454{
Hanno Becker99abf512019-05-20 14:50:53 +01002455 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002456 *
2457 * additional_data = seq_num + TLSCompressed.type +
2458 * TLSCompressed.version + TLSCompressed.length;
2459 *
Hanno Becker99abf512019-05-20 14:50:53 +01002460 * For the CID extension, this is extended as follows
2461 * (quoting draft-ietf-tls-dtls-connection-id-05,
2462 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002463 *
2464 * additional_data = seq_num + DTLSPlaintext.type +
2465 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002466 * cid +
2467 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002468 * length_of_DTLSInnerPlaintext;
2469 */
2470
Teppo Järvelin91d79382019-10-02 09:09:31 +03002471 mbedtls_platform_memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002472 add_data[8] = rec->type;
Teppo Järvelin91d79382019-10-02 09:09:31 +03002473 mbedtls_platform_memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002474
Hanno Beckera5a2b082019-05-15 14:03:01 +01002475#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002476 if( rec->cid_len != 0 )
2477 {
Teppo Järvelin91d79382019-10-02 09:09:31 +03002478 mbedtls_platform_memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002479 add_data[11 + rec->cid_len + 0] = rec->cid_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002480 (void)mbedtls_platform_put_uint16_be( &add_data[11 + rec->cid_len + 1],
2481 rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002482 *add_data_len = 13 + 1 + rec->cid_len;
2483 }
2484 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002485#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002486 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03002487 (void)mbedtls_platform_put_uint16_be( &add_data[11], rec->data_len );
Hanno Becker1f02f052019-05-09 11:38:24 +01002488 *add_data_len = 13;
2489 }
Hanno Becker3307b532017-12-27 21:37:21 +00002490}
2491
Hanno Becker611a83b2018-01-03 14:27:32 +00002492int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2493 mbedtls_ssl_transform *transform,
2494 mbedtls_record *rec,
2495 int (*f_rng)(void *, unsigned char *, size_t),
2496 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002499 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002500 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002501 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002502 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002503 size_t post_avail;
2504
2505 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002506#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002507 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002508 ((void) ssl);
2509#endif
2510
2511 /* The PRNG is used for dynamic IV generation that's used
2512 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2513#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2514 ( defined(MBEDTLS_AES_C) || \
2515 defined(MBEDTLS_ARIA_C) || \
2516 defined(MBEDTLS_CAMELLIA_C) ) && \
2517 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2518 ((void) f_rng);
2519 ((void) p_rng);
2520#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Hanno Becker3307b532017-12-27 21:37:21 +00002524 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002525 {
Hanno Becker3307b532017-12-27 21:37:21 +00002526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2528 }
Hanno Becker505089d2019-05-01 09:45:57 +01002529 if( rec == NULL
2530 || rec->buf == NULL
2531 || rec->buf_len < rec->data_offset
2532 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002533#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002534 || rec->cid_len != 0
2535#endif
2536 )
Hanno Becker3307b532017-12-27 21:37:21 +00002537 {
2538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002540 }
2541
Hanno Becker3307b532017-12-27 21:37:21 +00002542 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002543 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002545 data, rec->data_len );
2546
2547 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2548
2549 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2550 {
2551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2552 (unsigned) rec->data_len,
2553 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2555 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002556
Hanno Beckera5a2b082019-05-15 14:03:01 +01002557#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002558 /*
2559 * Add CID information
2560 */
2561 rec->cid_len = transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03002562 /* Not using more secure mbedtls_platform_memcpy as cid is public */
2563 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
Hanno Beckere83efe62019-04-29 13:52:53 +01002564 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002565
2566 if( rec->cid_len != 0 )
2567 {
2568 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002569 * Wrap plaintext into DTLSInnerPlaintext structure.
2570 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002571 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002572 * Note that this changes `rec->data_len`, and hence
2573 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002574 */
2575 if( ssl_cid_build_inner_plaintext( data,
2576 &rec->data_len,
2577 post_avail,
2578 rec->type ) != 0 )
2579 {
2580 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2581 }
2582
2583 rec->type = MBEDTLS_SSL_MSG_CID;
2584 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002585#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002586
Hanno Becker92c930f2019-04-29 17:31:37 +01002587 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2588
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002590 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002591 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002592#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 if( mode == MBEDTLS_MODE_STREAM ||
2594 ( mode == MBEDTLS_MODE_CBC
2595#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002596 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002597#endif
2598 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 {
Hanno Becker3307b532017-12-27 21:37:21 +00002600 if( post_avail < transform->maclen )
2601 {
2602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2603 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2604 }
2605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002607 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2608 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002609 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002610 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002611 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2612 data, rec->data_len, rec->ctr, rec->type, mac );
Teppo Järvelin91d79382019-10-02 09:09:31 +03002613 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002614 }
2615 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2618 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002619 if( mbedtls_ssl_ver_geq(
2620 mbedtls_ssl_transform_get_minor_ver( transform ),
2621 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002622 {
Hanno Becker992b6872017-11-09 18:57:39 +00002623 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2624
Hanno Beckere83efe62019-04-29 13:52:53 +01002625 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002626
Hanno Becker3307b532017-12-27 21:37:21 +00002627 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002628 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002629 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2630 data, rec->data_len );
2631 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2632 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2633
Teppo Järvelin91d79382019-10-02 09:09:31 +03002634 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002635 }
2636 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002637#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002641 }
2642
Hanno Becker3307b532017-12-27 21:37:21 +00002643 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2644 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002645
Hanno Becker3307b532017-12-27 21:37:21 +00002646 rec->data_len += transform->maclen;
2647 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002648 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002649 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002650#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002651
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002652 /*
2653 * Encrypt
2654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2656 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002658 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002659 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002661 "including %d bytes of padding",
2662 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Hanno Becker3307b532017-12-27 21:37:21 +00002664 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2665 transform->iv_enc, transform->ivlen,
2666 data, rec->data_len,
2667 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002670 return( ret );
2671 }
2672
Hanno Becker3307b532017-12-27 21:37:21 +00002673 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 }
Paul Bakker68884e32013-01-07 18:20:04 +01002679 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002681
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002682#if defined(MBEDTLS_GCM_C) || \
2683 defined(MBEDTLS_CCM_C) || \
2684 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002686 mode == MBEDTLS_MODE_CCM ||
2687 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002688 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002689 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002690 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002691 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002692
Hanno Becker3307b532017-12-27 21:37:21 +00002693 /* Check that there's space for both the authentication tag
2694 * and the explicit IV before and after the record content. */
2695 if( post_avail < transform->taglen ||
2696 rec->data_offset < explicit_iv_len )
2697 {
2698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2699 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2700 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002701
Paul Bakker68884e32013-01-07 18:20:04 +01002702 /*
2703 * Generate IV
2704 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002705 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2706 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002707 /* GCM and CCM: fixed || explicit (=seqnum) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002708 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2709 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, rec->ctr,
Hanno Becker3307b532017-12-27 21:37:21 +00002710 explicit_iv_len );
2711 /* Prefix record content with explicit IV. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002712 mbedtls_platform_memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002713 }
2714 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2715 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002716 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002717 unsigned char i;
2718
Teppo Järvelin91d79382019-10-02 09:09:31 +03002719 mbedtls_platform_memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002720
2721 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002722 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002723 }
2724 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002725 {
2726 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2728 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002729 }
2730
Hanno Beckere83efe62019-04-29 13:52:53 +01002731 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002732
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002733 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2734 iv, transform->ivlen );
2735 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002736 data - explicit_iv_len, explicit_iv_len );
2737 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002738 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002740 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002741 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002742
Paul Bakker68884e32013-01-07 18:20:04 +01002743 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002744 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002745 */
Hanno Becker3307b532017-12-27 21:37:21 +00002746
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002747 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002748 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002749 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002750 data, rec->data_len, /* source */
2751 data, &rec->data_len, /* destination */
2752 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002755 return( ret );
2756 }
2757
Hanno Becker3307b532017-12-27 21:37:21 +00002758 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2759 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002760
Hanno Becker3307b532017-12-27 21:37:21 +00002761 rec->data_len += transform->taglen + explicit_iv_len;
2762 rec->data_offset -= explicit_iv_len;
2763 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002764 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002765 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2768#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002769 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002772 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002773 size_t padlen, i;
2774 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002775
Hanno Becker3307b532017-12-27 21:37:21 +00002776 /* Currently we're always using minimal padding
2777 * (up to 255 bytes would be allowed). */
2778 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2779 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 padlen = 0;
2781
Hanno Becker3307b532017-12-27 21:37:21 +00002782 /* Check there's enough space in the buffer for the padding. */
2783 if( post_avail < padlen + 1 )
2784 {
2785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2786 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2787 }
2788
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002790 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Hanno Becker3307b532017-12-27 21:37:21 +00002792 rec->data_len += padlen + 1;
2793 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002796 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002797 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2798 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002800 if( mbedtls_ssl_ver_geq(
2801 mbedtls_ssl_transform_get_minor_ver( transform ),
2802 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 {
Hanno Becker3307b532017-12-27 21:37:21 +00002804 if( f_rng == NULL )
2805 {
2806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2808 }
2809
2810 if( rec->data_offset < transform->ivlen )
2811 {
2812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2813 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2814 }
2815
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002816 /*
2817 * Generate IV
2818 */
Hanno Becker3307b532017-12-27 21:37:21 +00002819 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002820 if( ret != 0 )
2821 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002822
Teppo Järvelin91d79382019-10-02 09:09:31 +03002823 mbedtls_platform_memcpy( data - transform->ivlen, transform->iv_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002824 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002825
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002830 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002831 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002832 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002833
Hanno Becker3307b532017-12-27 21:37:21 +00002834 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2835 transform->iv_enc,
2836 transform->ivlen,
2837 data, rec->data_len,
2838 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002841 return( ret );
2842 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002843
Hanno Becker3307b532017-12-27 21:37:21 +00002844 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2847 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002848 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01002851 if( mbedtls_ssl_ver_lt(
2852 mbedtls_ssl_transform_get_minor_ver( transform ),
2853 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02002854 {
2855 /*
2856 * Save IV in SSL3 and TLS1
2857 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03002858 mbedtls_platform_memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
Hanno Becker3307b532017-12-27 21:37:21 +00002859 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 }
Hanno Becker3307b532017-12-27 21:37:21 +00002861 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002862#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002863 {
2864 data -= transform->ivlen;
2865 rec->data_offset -= transform->ivlen;
2866 rec->data_len += transform->ivlen;
2867 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002870 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002871 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002872 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2873
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002874 /*
2875 * MAC(MAC_write_key, seq_num +
2876 * TLSCipherText.type +
2877 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002878 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002879 * IV + // except for TLS 1.0
2880 * ENC(content + padding + padding_length));
2881 */
Hanno Becker3307b532017-12-27 21:37:21 +00002882
2883 if( post_avail < transform->maclen)
2884 {
2885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2886 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2887 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002888
Hanno Beckere83efe62019-04-29 13:52:53 +01002889 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002892 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002893 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002894
Hanno Becker3307b532017-12-27 21:37:21 +00002895 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002896 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002897 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2898 data, rec->data_len );
2899 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2900 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002901
Teppo Järvelin91d79382019-10-02 09:09:31 +03002902 mbedtls_platform_memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002903
Hanno Becker3307b532017-12-27 21:37:21 +00002904 rec->data_len += transform->maclen;
2905 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002906 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002907 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002909 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002912 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2915 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002916 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002917
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002918 /* Make extra sure authentication was performed, exactly once */
2919 if( auth_done != 1 )
2920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002923 }
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
2927 return( 0 );
2928}
2929
Hanno Becker40478be2019-07-12 08:23:59 +01002930int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002931 mbedtls_ssl_transform *transform,
2932 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002933{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002934 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002936 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002937#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002938 size_t padlen = 0, correct = 1;
2939#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002940 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002941 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002942 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002943
Hanno Becker611a83b2018-01-03 14:27:32 +00002944#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002945 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002946 ((void) ssl);
2947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002950 if( rec == NULL ||
2951 rec->buf == NULL ||
2952 rec->buf_len < rec->data_offset ||
2953 rec->buf_len - rec->data_offset < rec->data_len )
2954 {
2955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002957 }
2958
Hanno Becker4c6876b2017-12-27 21:28:58 +00002959 data = rec->buf + rec->data_offset;
2960 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Hanno Beckera5a2b082019-05-15 14:03:01 +01002962#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002963 /*
2964 * Match record's CID with incoming CID.
2965 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002966 if( rec->cid_len != transform->in_cid_len ||
Teppo Järvelin0efac532019-10-04 13:21:08 +03002967 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) // use regular memcmp as CID is public
Hanno Beckerabd7c892019-05-08 13:02:22 +01002968 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002969 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002970 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002971#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2974 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002975 {
2976 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002977 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2978 transform->iv_dec,
2979 transform->ivlen,
2980 data, rec->data_len,
2981 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002984 return( ret );
2985 }
2986
Hanno Becker4c6876b2017-12-27 21:28:58 +00002987 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002991 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 }
Paul Bakker68884e32013-01-07 18:20:04 +01002993 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002995#if defined(MBEDTLS_GCM_C) || \
2996 defined(MBEDTLS_CCM_C) || \
2997 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002999 mode == MBEDTLS_MODE_CCM ||
3000 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003001 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003002 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003003 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003004
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003005 /*
3006 * Prepare IV from explicit and implicit data.
3007 */
3008
3009 /* Check that there's enough space for the explicit IV
3010 * (at the beginning of the record) and the MAC (at the
3011 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003012 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003015 "+ taglen (%d)", rec->data_len,
3016 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02003018 }
Paul Bakker68884e32013-01-07 18:20:04 +01003019
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003020#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003021 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
3022 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003023 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01003024
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003025 /* Fixed */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003026 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003027 /* Explicit */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003028 mbedtls_platform_memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003029 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003030 else
3031#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3032#if defined(MBEDTLS_CHACHAPOLY_C)
3033 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003034 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02003035 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003036 unsigned char i;
3037
Teppo Järvelin91d79382019-10-02 09:09:31 +03003038 mbedtls_platform_memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003039
3040 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003041 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003042 }
3043 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003044#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003045 {
3046 /* Reminder if we ever add an AEAD mode with a different size */
3047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3049 }
3050
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003051 /* Group changes to data, data_len, and add_data, because
3052 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003053 data += explicit_iv_len;
3054 rec->data_offset += explicit_iv_len;
3055 rec->data_len -= explicit_iv_len + transform->taglen;
3056
Hanno Beckere83efe62019-04-29 13:52:53 +01003057 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003058 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01003059 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003060
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003061 /* Because of the check above, we know that there are
3062 * explicit_iv_len Bytes preceeding data, and taglen
3063 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01003064 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003065 * mbedtls_cipher_auth_decrypt() below. */
3066
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02003067 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003068 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00003069 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01003070
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003071 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003072 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003073 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003074 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
3075 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01003076 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003077 data, rec->data_len,
3078 data, &olen,
3079 data + rec->data_len,
3080 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00003081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
3085 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02003086
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003087 return( ret );
3088 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003089 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003090
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003091 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003092 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02003096 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00003097 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
3100#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003101 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01003104 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003105
Paul Bakker5121ce52009-01-03 21:22:43 +00003106 /*
Paul Bakker45829992013-01-03 14:52:21 +01003107 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003110 if( mbedtls_ssl_ver_geq(
3111 mbedtls_ssl_transform_get_minor_ver( transform ),
3112 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003113 {
3114 /* The ciphertext is prefixed with the CBC IV. */
3115 minlen += transform->ivlen;
3116 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003117#endif
Paul Bakker45829992013-01-03 14:52:21 +01003118
Hanno Becker4c6876b2017-12-27 21:28:58 +00003119 /* Size considerations:
3120 *
3121 * - The CBC cipher text must not be empty and hence
3122 * at least of size transform->ivlen.
3123 *
3124 * Together with the potential IV-prefix, this explains
3125 * the first of the two checks below.
3126 *
3127 * - The record must contain a MAC, either in plain or
3128 * encrypted, depending on whether Encrypt-then-MAC
3129 * is used or not.
3130 * - If it is, the message contains the IV-prefix,
3131 * the CBC ciphertext, and the MAC.
3132 * - If it is not, the padded plaintext, and hence
3133 * the CBC ciphertext, has at least length maclen + 1
3134 * because there is at least the padding length byte.
3135 *
3136 * As the CBC ciphertext is not empty, both cases give the
3137 * lower bound minlen + maclen + 1 on the record size, which
3138 * we test for in the second check below.
3139 */
3140 if( rec->data_len < minlen + transform->ivlen ||
3141 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003144 "+ 1 ) ( + expl IV )", rec->data_len,
3145 transform->ivlen,
3146 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003148 }
3149
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003150 /*
3151 * Authenticate before decrypt if enabled
3152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003154 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003155 {
Hanno Becker992b6872017-11-09 18:57:39 +00003156 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003159
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003160 /* Update data_len in tandem with add_data.
3161 *
3162 * The subtraction is safe because of the previous check
3163 * data_len >= minlen + maclen + 1.
3164 *
3165 * Afterwards, we know that data + data_len is followed by at
3166 * least maclen Bytes, which justifies the call to
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003167 * mbedtls_platform_memcmp() below.
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003168 *
3169 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003170 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003171 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003172
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003173 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003174 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3175 add_data_len );
3176 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3177 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003178 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3179 data, rec->data_len );
3180 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3181 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003182
Hanno Becker4c6876b2017-12-27 21:28:58 +00003183 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3184 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003185 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003186 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003187
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003188 /* Compare expected MAC with MAC at the end of the record. */
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003189 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003190 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003194 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003195 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003196 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003198
3199 /*
3200 * Check length sanity
3201 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003202
3203 /* We know from above that data_len > minlen >= 0,
3204 * so the following check in particular implies that
3205 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003206 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003209 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003211 }
3212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003214 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003215 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003216 */
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003217 if( mbedtls_ssl_ver_geq(
3218 mbedtls_ssl_transform_get_minor_ver( transform ),
3219 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003220 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003221 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003222 mbedtls_platform_memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003223
Hanno Becker4c6876b2017-12-27 21:28:58 +00003224 data += transform->ivlen;
3225 rec->data_offset += transform->ivlen;
3226 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003229
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003230 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3231
Hanno Becker4c6876b2017-12-27 21:28:58 +00003232 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3233 transform->iv_dec, transform->ivlen,
3234 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003237 return( ret );
3238 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003239
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003240 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003241 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3244 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003245 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003248 if( mbedtls_ssl_ver_lt(
3249 mbedtls_ssl_transform_get_minor_ver( transform ),
3250 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Paul Bakkercca5b812013-08-31 17:40:26 +02003251 {
3252 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003253 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3254 * records is equivalent to CBC decryption of the concatenation
3255 * of the records; in other words, IVs are maintained across
3256 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003257 */
Teppo Järvelin91d79382019-10-02 09:09:31 +03003258 mbedtls_platform_memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003259 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003261#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003262
Hanno Becker4c6876b2017-12-27 21:28:58 +00003263 /* Safe since data_len >= minlen + maclen + 1, so after having
3264 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003265 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3266 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003267 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003268
Hanno Becker4c6876b2017-12-27 21:28:58 +00003269 if( auth_done == 1 )
3270 {
3271 correct *= ( rec->data_len >= padlen + 1 );
3272 padlen *= ( rec->data_len >= padlen + 1 );
3273 }
3274 else
Paul Bakker45829992013-01-03 14:52:21 +01003275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003277 if( rec->data_len < transform->maclen + padlen + 1 )
3278 {
3279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3280 rec->data_len,
3281 transform->maclen,
3282 padlen + 1 ) );
3283 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003284#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003285
3286 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3287 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003289
Hanno Becker4c6876b2017-12-27 21:28:58 +00003290 padlen++;
3291
3292 /* Regardless of the validity of the padding,
3293 * we have data_len >= padlen here. */
3294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003296 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3297 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003299 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#if defined(MBEDTLS_SSL_DEBUG_ALL)
3302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003303 "should be no more than %d",
3304 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003305#endif
Paul Bakker45829992013-01-03 14:52:21 +01003306 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 }
3308 }
3309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3311#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3312 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003313 if( mbedtls_ssl_ver_gt(
3314 mbedtls_ssl_transform_get_minor_ver( transform ),
3315 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003316 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003317 /* The padding check involves a series of up to 256
3318 * consecutive memory reads at the end of the record
3319 * plaintext buffer. In order to hide the length and
3320 * validity of the padding, always perform exactly
3321 * `min(256,plaintext_len)` reads (but take into account
3322 * only the last `padlen` bytes for the padding check). */
3323 size_t pad_count = 0;
3324 size_t real_count = 0;
3325 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003326
Hanno Becker4c6876b2017-12-27 21:28:58 +00003327 /* Index of first padding byte; it has been ensured above
3328 * that the subtraction is safe. */
3329 size_t const padding_idx = rec->data_len - padlen;
3330 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3331 size_t const start_idx = rec->data_len - num_checks;
3332 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003333
Hanno Becker4c6876b2017-12-27 21:28:58 +00003334 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003335 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003336 real_count |= ( idx >= padding_idx );
3337 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003338 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003339 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003342 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003344#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003345 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003347 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3349 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3352 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003353 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003354
Hanno Becker4c6876b2017-12-27 21:28:58 +00003355 /* If the padding was found to be invalid, padlen == 0
3356 * and the subtraction is safe. If the padding was found valid,
3357 * padlen hasn't been changed and the previous assertion
3358 * data_len >= padlen still holds. */
3359 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003361 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003363 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003368
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003369#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003371 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003372#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
3374 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003375 * Authenticate if not done yet.
3376 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003378#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003379 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003380 {
Hanno Becker992b6872017-11-09 18:57:39 +00003381 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003382
Hanno Becker4c6876b2017-12-27 21:28:58 +00003383 /* If the initial value of padlen was such that
3384 * data_len < maclen + padlen + 1, then padlen
3385 * got reset to 1, and the initial check
3386 * data_len >= minlen + maclen + 1
3387 * guarantees that at this point we still
3388 * have at least data_len >= maclen.
3389 *
3390 * If the initial value of padlen was such that
3391 * data_len >= maclen + padlen + 1, then we have
3392 * subtracted either padlen + 1 (if the padding was correct)
3393 * or 0 (if the padding was incorrect) since then,
3394 * hence data_len >= maclen in any case.
3395 */
3396 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003397 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003400 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3401 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003402 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003403 ssl_mac( &transform->md_ctx_dec,
3404 transform->mac_dec,
3405 data, rec->data_len,
3406 rec->ctr, rec->type,
3407 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003408 }
3409 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3411#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3412 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01003413 if( mbedtls_ssl_ver_gt(
3414 mbedtls_ssl_transform_get_minor_ver( transform ),
3415 MBEDTLS_SSL_MINOR_VERSION_0 ) )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003416 {
3417 /*
3418 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003419 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003420 *
3421 * Known timing attacks:
3422 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3423 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003424 * To compensate for different timings for the MAC calculation
3425 * depending on how much padding was removed (which is determined
3426 * by padlen), process extra_run more blocks through the hash
3427 * function.
3428 *
3429 * The formula in the paper is
3430 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3431 * where L1 is the size of the header plus the decrypted message
3432 * plus CBC padding and L2 is the size of the header plus the
3433 * decrypted message. This is for an underlying hash function
3434 * with 64-byte blocks.
3435 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3436 * correctly. We round down instead of up, so -56 is the correct
3437 * value for our calculations instead of -55.
3438 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003439 * Repeat the formula rather than defining a block_size variable.
3440 * This avoids requiring division by a variable at runtime
3441 * (which would be marginally less efficient and would require
3442 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003443 */
3444 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003445 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003446
3447 /*
3448 * The next two sizes are the minimum and maximum values of
3449 * in_msglen over all padlen values.
3450 *
3451 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003452 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003453 *
3454 * Note that max_len + maclen is never more than the buffer
3455 * length, as we previously did in_msglen -= maclen too.
3456 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003457 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003458 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3459
Hanno Becker4c6876b2017-12-27 21:28:58 +00003460 memset( tmp, 0, sizeof( tmp ) );
3461
Hanno Beckera5cedbc2019-07-17 11:21:02 +01003462 switch( mbedtls_md_get_type(
3463 mbedtls_md_get_handle( &transform->md_ctx_dec ) ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003464 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003465#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3466 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003467 case MBEDTLS_MD_MD5:
3468 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003469 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003470 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003471 extra_run =
3472 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3473 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003474 break;
3475#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003476#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003477 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003478 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003479 extra_run =
3480 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3481 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003482 break;
3483#endif
3484 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003486 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3487 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003488
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003489 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003490
Hanno Beckere83efe62019-04-29 13:52:53 +01003491 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3492 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003493 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3494 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003495 /* Make sure we access everything even when padlen > 0. This
3496 * makes the synchronisation requirements for just-in-time
3497 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003498 ssl_read_memory( data + rec->data_len, padlen );
3499 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003500
3501 /* Call mbedtls_md_process at least once due to cache attacks
3502 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003503 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003504 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003505
Hanno Becker4c6876b2017-12-27 21:28:58 +00003506 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003507
3508 /* Make sure we access all the memory that could contain the MAC,
3509 * before we check it in the next code block. This makes the
3510 * synchronisation requirements for just-in-time Prime+Probe
3511 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003512 ssl_read_memory( data + min_len,
3513 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003514 }
3515 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3517 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003522
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003523#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003524 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3525 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
Teppo Järvelin707ceb82019-10-04 07:49:39 +03003528 if( mbedtls_platform_memcmp( data + rec->data_len, mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003529 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003531#if defined(MBEDTLS_SSL_DEBUG_ALL)
3532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003533#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003534 correct = 0;
3535 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003536 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003537 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003538
3539 /*
3540 * Finally check the correct flag
3541 */
3542 if( correct == 0 )
3543 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003544#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003545
3546 /* Make extra sure authentication was performed, exactly once */
3547 if( auth_done != 1 )
3548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Hanno Beckera5a2b082019-05-15 14:03:01 +01003553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003554 if( rec->cid_len != 0 )
3555 {
3556 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3557 &rec->type );
3558 if( ret != 0 )
3559 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3560 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003561#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003564
3565 return( 0 );
3566}
3567
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003568#undef MAC_NONE
3569#undef MAC_PLAINTEXT
3570#undef MAC_CIPHERTEXT
3571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003573/*
3574 * Compression/decompression functions
3575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003577{
3578 int ret;
3579 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003580 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003581 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003582 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003585
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003586 if( len_pre == 0 )
3587 return( 0 );
3588
Teppo Järvelin91d79382019-10-02 09:09:31 +03003589 mbedtls_platform_memcpy( msg_pre, ssl->out_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003592 ssl->out_msglen ) );
3593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003595 ssl->out_msg, ssl->out_msglen );
3596
Paul Bakker48916f92012-09-16 19:57:18 +00003597 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3598 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3599 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003600 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003601
Paul Bakker48916f92012-09-16 19:57:18 +00003602 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003603 if( ret != Z_OK )
3604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3606 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003607 }
3608
Angus Grattond8213d02016-05-25 20:56:48 +10003609 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003610 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003613 ssl->out_msglen ) );
3614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003616 ssl->out_msg, ssl->out_msglen );
3617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003619
3620 return( 0 );
3621}
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003624{
3625 int ret;
3626 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003627 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003628 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003629 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003632
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003633 if( len_pre == 0 )
3634 return( 0 );
3635
Teppo Järvelin91d79382019-10-02 09:09:31 +03003636 mbedtls_platform_memcpy( msg_pre, ssl->in_msg, len_pre );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003639 ssl->in_msglen ) );
3640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003642 ssl->in_msg, ssl->in_msglen );
3643
Paul Bakker48916f92012-09-16 19:57:18 +00003644 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3645 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3646 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003647 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003648 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003649
Paul Bakker48916f92012-09-16 19:57:18 +00003650 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003651 if( ret != Z_OK )
3652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3654 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003655 }
3656
Angus Grattond8213d02016-05-25 20:56:48 +10003657 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003658 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003661 ssl->in_msglen ) );
3662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003664 ssl->in_msg, ssl->in_msglen );
3665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003667
3668 return( 0 );
3669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003670#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3673static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675#if defined(MBEDTLS_SSL_PROTO_DTLS)
3676static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003677{
3678 /* If renegotiation is not enforced, retransmit until we would reach max
3679 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003680 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003681 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003682 uint32_t ratio =
3683 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3684 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003685 unsigned char doublings = 1;
3686
3687 while( ratio != 0 )
3688 {
3689 ++doublings;
3690 ratio >>= 1;
3691 }
3692
3693 if( ++ssl->renego_records_seen > doublings )
3694 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003696 return( 0 );
3697 }
3698 }
3699
3700 return( ssl_write_hello_request( ssl ) );
3701}
3702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003704
Paul Bakker5121ce52009-01-03 21:22:43 +00003705/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003706 * Fill the input message buffer by appending data to it.
3707 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003708 *
3709 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3710 * available (from this read and/or a previous one). Otherwise, an error code
3711 * is returned (possibly EOF or WANT_READ).
3712 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003713 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3714 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3715 * since we always read a whole datagram at once.
3716 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003717 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003718 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003721{
Paul Bakker23986e52011-04-24 08:57:21 +00003722 int ret;
3723 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
Hanno Beckera58a8962019-06-13 16:11:15 +01003727 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3728 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003731 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003733 }
3734
Angus Grattond8213d02016-05-25 20:56:48 +10003735 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003739 }
3740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003742 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003744 uint32_t timeout;
3745
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003746 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003747 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3748 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003749 {
3750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3751 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3753 }
3754
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003755 /*
3756 * The point is, we need to always read a full datagram at once, so we
3757 * sometimes read more then requested, and handle the additional data.
3758 * It could be the rest of the current record (while fetching the
3759 * header) and/or some other records in the same datagram.
3760 */
3761
3762 /*
3763 * Move to the next record in the already read datagram if applicable
3764 */
3765 if( ssl->next_record_offset != 0 )
3766 {
3767 if( ssl->in_left < ssl->next_record_offset )
3768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003771 }
3772
3773 ssl->in_left -= ssl->next_record_offset;
3774
3775 if( ssl->in_left != 0 )
3776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003778 ssl->next_record_offset ) );
3779 memmove( ssl->in_hdr,
3780 ssl->in_hdr + ssl->next_record_offset,
3781 ssl->in_left );
3782 }
3783
3784 ssl->next_record_offset = 0;
3785 }
3786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003789
3790 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003791 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003792 */
3793 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003796 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003797 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003798
3799 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003800 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003801 * are not at the beginning of a new record, the caller did something
3802 * wrong.
3803 */
3804 if( ssl->in_left != 0 )
3805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003808 }
3809
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003810 /*
3811 * Don't even try to read if time's out already.
3812 * This avoids by-passing the timer when repeatedly receiving messages
3813 * that will end up being dropped.
3814 */
3815 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003816 {
3817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003818 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003819 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003820 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003821 {
Angus Grattond8213d02016-05-25 20:56:48 +10003822 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003824 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003825 timeout = ssl->handshake->retransmit_timeout;
3826 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003827 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003830
Hanno Beckera58a8962019-06-13 16:11:15 +01003831 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3832 {
3833 ret = mbedtls_ssl_get_recv_timeout( ssl )
3834 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3835 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003836 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003837 {
3838 ret = mbedtls_ssl_get_recv( ssl )
3839 ( ssl->p_bio, ssl->in_hdr, len );
3840 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003843
3844 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003846 }
3847
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003848 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003851 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003854 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003855 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003858 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003859 }
3860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003864 return( ret );
3865 }
3866
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003867 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003868 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003870 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3871 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003873 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003874 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003877 return( ret );
3878 }
3879
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003880 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003881 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003883 }
3884
Paul Bakker5121ce52009-01-03 21:22:43 +00003885 if( ret < 0 )
3886 return( ret );
3887
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003888 ssl->in_left = ret;
3889 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003890 MBEDTLS_SSL_TRANSPORT_ELSE
3891#endif /* MBEDTLS_SSL_PROTO_DTLS */
3892#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003895 ssl->in_left, nb_want ) );
3896
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003897 while( ssl->in_left < nb_want )
3898 {
3899 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003900
3901 if( ssl_check_timer( ssl ) != 0 )
3902 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3903 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003904 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003905 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003906 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003907 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3908 ssl->in_hdr + ssl->in_left, len,
3909 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003910 }
3911 else
3912 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003913 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003914 ssl->in_hdr + ssl->in_left, len );
3915 }
3916 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003919 ssl->in_left, nb_want ) );
3920 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003921
3922 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003924
3925 if( ret < 0 )
3926 return( ret );
3927
mohammad160352aecb92018-03-28 23:41:40 -07003928 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003929 {
Darryl Green11999bb2018-03-13 15:22:58 +00003930 MBEDTLS_SSL_DEBUG_MSG( 1,
3931 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003932 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3934 }
3935
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003936 ssl->in_left += ret;
3937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003938 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003939#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
3943 return( 0 );
3944}
3945
3946/*
3947 * Flush any data not yet written
3948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003950{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003951 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003952 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003955
Hanno Beckera58a8962019-06-13 16:11:15 +01003956 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003959 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003961 }
3962
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003963 /* Avoid incrementing counter if data is flushed */
3964 if( ssl->out_left == 0 )
3965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003967 return( 0 );
3968 }
3969
Paul Bakker5121ce52009-01-03 21:22:43 +00003970 while( ssl->out_left > 0 )
3971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003973 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003974
Hanno Becker2b1e3542018-08-06 11:19:13 +01003975 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003976 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003979
3980 if( ret <= 0 )
3981 return( ret );
3982
mohammad160352aecb92018-03-28 23:41:40 -07003983 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003984 {
Darryl Green11999bb2018-03-13 15:22:58 +00003985 MBEDTLS_SSL_DEBUG_MSG( 1,
3986 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003987 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3989 }
3990
Paul Bakker5121ce52009-01-03 21:22:43 +00003991 ssl->out_left -= ret;
3992 }
3993
Hanno Becker2b1e3542018-08-06 11:19:13 +01003994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003995 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003996 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003997 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003998 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003999 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01004000#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004001#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01004002 {
4003 ssl->out_hdr = ssl->out_buf + 8;
4004 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004005#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01004006 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004009
4010 return( 0 );
4011}
4012
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004013/*
4014 * Functions to handle the DTLS retransmission state machine
4015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004017/*
4018 * Append current handshake message to current outgoing flight
4019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004021{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01004023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
4024 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
4025 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004026
4027 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004028 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004029 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004032 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033 }
4034
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02004035 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004036 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02004037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02004039 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004040 }
4041
4042 /* Copy current handshake message with headers */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004043 mbedtls_platform_memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004044 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004045 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004046 msg->next = NULL;
4047
4048 /* Append to the current flight */
4049 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004050 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004051 else
4052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004054 while( cur->next != NULL )
4055 cur = cur->next;
4056 cur->next = msg;
4057 }
4058
Hanno Becker3b235902018-08-06 09:54:53 +01004059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004060 return( 0 );
4061}
4062
4063/*
4064 * Free the current flight of handshake messages
4065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068 mbedtls_ssl_flight_item *cur = flight;
4069 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004070
4071 while( cur != NULL )
4072 {
4073 next = cur->next;
4074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004075 mbedtls_free( cur->p );
4076 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004077
4078 cur = next;
4079 }
4080}
4081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4083static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004084#endif
4085
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004086/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004087 * Swap transform_out and out_ctr with the alternative ones
4088 */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004089static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004092 unsigned char tmp_out_ctr[8];
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004093#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4094 int ret;
4095#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004096
4097 if( ssl->transform_out == ssl->handshake->alt_transform_out )
4098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004100 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004101 }
4102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004104
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004105 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004106 tmp_transform = ssl->transform_out;
4107 ssl->transform_out = ssl->handshake->alt_transform_out;
4108 ssl->handshake->alt_transform_out = tmp_transform;
4109
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004110 /* Swap epoch + sequence_number */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004111 mbedtls_platform_memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
4112 mbedtls_platform_memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
4113 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004114
4115 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004116 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4119 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4124 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004125 }
4126 }
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004127#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
4128
4129 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004130}
4131
4132/*
4133 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004134 */
4135int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4136{
4137 int ret = 0;
4138
4139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4140
4141 ret = mbedtls_ssl_flight_transmit( ssl );
4142
4143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4144
4145 return( ret );
4146}
4147
4148/*
4149 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004150 *
4151 * Need to remember the current message in case flush_output returns
4152 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004153 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004154 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004155int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004156{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004157 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004161 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004163
4164 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004165 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004166 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4167 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004170 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004171
4172 while( ssl->handshake->cur_msg != NULL )
4173 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004174 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004175 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004176
Hanno Beckere1dcb032018-08-17 16:47:58 +01004177 int const is_finished =
4178 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4179 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4180
Hanno Becker04da1892018-08-14 13:22:10 +01004181 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4182 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4183
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004184 /* Swap epochs before sending Finished: we can't do it after
4185 * sending ChangeCipherSpec, in case write returns WANT_READ.
4186 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004187 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004188 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004190 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4191 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004192 }
4193
Hanno Becker67bc7c32018-08-06 11:33:50 +01004194 ret = ssl_get_remaining_payload_in_datagram( ssl );
4195 if( ret < 0 )
4196 return( ret );
4197 max_frag_len = (size_t) ret;
4198
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004199 /* CCS is copied as is, while HS messages may need fragmentation */
4200 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4201 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004202 if( max_frag_len == 0 )
4203 {
4204 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4205 return( ret );
4206
4207 continue;
4208 }
4209
Teppo Järvelin91d79382019-10-02 09:09:31 +03004210 mbedtls_platform_memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004211 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004212 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004213
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004214 /* Update position inside current message */
4215 ssl->handshake->cur_msg_p += cur->len;
4216 }
4217 else
4218 {
4219 const unsigned char * const p = ssl->handshake->cur_msg_p;
4220 const size_t hs_len = cur->len - 12;
4221 const size_t frag_off = p - ( cur->p + 12 );
4222 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004223 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004224
Hanno Beckere1dcb032018-08-17 16:47:58 +01004225 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004226 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004227 if( is_finished )
Andrzej Kurek825ebd42020-05-18 11:47:25 -04004228 {
4229 if( ( ret = ssl_swap_epochs( ssl ) ) != 0 )
4230 return( ret );
4231 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004232
Hanno Becker67bc7c32018-08-06 11:33:50 +01004233 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4234 return( ret );
4235
4236 continue;
4237 }
4238 max_hs_frag_len = max_frag_len - 12;
4239
4240 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4241 max_hs_frag_len : rem_len;
4242
4243 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004244 {
4245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004246 (unsigned) cur_hs_frag_len,
4247 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004248 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004249
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004250 /* Messages are stored with handshake headers as if not fragmented,
4251 * copy beginning of headers then fill fragmentation fields.
4252 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004253 mbedtls_platform_memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004254
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004255 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[6], frag_off );
4256 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[9],
4257 cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004258
4259 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4260
Hanno Becker3f7b9732018-08-28 09:53:25 +01004261 /* Copy the handshake message content and set records fields */
Teppo Järvelin91d79382019-10-02 09:09:31 +03004262 mbedtls_platform_memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004263 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004264 ssl->out_msgtype = cur->type;
4265
4266 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004267 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004268 }
4269
4270 /* If done with the current message move to the next one if any */
4271 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4272 {
4273 if( cur->next != NULL )
4274 {
4275 ssl->handshake->cur_msg = cur->next;
4276 ssl->handshake->cur_msg_p = cur->next->p + 12;
4277 }
4278 else
4279 {
4280 ssl->handshake->cur_msg = NULL;
4281 ssl->handshake->cur_msg_p = NULL;
4282 }
4283 }
4284
4285 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004286 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004289 return( ret );
4290 }
4291 }
4292
Hanno Becker67bc7c32018-08-06 11:33:50 +01004293 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4294 return( ret );
4295
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004296 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4298 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004299 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004302 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4303 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004304
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004306
4307 return( 0 );
4308}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004309
4310/*
4311 * To be called when the last message of an incoming flight is received.
4312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004314{
4315 /* We won't need to resend that one any more */
4316 ssl_flight_free( ssl->handshake->flight );
4317 ssl->handshake->flight = NULL;
4318 ssl->handshake->cur_msg = NULL;
4319
4320 /* The next incoming flight will start with this msg_seq */
4321 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4322
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004323 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004324 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004325
Hanno Becker0271f962018-08-16 13:23:47 +01004326 /* Clear future message buffering structure. */
4327 ssl_buffering_free( ssl );
4328
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004329 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004330 ssl_set_timer( ssl, 0 );
4331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004332 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4333 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004336 }
4337 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004339}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004340
4341/*
4342 * To be called when the last message of an outgoing flight is send.
4343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004345{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004346 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004347 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4350 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004352 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004353 }
4354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004358
Paul Bakker5121ce52009-01-03 21:22:43 +00004359/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004360 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004361 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004362
4363/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004364 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004365 *
4366 * - fill in handshake headers
4367 * - update handshake checksum
4368 * - DTLS: save message for resending
4369 * - then pass to the record layer
4370 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004371 * DTLS: except for HelloRequest, messages are only queued, and will only be
4372 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004373 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004374 * Inputs:
4375 * - ssl->out_msglen: 4 + actual handshake message len
4376 * (4 is the size of handshake headers for TLS)
4377 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4378 * - ssl->out_msg + 4: the handshake message body
4379 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004380 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004381 * - ssl->out_msglen: the length of the record contents
4382 * (including handshake headers but excluding record headers)
4383 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004384 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004385int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004386{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004387 int ret;
4388 const size_t hs_len = ssl->out_msglen - 4;
4389 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004390
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4392
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004393 /*
4394 * Sanity checks
4395 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004396 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004397 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4398 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004399 /* In SSLv3, the client might send a NoCertificate alert. */
4400#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004401 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004402 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004403 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4404 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004405#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4406 {
4407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4409 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004411
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004412 /* Whenever we send anything different from a
4413 * HelloRequest we should be in a handshake - double check. */
4414 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4415 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004416 ssl->handshake == NULL )
4417 {
4418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004423 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004424 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004426 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004429 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004430#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004431
Hanno Beckerb50a2532018-08-06 11:52:54 +01004432 /* Double-check that we did not exceed the bounds
4433 * of the outgoing record buffer.
4434 * This should never fail as the various message
4435 * writing functions must obey the bounds of the
4436 * outgoing record buffer, but better be safe.
4437 *
4438 * Note: We deliberately do not check for the MTU or MFL here.
4439 */
4440 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4441 {
4442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4443 "size %u, maximum %u",
4444 (unsigned) ssl->out_msglen,
4445 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4447 }
4448
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004449 /*
4450 * Fill handshake headers
4451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004453 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004454 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[1], hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004455
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004456 /*
4457 * DTLS has additional fields in the Handshake layer,
4458 * between the length field and the actual payload:
4459 * uint16 message_seq;
4460 * uint24 fragment_offset;
4461 * uint24 fragment_length;
4462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004464 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004465 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004466 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004467 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004468 {
4469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4470 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004471 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004472 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4474 }
4475
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004476 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004477 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004478
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004479 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004480 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004481 {
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004482 (void)mbedtls_platform_put_uint16_be( &ssl->out_msg[4],
4483 ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004484 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004485 }
4486 else
4487 {
4488 ssl->out_msg[4] = 0;
4489 ssl->out_msg[5] = 0;
4490 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004491
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004492 /* Handshake hashes are computed without fragmentation,
4493 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004494 mbedtls_platform_memset( ssl->out_msg + 6, 0x00, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03004495 mbedtls_platform_memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004496 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004498
Hanno Becker0207e532018-08-28 10:28:28 +01004499 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004500 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004501 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004502 }
4503
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004504 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004506 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004507 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4508 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004509 {
4510 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004512 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004513 return( ret );
4514 }
4515 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004516 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004517#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004518 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004519 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004520 {
4521 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4522 return( ret );
4523 }
4524 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004525
4526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4527
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004528 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004529}
4530
4531/*
4532 * Record layer functions
4533 */
4534
4535/*
4536 * Write current record.
4537 *
4538 * Uses:
4539 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4540 * - ssl->out_msglen: length of the record content (excl headers)
4541 * - ssl->out_msg: record content
4542 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004543int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004544{
4545 int ret, done = 0;
4546 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004547 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004548
4549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004552 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004554 {
4555 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004558 return( ret );
4559 }
4560
4561 len = ssl->out_msglen;
4562 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4566 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570 ret = mbedtls_ssl_hw_record_write( ssl );
4571 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4574 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004575 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004576
4577 if( ret == 0 )
4578 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004579 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004581 if( !done )
4582 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004583 unsigned i;
4584 size_t protected_record_size;
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004585 volatile int encrypted_fi = 0;
Hanno Becker2b1e3542018-08-06 11:19:13 +01004586
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004587 /* Skip writing the record content type to after the encryption,
4588 * as it may change when using the CID extension. */
4589
Hanno Becker2881d802019-05-22 14:44:53 +01004590 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4591 mbedtls_ssl_get_minor_ver( ssl ),
4592 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004593
Teppo Järvelin91d79382019-10-02 09:09:31 +03004594 mbedtls_platform_memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004595 (void)mbedtls_platform_put_uint16_be( ssl->out_len, len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004596
Paul Bakker48916f92012-09-16 19:57:18 +00004597 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004598 {
Hanno Becker3307b532017-12-27 21:37:21 +00004599 mbedtls_record rec;
4600
4601 rec.buf = ssl->out_iv;
4602 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4603 ( ssl->out_iv - ssl->out_buf );
4604 rec.data_len = ssl->out_msglen;
4605 rec.data_offset = ssl->out_msg - rec.buf;
4606
Teppo Järvelin91d79382019-10-02 09:09:31 +03004607 mbedtls_platform_memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004608 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4609 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004610 ssl->conf->transport, rec.ver );
4611 rec.type = ssl->out_msgtype;
4612
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004614 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004615 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004616#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004617
Hanno Becker611a83b2018-01-03 14:27:32 +00004618 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004619 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004620 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004623 return( ret );
4624 }
4625
Hanno Becker3307b532017-12-27 21:37:21 +00004626 if( rec.data_offset != 0 )
4627 {
4628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4630 }
4631
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004632 /* Update the record content type and CID. */
4633 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004634#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03004635 /* Not using more secure mbedtls_platform_memcpy as cid is public */
4636 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004637#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004638 ssl->out_msglen = len = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03004639 (void)mbedtls_platform_put_uint16_be( ssl->out_len, rec.data_len );
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004640 encrypted_fi = 1;
4641 }
4642
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004643 /* Double check to ensure the encryption has been done */
Jarno Lamsaacb5eb02019-11-14 14:13:10 +02004644 if( ssl->transform_out != NULL && encrypted_fi == 0 )
4645 {
Jarno Lamsa88db2ae2019-12-19 14:51:34 +02004646 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004647 }
4648
Hanno Becker43395762019-05-03 14:46:38 +01004649 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004650
4651#if defined(MBEDTLS_SSL_PROTO_DTLS)
4652 /* In case of DTLS, double-check that we don't exceed
4653 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004654 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004655 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004656 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004657 if( ret < 0 )
4658 return( ret );
4659
4660 if( protected_record_size > (size_t) ret )
4661 {
4662 /* Should never happen */
4663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4664 }
4665 }
4666#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004667
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004668 /* Now write the potentially updated record content type. */
4669 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004672 "version = [%d:%d], msglen = %d",
4673 ssl->out_hdr[0], ssl->out_hdr[1],
4674 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004677 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004678
4679 ssl->out_left += protected_record_size;
4680 ssl->out_hdr += protected_record_size;
4681 ssl_update_out_pointers( ssl, ssl->transform_out );
4682
Hanno Becker04484622018-08-06 09:49:38 +01004683 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4684 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4685 break;
4686
4687 /* The loop goes to its end iff the counter is wrapping */
4688 if( i == ssl_ep_len( ssl ) )
4689 {
4690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4691 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 }
4694
Hanno Becker67bc7c32018-08-06 11:33:50 +01004695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004696 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004697 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004698 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004699 size_t remaining;
4700 ret = ssl_get_remaining_payload_in_datagram( ssl );
4701 if( ret < 0 )
4702 {
4703 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4704 ret );
4705 return( ret );
4706 }
4707
4708 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004709 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004710 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004711 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004712 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004713 else
4714 {
Hanno Becker513815a2018-08-20 11:56:09 +01004715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004716 }
4717 }
4718#endif /* MBEDTLS_SSL_PROTO_DTLS */
4719
4720 if( ( flush == SSL_FORCE_FLUSH ) &&
4721 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004724 return( ret );
4725 }
4726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004728
4729 return( 0 );
4730}
4731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004733
4734static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4735{
4736 if( ssl->in_msglen < ssl->in_hslen ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +03004737 mbedtls_platform_memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4738 mbedtls_platform_memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
Hanno Beckere25e3b72018-08-16 09:30:53 +01004739 {
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04004740 return( PROPER_HS_FRAGMENT );
Hanno Beckere25e3b72018-08-16 09:30:53 +01004741 }
4742 return( 0 );
4743}
Hanno Becker44650b72018-08-16 12:51:11 +01004744
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004745static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004746{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004747 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[9] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004748}
4749
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004750static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004751{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004752 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[6] ) );
Hanno Becker44650b72018-08-16 12:51:11 +01004753}
4754
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004755static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004756{
4757 uint32_t msg_len, frag_off, frag_len;
4758
4759 msg_len = ssl_get_hs_total_len( ssl );
4760 frag_off = ssl_get_hs_frag_off( ssl );
4761 frag_len = ssl_get_hs_frag_len( ssl );
4762
4763 if( frag_off > msg_len )
4764 return( -1 );
4765
4766 if( frag_len > msg_len - frag_off )
4767 return( -1 );
4768
4769 if( frag_len + 12 > ssl->in_msglen )
4770 return( -1 );
4771
4772 return( 0 );
4773}
4774
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004775/*
4776 * Mark bits in bitmask (used for DTLS HS reassembly)
4777 */
4778static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4779{
4780 unsigned int start_bits, end_bits;
4781
4782 start_bits = 8 - ( offset % 8 );
4783 if( start_bits != 8 )
4784 {
4785 size_t first_byte_idx = offset / 8;
4786
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004787 /* Special case */
4788 if( len <= start_bits )
4789 {
4790 for( ; len != 0; len-- )
4791 mask[first_byte_idx] |= 1 << ( start_bits - len );
4792
4793 /* Avoid potential issues with offset or len becoming invalid */
4794 return;
4795 }
4796
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004797 offset += start_bits; /* Now offset % 8 == 0 */
4798 len -= start_bits;
4799
4800 for( ; start_bits != 0; start_bits-- )
4801 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4802 }
4803
4804 end_bits = len % 8;
4805 if( end_bits != 0 )
4806 {
4807 size_t last_byte_idx = ( offset + len ) / 8;
4808
4809 len -= end_bits; /* Now len % 8 == 0 */
4810
4811 for( ; end_bits != 0; end_bits-- )
4812 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4813 }
4814
4815 memset( mask + offset / 8, 0xFF, len / 8 );
4816}
4817
4818/*
4819 * Check that bitmask is full
4820 */
4821static int ssl_bitmask_check( unsigned char *mask, size_t len )
4822{
4823 size_t i;
4824
4825 for( i = 0; i < len / 8; i++ )
4826 if( mask[i] != 0xFF )
4827 return( -1 );
4828
4829 for( i = 0; i < len % 8; i++ )
4830 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4831 return( -1 );
4832
4833 return( 0 );
4834}
4835
Hanno Becker56e205e2018-08-16 09:06:12 +01004836/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004837static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004838 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004839{
Hanno Becker56e205e2018-08-16 09:06:12 +01004840 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004841
Hanno Becker56e205e2018-08-16 09:06:12 +01004842 alloc_len = 12; /* Handshake header */
4843 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004844
Hanno Beckerd07df862018-08-16 09:14:58 +01004845 if( add_bitmap )
4846 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004847
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004848 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004849}
Hanno Becker56e205e2018-08-16 09:06:12 +01004850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004852
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004853static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004854{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004855 return( (uint32_t)mbedtls_platform_get_uint24_be( &ssl->in_msg[1] ) );
Hanno Becker12555c62018-08-16 12:47:53 +01004856}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004857
Simon Butcher99000142016-10-13 17:21:01 +01004858int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004859{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004863 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004865 }
4866
Hanno Becker12555c62018-08-16 12:47:53 +01004867 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004870 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004871 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004874 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004875 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004876 int ret;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03004877 unsigned int recv_msg_seq = (unsigned int)
4878 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004879
Hanno Becker44650b72018-08-16 12:51:11 +01004880 if( ssl_check_hs_header( ssl ) != 0 )
4881 {
4882 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4883 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4884 }
4885
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004886 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004887 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4888 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4889 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4890 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004891 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004892 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4893 {
4894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4895 recv_msg_seq,
4896 ssl->handshake->in_msg_seq ) );
4897 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4898 }
4899
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004900 /* Retransmit only on last message from previous flight, to avoid
4901 * too many retransmissions.
4902 * Besides, No sane server ever retransmits HelloVerifyRequest */
4903 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004907 "message_seq = %d, start_of_flight = %d",
4908 recv_msg_seq,
4909 ssl->handshake->in_flight_start_seq ) );
4910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004914 return( ret );
4915 }
4916 }
4917 else
4918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004920 "message_seq = %d, expected = %d",
4921 recv_msg_seq,
4922 ssl->handshake->in_msg_seq ) );
4923 }
4924
Hanno Becker90333da2017-10-10 11:27:13 +01004925 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004926 }
4927 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004928
Hanno Becker6d97ef52018-08-16 13:09:04 +01004929 /* Message reassembly is handled alongside buffering of future
4930 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004931 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004932 * handshake logic layer. */
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04004933 if( ssl_hs_is_proper_fragment( ssl ) == PROPER_HS_FRAGMENT )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004936 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004937 }
4938 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004939 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004941#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004942 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004943 /* With TLS we don't handle fragmentation (for now) */
4944 if( ssl->in_msglen < ssl->in_hslen )
4945 {
4946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4947 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4948 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004949 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004950#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004951
Simon Butcher99000142016-10-13 17:21:01 +01004952 return( 0 );
4953}
4954
4955void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4956{
Hanno Becker0271f962018-08-16 13:23:47 +01004957 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004958
Hanno Becker0271f962018-08-16 13:23:47 +01004959 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004960 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004961
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004962 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004964 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004965 ssl->handshake != NULL )
4966 {
Hanno Becker0271f962018-08-16 13:23:47 +01004967 unsigned offset;
4968 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004969
Hanno Becker0271f962018-08-16 13:23:47 +01004970 /* Increment handshake sequence number */
4971 hs->in_msg_seq++;
4972
4973 /*
4974 * Clear up handshake buffering and reassembly structure.
4975 */
4976
4977 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004978 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004979
4980 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004981 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4982 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004983 offset++, hs_buf++ )
4984 {
4985 *hs_buf = *(hs_buf + 1);
4986 }
4987
4988 /* Create a fresh last entry */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02004989 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004990 }
4991#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004992}
4993
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004994/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004995 * DTLS anti-replay: RFC 6347 4.1.2.6
4996 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004997 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4998 * Bit n is set iff record number in_window_top - n has been seen.
4999 *
5000 * Usually, in_window_top is the last record number seen and the lsb of
5001 * in_window is set. The only exception is the initial state (record number 0
5002 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5005static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005006{
5007 ssl->in_window_top = 0;
5008 ssl->in_window = 0;
5009}
5010
5011static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
5012{
5013 return( ( (uint64_t) buf[0] << 40 ) |
5014 ( (uint64_t) buf[1] << 32 ) |
5015 ( (uint64_t) buf[2] << 24 ) |
5016 ( (uint64_t) buf[3] << 16 ) |
5017 ( (uint64_t) buf[4] << 8 ) |
5018 ( (uint64_t) buf[5] ) );
5019}
5020
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005021static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
5022{
5023 int ret;
5024 unsigned char *original_in_ctr;
5025
5026 // save original in_ctr
5027 original_in_ctr = ssl->in_ctr;
5028
5029 // use counter from record
5030 ssl->in_ctr = record_in_ctr;
5031
5032 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
5033
5034 // restore the counter
5035 ssl->in_ctr = original_in_ctr;
5036
5037 return ret;
5038}
5039
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005040/*
5041 * Return 0 if sequence number is acceptable, -1 otherwise
5042 */
Hanno Beckerfc551722019-07-12 08:50:37 +01005043int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005044{
5045 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5046 uint64_t bit;
5047
Hanno Becker7f376f42019-06-12 16:20:48 +01005048 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5049 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5050 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005051 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01005052 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005053
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005054 if( rec_seqnum > ssl->in_window_top )
5055 return( 0 );
5056
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005057 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005058
5059 if( bit >= 64 )
5060 return( -1 );
5061
5062 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
5063 return( -1 );
5064
5065 return( 0 );
5066}
5067
5068/*
5069 * Update replay window on new validated record
5070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005072{
5073 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
5074
Hanno Becker7f376f42019-06-12 16:20:48 +01005075 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
5076 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
5077 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005078 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01005079 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005080
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005081 if( rec_seqnum > ssl->in_window_top )
5082 {
5083 /* Update window_top and the contents of the window */
5084 uint64_t shift = rec_seqnum - ssl->in_window_top;
5085
5086 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005087 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005088 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005089 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005090 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005091 ssl->in_window |= 1;
5092 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005093
5094 ssl->in_window_top = rec_seqnum;
5095 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005096 else
5097 {
5098 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02005099 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005100
5101 if( bit < 64 ) /* Always true, but be extra sure */
5102 ssl->in_window |= (uint64_t) 1 << bit;
5103 }
5104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005106
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005107#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005108/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02005109static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
5110
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005111/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005112 * Without any SSL context, check if a datagram looks like a ClientHello with
5113 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01005114 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005115 *
5116 * - if cookie is valid, return 0
5117 * - if ClientHello looks superficially valid but cookie is not,
5118 * fill obuf and set olen, then
5119 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
5120 * - otherwise return a specific error code
5121 */
5122static int ssl_check_dtls_clihlo_cookie(
5123 mbedtls_ssl_cookie_write_t *f_cookie_write,
5124 mbedtls_ssl_cookie_check_t *f_cookie_check,
5125 void *p_cookie,
5126 const unsigned char *cli_id, size_t cli_id_len,
5127 const unsigned char *in, size_t in_len,
5128 unsigned char *obuf, size_t buf_len, size_t *olen )
5129{
5130 size_t sid_len, cookie_len;
5131 unsigned char *p;
5132
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005133 /*
5134 * Structure of ClientHello with record and handshake headers,
5135 * and expected values. We don't need to check a lot, more checks will be
5136 * done when actually parsing the ClientHello - skipping those checks
5137 * avoids code duplication and does not make cookie forging any easier.
5138 *
5139 * 0-0 ContentType type; copied, must be handshake
5140 * 1-2 ProtocolVersion version; copied
5141 * 3-4 uint16 epoch; copied, must be 0
5142 * 5-10 uint48 sequence_number; copied
5143 * 11-12 uint16 length; (ignored)
5144 *
5145 * 13-13 HandshakeType msg_type; (ignored)
5146 * 14-16 uint24 length; (ignored)
5147 * 17-18 uint16 message_seq; copied
5148 * 19-21 uint24 fragment_offset; copied, must be 0
5149 * 22-24 uint24 fragment_length; (ignored)
5150 *
5151 * 25-26 ProtocolVersion client_version; (ignored)
5152 * 27-58 Random random; (ignored)
5153 * 59-xx SessionID session_id; 1 byte len + sid_len content
5154 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5155 * ...
5156 *
5157 * Minimum length is 61 bytes.
5158 */
5159 if( in_len < 61 ||
5160 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5161 in[3] != 0 || in[4] != 0 ||
5162 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5163 {
5164 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5165 }
5166
5167 sid_len = in[59];
5168 if( sid_len > in_len - 61 )
5169 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5170
5171 cookie_len = in[60 + sid_len];
5172 if( cookie_len > in_len - 60 )
5173 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5174
5175 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5176 cli_id, cli_id_len ) == 0 )
5177 {
5178 /* Valid cookie */
5179 return( 0 );
5180 }
5181
5182 /*
5183 * If we get here, we've got an invalid cookie, let's prepare HVR.
5184 *
5185 * 0-0 ContentType type; copied
5186 * 1-2 ProtocolVersion version; copied
5187 * 3-4 uint16 epoch; copied
5188 * 5-10 uint48 sequence_number; copied
5189 * 11-12 uint16 length; olen - 13
5190 *
5191 * 13-13 HandshakeType msg_type; hello_verify_request
5192 * 14-16 uint24 length; olen - 25
5193 * 17-18 uint16 message_seq; copied
5194 * 19-21 uint24 fragment_offset; copied
5195 * 22-24 uint24 fragment_length; olen - 25
5196 *
5197 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5198 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5199 *
5200 * Minimum length is 28.
5201 */
5202 if( buf_len < 28 )
5203 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5204
5205 /* Copy most fields and adapt others */
Teppo Järvelin91d79382019-10-02 09:09:31 +03005206 mbedtls_platform_memcpy( obuf, in, 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005207 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5208 obuf[25] = 0xfe;
5209 obuf[26] = 0xff;
5210
5211 /* Generate and write actual cookie */
5212 p = obuf + 28;
5213 if( f_cookie_write( p_cookie,
5214 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5215 {
5216 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5217 }
5218
5219 *olen = p - obuf;
5220
5221 /* Go back and fill length fields */
5222 obuf[27] = (unsigned char)( *olen - 28 );
5223
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005224 (void)mbedtls_platform_put_uint24_be( &obuf[14], ( *olen - 25 ) );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005225 obuf[22] = obuf[14];
5226 obuf[23] = obuf[15];
5227 obuf[24] = obuf[16];
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005228
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03005229 (void)mbedtls_platform_put_uint16_be( &obuf[11], ( *olen - 13 ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005230
5231 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5232}
5233
5234/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005235 * Handle possible client reconnect with the same UDP quadruplet
5236 * (RFC 6347 Section 4.2.8).
5237 *
5238 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5239 * that looks like a ClientHello.
5240 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005241 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005242 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005243 * - if the input looks like a ClientHello with a valid cookie,
5244 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005245 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005246 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005247 *
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005248 * This function is called (through ssl_check_client_reconnect()) when an
5249 * unexpected record is found in ssl_get_next_record(), which will discard the
5250 * record if we return 0, and bubble up the return value otherwise (this
5251 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
5252 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005253 */
5254static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5255{
5256 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005257 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005258
Hanno Becker87b56262019-07-10 14:37:41 +01005259 if( ssl->conf->f_cookie_write == NULL ||
5260 ssl->conf->f_cookie_check == NULL )
5261 {
5262 /* If we can't use cookies to verify reachability of the peer,
5263 * drop the record. */
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
5265 "can't check reconnect validity" ) );
Hanno Becker87b56262019-07-10 14:37:41 +01005266 return( 0 );
5267 }
5268
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005269 ret = ssl_check_dtls_clihlo_cookie(
5270 ssl->conf->f_cookie_write,
5271 ssl->conf->f_cookie_check,
5272 ssl->conf->p_cookie,
5273 ssl->cli_id, ssl->cli_id_len,
5274 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005275 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005276
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005277 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5278
5279 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005280 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005281 int send_ret;
5282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
5283 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
5284 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08005285 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005286 * If the error is permanent we'll catch it later,
5287 * if it's not, then hopefully it'll work next time. */
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005288 send_ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
5289 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_get_send", send_ret );
5290 (void) send_ret;
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02005291 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005292 }
5293
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005294 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005295 {
Andrzej Kurek825ebd42020-05-18 11:47:25 -04005296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005297 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5298 {
5299 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5300 return( ret );
5301 }
5302
5303 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005304 }
5305
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005306 return( ret );
5307}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005308#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005309
Hanno Becker46483f12019-05-03 13:25:54 +01005310static int ssl_check_record_type( uint8_t record_type )
5311{
5312 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5313 record_type != MBEDTLS_SSL_MSG_ALERT &&
5314 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5315 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5316 {
5317 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5318 }
5319
5320 return( 0 );
5321}
5322
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005323/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005324 * ContentType type;
5325 * ProtocolVersion version;
5326 * uint16 epoch; // DTLS only
5327 * uint48 sequence_number; // DTLS only
5328 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005329 *
5330 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005331 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005332 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5333 *
5334 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005335 * 1. proceed with the record if this function returns 0
5336 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5337 * 3. return CLIENT_RECONNECT if this function return that value
5338 * 4. drop the whole datagram if this function returns anything else.
5339 * Point 2 is needed when the peer is resending, and we have already received
5340 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005341 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005342static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005343 unsigned char *buf,
5344 size_t len,
5345 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005346{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005347 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005348
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005349 size_t const rec_hdr_type_offset = 0;
5350 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005351
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005352 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5353 rec_hdr_type_len;
5354 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005355
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005356 size_t const rec_hdr_ctr_len = 8;
5357#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005358 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005359 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5360 rec_hdr_version_len;
5361
Hanno Beckera5a2b082019-05-15 14:03:01 +01005362#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005363 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5364 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005365 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005366#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5367#endif /* MBEDTLS_SSL_PROTO_DTLS */
5368
5369 size_t rec_hdr_len_offset; /* To be determined */
5370 size_t const rec_hdr_len_len = 2;
5371
5372 /*
5373 * Check minimum lengths for record header.
5374 */
5375
5376#if defined(MBEDTLS_SSL_PROTO_DTLS)
5377 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5378 {
5379 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5380 }
5381 MBEDTLS_SSL_TRANSPORT_ELSE
5382#endif /* MBEDTLS_SSL_PROTO_DTLS */
5383#if defined(MBEDTLS_SSL_PROTO_TLS)
5384 {
5385 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5386 }
5387#endif /* MBEDTLS_SSL_PROTO_DTLS */
5388
5389 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5390 {
5391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5392 (unsigned) len,
5393 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5394 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5395 }
5396
5397 /*
5398 * Parse and validate record content type
5399 */
5400
5401 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005402
5403 /* Check record content type */
5404#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5405 rec->cid_len = 0;
5406
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005407 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005408 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5409 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005410 {
5411 /* Shift pointers to account for record header including CID
5412 * struct {
5413 * ContentType special_type = tls12_cid;
5414 * ProtocolVersion version;
5415 * uint16 epoch;
5416 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005417 * opaque cid[cid_length]; // Additional field compared to
5418 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005419 * uint16 length;
5420 * opaque enc_content[DTLSCiphertext.length];
5421 * } DTLSCiphertext;
5422 */
5423
5424 /* So far, we only support static CID lengths
5425 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005426 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5427 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005428
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005429 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005430 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5432 (unsigned) len,
5433 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005434 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005435 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005436
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005437 /* configured CID len is guaranteed at most 255, see
5438 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5439 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005440 /* Not using more secure mbedtls_platform_memcpy as cid is public */
5441 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005442 }
5443 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005444#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005445 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005446 if( ssl_check_record_type( rec->type ) )
5447 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5449 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005450 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5451 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005452 }
5453
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005454 /*
5455 * Parse and validate record version
5456 */
5457
Hanno Becker8061c6e2019-07-26 08:07:03 +01005458 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5459 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005460 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5461 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005462 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005463
Hanno Becker2881d802019-05-22 14:44:53 +01005464 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5467 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005468 }
5469
Hanno Becker7bcf2b52019-07-26 09:02:40 +01005470 if( mbedtls_ssl_ver_gt( minor_ver,
5471 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5474 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005475 }
5476
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005477 /*
5478 * Parse/Copy record sequence number.
5479 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005480
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005481#if defined(MBEDTLS_SSL_PROTO_DTLS)
5482 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5483 {
5484 /* Copy explicit record sequence number from input buffer. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005485 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5486 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005487 rec_hdr_ctr_len );
5488 }
5489 MBEDTLS_SSL_TRANSPORT_ELSE
5490#endif /* MBEDTLS_SSL_PROTO_DTLS */
5491#if defined(MBEDTLS_SSL_PROTO_TLS)
5492 {
5493 /* Copy implicit record sequence number from SSL context structure. */
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03005494 /* Not using more secure mbedtls_platform_memcpy as sequence number is public */
5495 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005496 }
5497#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005498
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005499 /*
5500 * Parse record length.
5501 */
5502
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005503 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03005504 rec->data_len = mbedtls_platform_get_uint16_be( &buf[rec_hdr_len_offset] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005505 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5506
Hanno Becker8b09b732019-05-08 12:03:28 +01005507 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005508 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005509 rec->type,
5510 major_ver, minor_ver, rec->data_len ) );
5511
5512 rec->buf = buf;
5513 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005514
Hanno Beckerec014082019-07-26 08:20:27 +01005515 if( rec->data_len == 0 )
5516 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5517
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005518 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005519 * DTLS-related tests.
5520 * Check epoch before checking length constraint because
5521 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5522 * message gets duplicated before the corresponding Finished message,
5523 * the second ChangeCipherSpec should be discarded because it belongs
5524 * to an old epoch, but not because its length is shorter than
5525 * the minimum record length for packets using the new record transform.
5526 * Note that these two kinds of failures are handled differently,
5527 * as an unexpected record is silently skipped but an invalid
5528 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005529 */
5530#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005531 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005532 {
Arto Kinnunena3fa06e2019-09-09 12:22:51 +03005533 rec_epoch = (uint32_t)mbedtls_platform_get_uint16_be( rec->ctr );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005534
Hanno Beckere0452772019-07-10 17:12:07 +01005535 /* Check that the datagram is large enough to contain a record
5536 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005537 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005538 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5540 (unsigned) len,
5541 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005542 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5543 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005544
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005545 /* Records from other, non-matching epochs are silently discarded.
5546 * (The case of same-port Client reconnects must be considered in
5547 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005548 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005549 {
5550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5551 "expected %d, received %d",
5552 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005553
5554 /* Records from the next epoch are considered for buffering
5555 * (concretely: early Finished messages). */
5556 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5557 {
5558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5559 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5560 }
5561
Hanno Becker87b56262019-07-10 14:37:41 +01005562 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005563 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005564#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005565 /* For records from the correct epoch, check whether their
5566 * sequence number has been seen before. */
Arto Kinnunen8a8488c2019-10-29 11:13:33 +02005567 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
5568 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005569 {
5570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5571 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5572 }
5573#endif
5574 }
5575#endif /* MBEDTLS_SSL_PROTO_DTLS */
5576
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005577 return( 0 );
5578}
Paul Bakker5121ce52009-01-03 21:22:43 +00005579
Hanno Becker87b56262019-07-10 14:37:41 +01005580
5581#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5582static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5583{
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03005584 unsigned int rec_epoch = (unsigned int)
5585 mbedtls_platform_get_uint16_be( &ssl->in_ctr[0] );
Hanno Becker87b56262019-07-10 14:37:41 +01005586
5587 /*
5588 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5589 * access the first byte of record content (handshake type), as we
5590 * have an active transform (possibly iv_len != 0), so use the
5591 * fact that the record header len is 13 instead.
5592 */
5593 if( rec_epoch == 0 &&
5594 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5595 MBEDTLS_SSL_IS_SERVER &&
5596 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5597 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5598 ssl->in_left > 13 &&
5599 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5600 {
5601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5602 "from the same port" ) );
5603 return( ssl_handle_possible_reconnect( ssl ) );
5604 }
5605
5606 return( 0 );
5607}
5608#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5609
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005610/*
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01005611 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005612 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005613static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5614 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005615{
5616 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005619 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5622 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626 ret = mbedtls_ssl_hw_record_read( ssl );
5627 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5630 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005631 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005632
5633 if( ret == 0 )
5634 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005635 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005637 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005639 unsigned char const old_msg_type = rec->type;
5640
Hanno Becker611a83b2018-01-03 14:27:32 +00005641 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005642 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005645
Hanno Beckera5a2b082019-05-15 14:03:01 +01005646#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005647 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005648 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5649 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005650 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005651 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005652 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005653 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005654#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005655
Paul Bakker5121ce52009-01-03 21:22:43 +00005656 return( ret );
5657 }
5658
Hanno Becker106f3da2019-07-12 09:35:58 +01005659 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005660 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005661 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005662 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005664
Paul Bakker5121ce52009-01-03 21:22:43 +00005665 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005666 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005667
Hanno Beckera5a2b082019-05-15 14:03:01 +01005668#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005669 /* We have already checked the record content type
5670 * in ssl_parse_record_header(), failing or silently
5671 * dropping the record in the case of an unknown type.
5672 *
5673 * Since with the use of CIDs, the record content type
5674 * might change during decryption, re-check the record
5675 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005676 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005677 {
5678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5679 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5680 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005681#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005682
Hanno Becker106f3da2019-07-12 09:35:58 +01005683 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005684 {
5685#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005686 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005687 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005688 {
5689 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5691 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5692 }
5693#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5694
5695 ssl->nb_zero++;
5696
5697 /*
5698 * Three or more empty messages may be a DoS attack
5699 * (excessive CPU consumption).
5700 */
5701 if( ssl->nb_zero > 3 )
5702 {
5703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005704 "messages, possible DoS attack" ) );
5705 /* Treat the records as if they were not properly authenticated,
5706 * thereby failing the connection if we see more than allowed
5707 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005708 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5709 }
5710 }
5711 else
5712 ssl->nb_zero = 0;
5713
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005714 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5715#if defined(MBEDTLS_SSL_PROTO_TLS)
5716 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005717 {
5718 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005719 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005720 if( ++ssl->in_ctr[i - 1] != 0 )
5721 break;
5722
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005723 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005724 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005725 {
5726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5727 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5728 }
5729 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005730#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005731
Paul Bakker5121ce52009-01-03 21:22:43 +00005732 }
5733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005735 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005737 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005738 }
5739#endif
5740
Hanno Beckerf0242852019-07-09 17:30:02 +01005741 /* Check actual (decrypted) record content length against
5742 * configured maximum. */
5743 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5744 {
5745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5746 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5747 }
5748
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005749 return( 0 );
5750}
5751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005752static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005753
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005754/*
5755 * Read a record.
5756 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005757 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5758 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5759 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005760 */
Hanno Becker1097b342018-08-15 14:09:41 +01005761
5762/* Helper functions for mbedtls_ssl_read_record(). */
5763static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005764static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5765static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005766
Hanno Becker327c93b2018-08-15 13:56:18 +01005767int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005768 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005769{
5770 int ret;
5771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005773
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005774 if( ssl->keep_current_message == 0 )
5775 {
5776 do {
Simon Butcher99000142016-10-13 17:21:01 +01005777
Hanno Becker26994592018-08-15 14:14:59 +01005778 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005779 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005780 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005781
Hanno Beckere74d5562018-08-15 14:26:08 +01005782 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005783 {
Hanno Becker40f50842018-08-15 14:48:01 +01005784#if defined(MBEDTLS_SSL_PROTO_DTLS)
5785 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005786
Hanno Becker40f50842018-08-15 14:48:01 +01005787 /* We only check for buffered messages if the
5788 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005789 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005790 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005791 {
Hanno Becker40f50842018-08-15 14:48:01 +01005792 if( ssl_load_buffered_message( ssl ) == 0 )
5793 have_buffered = 1;
5794 }
5795
5796 if( have_buffered == 0 )
5797#endif /* MBEDTLS_SSL_PROTO_DTLS */
5798 {
5799 ret = ssl_get_next_record( ssl );
5800 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5801 continue;
5802
5803 if( ret != 0 )
5804 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005805 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005806 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005807 return( ret );
5808 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005809 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005810 }
5811
5812 ret = mbedtls_ssl_handle_message_type( ssl );
5813
Hanno Becker40f50842018-08-15 14:48:01 +01005814#if defined(MBEDTLS_SSL_PROTO_DTLS)
5815 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5816 {
5817 /* Buffer future message */
5818 ret = ssl_buffer_message( ssl );
5819 if( ret != 0 )
5820 return( ret );
5821
5822 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5823 }
5824#endif /* MBEDTLS_SSL_PROTO_DTLS */
5825
Hanno Becker90333da2017-10-10 11:27:13 +01005826 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5827 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005828
5829 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005830 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005831 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005832 return( ret );
5833 }
5834
Hanno Becker327c93b2018-08-15 13:56:18 +01005835 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005836 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005837 {
5838 mbedtls_ssl_update_handshake_status( ssl );
5839 }
Simon Butcher99000142016-10-13 17:21:01 +01005840 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005841 else
Simon Butcher99000142016-10-13 17:21:01 +01005842 {
Hanno Becker02f59072018-08-15 14:00:24 +01005843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005844 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005845 }
5846
5847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5848
5849 return( 0 );
5850}
5851
Hanno Becker40f50842018-08-15 14:48:01 +01005852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005853static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005854{
Hanno Becker40f50842018-08-15 14:48:01 +01005855 if( ssl->in_left > ssl->next_record_offset )
5856 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005857
Hanno Becker40f50842018-08-15 14:48:01 +01005858 return( 0 );
5859}
5860
5861static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5862{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005864 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005865 int ret = 0;
5866
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005867 if( hs == NULL )
5868 return( -1 );
5869
Hanno Beckere00ae372018-08-20 09:39:42 +01005870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5871
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005872 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5873 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5874 {
5875 /* Check if we have seen a ChangeCipherSpec before.
5876 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005877 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005878 {
5879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5880 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005881 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005882 }
5883
Hanno Becker39b8bc92018-08-28 17:17:13 +01005884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005885 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5886 ssl->in_msglen = 1;
5887 ssl->in_msg[0] = 1;
5888
5889 /* As long as they are equal, the exact value doesn't matter. */
5890 ssl->in_left = 0;
5891 ssl->next_record_offset = 0;
5892
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005893 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005894 goto exit;
5895 }
Hanno Becker37f95322018-08-16 13:55:32 +01005896
Hanno Beckerb8f50142018-08-28 10:01:34 +01005897#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005898 /* Debug only */
5899 {
5900 unsigned offset;
5901 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5902 {
5903 hs_buf = &hs->buffering.hs[offset];
5904 if( hs_buf->is_valid == 1 )
5905 {
5906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5907 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005908 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005909 }
5910 }
5911 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005912#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005913
5914 /* Check if we have buffered and/or fully reassembled the
5915 * next handshake message. */
5916 hs_buf = &hs->buffering.hs[0];
5917 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5918 {
5919 /* Synthesize a record containing the buffered HS message. */
Arto Kinnunen84eeb4f2019-09-10 10:32:30 +03005920 size_t msg_len = mbedtls_platform_get_uint24_be( &hs_buf->data[1] );
Hanno Becker37f95322018-08-16 13:55:32 +01005921
5922 /* Double-check that we haven't accidentally buffered
5923 * a message that doesn't fit into the input buffer. */
5924 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5925 {
5926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5928 }
5929
5930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5931 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5932 hs_buf->data, msg_len + 12 );
5933
5934 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5935 ssl->in_hslen = msg_len + 12;
5936 ssl->in_msglen = msg_len + 12;
Teppo Järvelin91d79382019-10-02 09:09:31 +03005937 mbedtls_platform_memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
Hanno Becker37f95322018-08-16 13:55:32 +01005938
5939 ret = 0;
5940 goto exit;
5941 }
5942 else
5943 {
5944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5945 hs->in_msg_seq ) );
5946 }
5947
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005948 ret = -1;
5949
5950exit:
5951
5952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5953 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005954}
5955
Hanno Beckera02b0b42018-08-21 17:20:27 +01005956static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5957 size_t desired )
5958{
5959 int offset;
5960 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5962 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005963
Hanno Becker01315ea2018-08-21 17:22:17 +01005964 /* Get rid of future records epoch first, if such exist. */
5965 ssl_free_buffered_record( ssl );
5966
5967 /* Check if we have enough space available now. */
5968 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5969 hs->buffering.total_bytes_buffered ) )
5970 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005972 return( 0 );
5973 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005974
Hanno Becker4f432ad2018-08-28 10:02:32 +01005975 /* We don't have enough space to buffer the next expected handshake
5976 * message. Remove buffers used for future messages to gain space,
5977 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005978 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5979 offset >= 0; offset-- )
5980 {
5981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5982 offset ) );
5983
Hanno Beckerb309b922018-08-23 13:18:05 +01005984 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005985
5986 /* Check if we have enough space available now. */
5987 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5988 hs->buffering.total_bytes_buffered ) )
5989 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005991 return( 0 );
5992 }
5993 }
5994
5995 return( -1 );
5996}
5997
Hanno Becker40f50842018-08-15 14:48:01 +01005998static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5999{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006000 int ret = 0;
6001 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6002
6003 if( hs == NULL )
6004 return( 0 );
6005
6006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
6007
6008 switch( ssl->in_msgtype )
6009 {
6010 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
6011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01006012
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01006013 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006014 break;
6015
6016 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01006017 {
6018 unsigned recv_msg_seq_offset;
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03006019 unsigned recv_msg_seq = (unsigned)
6020 mbedtls_platform_get_uint16_be( &ssl->in_msg[4] );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03006021
Hanno Becker37f95322018-08-16 13:55:32 +01006022 mbedtls_ssl_hs_buffer *hs_buf;
6023 size_t msg_len = ssl->in_hslen - 12;
6024
6025 /* We should never receive an old handshake
6026 * message - double-check nonetheless. */
6027 if( recv_msg_seq < ssl->handshake->in_msg_seq )
6028 {
6029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6031 }
6032
6033 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
6034 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6035 {
6036 /* Silently ignore -- message too far in the future */
6037 MBEDTLS_SSL_DEBUG_MSG( 2,
6038 ( "Ignore future HS message with sequence number %u, "
6039 "buffering window %u - %u",
6040 recv_msg_seq, ssl->handshake->in_msg_seq,
6041 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
6042
6043 goto exit;
6044 }
6045
6046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
6047 recv_msg_seq, recv_msg_seq_offset ) );
6048
6049 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
6050
6051 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01006052 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01006053 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006054 size_t reassembly_buf_sz;
6055
Hanno Becker37f95322018-08-16 13:55:32 +01006056 hs_buf->is_fragmented =
Andrzej Kurek8f52a8a2020-06-08 11:02:22 -04006057 ( ssl_hs_is_proper_fragment( ssl ) == PROPER_HS_FRAGMENT );
Hanno Becker37f95322018-08-16 13:55:32 +01006058
6059 /* We copy the message back into the input buffer
6060 * after reassembly, so check that it's not too large.
6061 * This is an implementation-specific limitation
6062 * and not one from the standard, hence it is not
6063 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01006064 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01006065 {
6066 /* Ignore message */
6067 goto exit;
6068 }
6069
Hanno Beckere0b150f2018-08-21 15:51:03 +01006070 /* Check if we have enough space to buffer the message. */
6071 if( hs->buffering.total_bytes_buffered >
6072 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
6073 {
6074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6076 }
6077
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006078 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
6079 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01006080
6081 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
6082 hs->buffering.total_bytes_buffered ) )
6083 {
6084 if( recv_msg_seq_offset > 0 )
6085 {
6086 /* If we can't buffer a future message because
6087 * of space limitations -- ignore. */
6088 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",
6089 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6090 (unsigned) hs->buffering.total_bytes_buffered ) );
6091 goto exit;
6092 }
Hanno Beckere1801392018-08-21 16:51:05 +01006093 else
6094 {
6095 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",
6096 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
6097 (unsigned) hs->buffering.total_bytes_buffered ) );
6098 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006099
Hanno Beckera02b0b42018-08-21 17:20:27 +01006100 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006101 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01006102 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",
6103 (unsigned) msg_len,
6104 (unsigned) reassembly_buf_sz,
6105 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01006106 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01006107 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6108 goto exit;
6109 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006110 }
6111
6112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
6113 msg_len ) );
6114
Hanno Becker2a97b0e2018-08-21 15:47:49 +01006115 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
6116 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01006117 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01006118 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01006119 goto exit;
6120 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01006121 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006122
6123 /* Prepare final header: copy msg_type, length and message_seq,
6124 * then add standardised fragment_offset and fragment_length */
Teppo Järvelin91d79382019-10-02 09:09:31 +03006125 mbedtls_platform_memcpy( hs_buf->data, ssl->in_msg, 6 );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02006126 mbedtls_platform_memset( hs_buf->data + 6, 0, 3 );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006127 mbedtls_platform_memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
Hanno Becker37f95322018-08-16 13:55:32 +01006128
6129 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01006130
6131 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01006132 }
6133 else
6134 {
6135 /* Make sure msg_type and length are consistent */
Teppo Järvelin0efac532019-10-04 13:21:08 +03006136 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 +01006137 {
6138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6139 /* Ignore */
6140 goto exit;
6141 }
6142 }
6143
Hanno Becker4422bbb2018-08-20 09:40:19 +01006144 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006145 {
6146 size_t frag_len, frag_off;
6147 unsigned char * const msg = hs_buf->data + 12;
6148
6149 /*
6150 * Check and copy current fragment
6151 */
6152
6153 /* Validation of header fields already done in
6154 * mbedtls_ssl_prepare_handshake_record(). */
6155 frag_off = ssl_get_hs_frag_off( ssl );
6156 frag_len = ssl_get_hs_frag_len( ssl );
6157
6158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6159 frag_off, frag_len ) );
Teppo Järvelin91d79382019-10-02 09:09:31 +03006160 mbedtls_platform_memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
Hanno Becker37f95322018-08-16 13:55:32 +01006161
6162 if( hs_buf->is_fragmented )
6163 {
6164 unsigned char * const bitmask = msg + msg_len;
6165 ssl_bitmask_set( bitmask, frag_off, frag_len );
6166 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6167 msg_len ) == 0 );
6168 }
6169 else
6170 {
6171 hs_buf->is_complete = 1;
6172 }
6173
6174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6175 hs_buf->is_complete ? "" : "not yet " ) );
6176 }
6177
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006178 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006179 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006180
6181 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006182 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006183 break;
6184 }
6185
6186exit:
6187
6188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6189 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006190}
6191#endif /* MBEDTLS_SSL_PROTO_DTLS */
6192
Hanno Becker1097b342018-08-15 14:09:41 +01006193static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006194{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006195 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006196 * Consume last content-layer message and potentially
6197 * update in_msglen which keeps track of the contents'
6198 * consumption state.
6199 *
6200 * (1) Handshake messages:
6201 * Remove last handshake message, move content
6202 * and adapt in_msglen.
6203 *
6204 * (2) Alert messages:
6205 * Consume whole record content, in_msglen = 0.
6206 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006207 * (3) Change cipher spec:
6208 * Consume whole record content, in_msglen = 0.
6209 *
6210 * (4) Application data:
6211 * Don't do anything - the record layer provides
6212 * the application data as a stream transport
6213 * and consumes through mbedtls_ssl_read only.
6214 *
6215 */
6216
6217 /* Case (1): Handshake messages */
6218 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006219 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006220 /* Hard assertion to be sure that no application data
6221 * is in flight, as corrupting ssl->in_msglen during
6222 * ssl->in_offt != NULL is fatal. */
6223 if( ssl->in_offt != NULL )
6224 {
6225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6227 }
6228
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006229 /*
6230 * Get next Handshake message in the current record
6231 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006232
Hanno Becker4a810fb2017-05-24 16:27:30 +01006233 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006234 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006235 * current handshake content: If DTLS handshake
6236 * fragmentation is used, that's the fragment
6237 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006238 * size here is faulty and should be changed at
6239 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006240 * (2) While it doesn't seem to cause problems, one
6241 * has to be very careful not to assume that in_hslen
6242 * is always <= in_msglen in a sensible communication.
6243 * Again, it's wrong for DTLS handshake fragmentation.
6244 * The following check is therefore mandatory, and
6245 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006246 * Additionally, ssl->in_hslen might be arbitrarily out of
6247 * bounds after handling a DTLS message with an unexpected
6248 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006249 */
6250 if( ssl->in_hslen < ssl->in_msglen )
6251 {
6252 ssl->in_msglen -= ssl->in_hslen;
6253 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6254 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006255
Hanno Becker4a810fb2017-05-24 16:27:30 +01006256 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6257 ssl->in_msg, ssl->in_msglen );
6258 }
6259 else
6260 {
6261 ssl->in_msglen = 0;
6262 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006263
Hanno Becker4a810fb2017-05-24 16:27:30 +01006264 ssl->in_hslen = 0;
6265 }
6266 /* Case (4): Application data */
6267 else if( ssl->in_offt != NULL )
6268 {
6269 return( 0 );
6270 }
6271 /* Everything else (CCS & Alerts) */
6272 else
6273 {
6274 ssl->in_msglen = 0;
6275 }
6276
Hanno Becker1097b342018-08-15 14:09:41 +01006277 return( 0 );
6278}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006279
Hanno Beckere74d5562018-08-15 14:26:08 +01006280static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6281{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006282 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006283 return( 1 );
6284
6285 return( 0 );
6286}
6287
Hanno Becker5f066e72018-08-16 14:56:31 +01006288#if defined(MBEDTLS_SSL_PROTO_DTLS)
6289
6290static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6291{
6292 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6293 if( hs == NULL )
6294 return;
6295
Hanno Becker01315ea2018-08-21 17:22:17 +01006296 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006297 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006298 hs->buffering.total_bytes_buffered -=
6299 hs->buffering.future_record.len;
6300
6301 mbedtls_free( hs->buffering.future_record.data );
6302 hs->buffering.future_record.data = NULL;
6303 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006304}
6305
6306static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6307{
6308 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6309 unsigned char * rec;
6310 size_t rec_len;
6311 unsigned rec_epoch;
6312
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006313 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006314 return( 0 );
6315
6316 if( hs == NULL )
6317 return( 0 );
6318
Hanno Becker5f066e72018-08-16 14:56:31 +01006319 rec = hs->buffering.future_record.data;
6320 rec_len = hs->buffering.future_record.len;
6321 rec_epoch = hs->buffering.future_record.epoch;
6322
6323 if( rec == NULL )
6324 return( 0 );
6325
Hanno Becker4cb782d2018-08-20 11:19:05 +01006326 /* Only consider loading future records if the
6327 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006328 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006329 return( 0 );
6330
Hanno Becker5f066e72018-08-16 14:56:31 +01006331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6332
6333 if( rec_epoch != ssl->in_epoch )
6334 {
6335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6336 goto exit;
6337 }
6338
6339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6340
6341 /* Double-check that the record is not too large */
6342 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6343 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6344 {
6345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6346 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6347 }
6348
Teppo Järvelin91d79382019-10-02 09:09:31 +03006349 mbedtls_platform_memcpy( ssl->in_hdr, rec, rec_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006350 ssl->in_left = rec_len;
6351 ssl->next_record_offset = 0;
6352
6353 ssl_free_buffered_record( ssl );
6354
6355exit:
6356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6357 return( 0 );
6358}
6359
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006360static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6361 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006362{
6363 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006364
6365 /* Don't buffer future records outside handshakes. */
6366 if( hs == NULL )
6367 return( 0 );
6368
6369 /* Only buffer handshake records (we are only interested
6370 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006371 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006372 return( 0 );
6373
6374 /* Don't buffer more than one future epoch record. */
6375 if( hs->buffering.future_record.data != NULL )
6376 return( 0 );
6377
Hanno Becker01315ea2018-08-21 17:22:17 +01006378 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006379 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006380 hs->buffering.total_bytes_buffered ) )
6381 {
6382 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 +01006383 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006384 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006385 return( 0 );
6386 }
6387
Hanno Becker5f066e72018-08-16 14:56:31 +01006388 /* Buffer record */
6389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6390 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006391 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006392
6393 /* ssl_parse_record_header() only considers records
6394 * of the next epoch as candidates for buffering. */
6395 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006396 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006397
6398 hs->buffering.future_record.data =
6399 mbedtls_calloc( 1, hs->buffering.future_record.len );
6400 if( hs->buffering.future_record.data == NULL )
6401 {
6402 /* If we run out of RAM trying to buffer a
6403 * record from the next epoch, just ignore. */
6404 return( 0 );
6405 }
6406
Teppo Järvelin91d79382019-10-02 09:09:31 +03006407 mbedtls_platform_memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006408
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006409 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006410 return( 0 );
6411}
6412
6413#endif /* MBEDTLS_SSL_PROTO_DTLS */
6414
Hanno Beckere74d5562018-08-15 14:26:08 +01006415static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006416{
6417 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006418 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006419
Hanno Becker5f066e72018-08-16 14:56:31 +01006420#if defined(MBEDTLS_SSL_PROTO_DTLS)
6421 /* We might have buffered a future record; if so,
6422 * and if the epoch matches now, load it.
6423 * On success, this call will set ssl->in_left to
6424 * the length of the buffered record, so that
6425 * the calls to ssl_fetch_input() below will
6426 * essentially be no-ops. */
6427 ret = ssl_load_buffered_record( ssl );
6428 if( ret != 0 )
6429 return( ret );
6430#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006431
Hanno Becker8b09b732019-05-08 12:03:28 +01006432 /* Ensure that we have enough space available for the default form
6433 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6434 * with no space for CIDs counted in). */
6435 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6436 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006439 return( ret );
6440 }
6441
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006442 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6443 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006446 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006447 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006448 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6449 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006450 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006451 if( ret != 0 )
6452 return( ret );
6453
6454 /* Fall through to handling of unexpected records */
6455 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6456 }
6457
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006458 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6459 {
Hanno Becker87b56262019-07-10 14:37:41 +01006460#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006461 /* Reset in pointers to default state for TLS/DTLS records,
6462 * assuming no CID and no offset between record content and
6463 * record plaintext. */
6464 ssl_update_in_pointers( ssl );
6465
Hanno Becker69412452019-07-12 08:33:49 +01006466 /* Setup internal message pointers from record structure. */
6467 ssl->in_msgtype = rec.type;
6468#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6469 ssl->in_len = ssl->in_cid + rec.cid_len;
6470#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006471 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006472 ssl->in_msglen = rec.data_len;
6473
Hanno Becker87b56262019-07-10 14:37:41 +01006474 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard731d7c02020-04-01 09:58:39 +02006475 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker87b56262019-07-10 14:37:41 +01006476 if( ret != 0 )
6477 return( ret );
6478#endif
6479
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006480 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006481 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006482
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6484 "(header)" ) );
6485 }
6486 else
6487 {
6488 /* Skip invalid record and the rest of the datagram */
6489 ssl->next_record_offset = 0;
6490 ssl->in_left = 0;
6491
6492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6493 "(header)" ) );
6494 }
6495
6496 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006497 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006498 }
Hanno Becker87b56262019-07-10 14:37:41 +01006499 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006500#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006501 {
6502 return( ret );
6503 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006504 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006507 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006508 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006509 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006510 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006511 if( ssl->next_record_offset < ssl->in_left )
6512 {
6513 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6514 }
6515 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006516 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006517#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006518#if defined(MBEDTLS_SSL_PROTO_TLS)
6519 {
Hanno Beckere0452772019-07-10 17:12:07 +01006520 /*
6521 * Fetch record contents from underlying transport.
6522 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006523 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006524 if( ret != 0 )
6525 {
6526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6527 return( ret );
6528 }
6529
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006530 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006531 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006532#endif /* MBEDTLS_SSL_PROTO_TLS */
6533
6534 /*
6535 * Decrypt record contents.
6536 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006537
Hanno Beckera89610a2019-07-11 13:07:45 +01006538 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006541 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006542 {
6543 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006544 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006545 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006546 /* Except when waiting for Finished as a bad mac here
6547 * probably means something went wrong in the handshake
6548 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6549 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6550 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6551 {
6552#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6553 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6554 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006555 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006556 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6557 }
6558#endif
6559 return( ret );
6560 }
6561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006563 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6564 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6567 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006568 }
6569#endif
6570
Hanno Becker4a810fb2017-05-24 16:27:30 +01006571 /* As above, invalid records cause
6572 * dismissal of the whole datagram. */
6573
6574 ssl->next_record_offset = 0;
6575 ssl->in_left = 0;
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006578 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006579 }
6580
6581 return( ret );
6582 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006583 MBEDTLS_SSL_TRANSPORT_ELSE
6584#endif /* MBEDTLS_SSL_PROTO_DTLS */
6585#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006586 {
6587 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006588#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6589 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006590 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006591 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006592 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006593 }
6594#endif
6595 return( ret );
6596 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006597#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006598 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006599
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006600
6601 /* Reset in pointers to default state for TLS/DTLS records,
6602 * assuming no CID and no offset between record content and
6603 * record plaintext. */
6604 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006605#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6606 ssl->in_len = ssl->in_cid + rec.cid_len;
6607#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006608 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006609
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006610 /* The record content type may change during decryption,
6611 * so re-read it. */
6612 ssl->in_msgtype = rec.type;
6613 /* Also update the input buffer, because unfortunately
6614 * the server-side ssl_parse_client_hello() reparses the
6615 * record header when receiving a ClientHello initiating
6616 * a renegotiation. */
6617 ssl->in_hdr[0] = rec.type;
6618 ssl->in_msg = rec.buf + rec.data_offset;
6619 ssl->in_msglen = rec.data_len;
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03006620 (void)mbedtls_platform_put_uint16_be( ssl->in_len, rec.data_len );
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006621
Manuel Pégourié-Gonnardae48d862020-01-03 12:18:49 +01006622#if defined(MBEDTLS_ZLIB_SUPPORT)
6623 if( ssl->transform_in != NULL &&
6624 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
6625 {
6626 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
6627 {
6628 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
6629 return( ret );
6630 }
6631
6632 /* Check actual (decompress) record content length against
6633 * configured maximum. */
6634 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
6635 {
6636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
6637 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6638 }
6639 }
6640#endif /* MBEDTLS_ZLIB_SUPPORT */
6641
Simon Butcher99000142016-10-13 17:21:01 +01006642 return( 0 );
6643}
6644
6645int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6646{
6647 int ret;
6648
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006649 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006650 * Handle particular types of records
6651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006653 {
Simon Butcher99000142016-10-13 17:21:01 +01006654 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6655 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006656 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006658 }
6659
Hanno Beckere678eaa2018-08-21 14:57:46 +01006660 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006661 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006662 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006663 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6665 ssl->in_msglen ) );
6666 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006667 }
6668
Hanno Beckere678eaa2018-08-21 14:57:46 +01006669 if( ssl->in_msg[0] != 1 )
6670 {
6671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6672 ssl->in_msg[0] ) );
6673 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6674 }
6675
6676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006678 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6679 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6680 {
6681 if( ssl->handshake == NULL )
6682 {
6683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6684 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6685 }
6686
6687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6688 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6689 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006690#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006691 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006693 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006694 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006695 if( ssl->in_msglen != 2 )
6696 {
6697 /* Note: Standard allows for more than one 2 byte alert
6698 to be packed in a single message, but Mbed TLS doesn't
6699 currently support this. */
6700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6701 ssl->in_msglen ) );
6702 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6703 }
6704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006706 ssl->in_msg[0], ssl->in_msg[1] ) );
6707
6708 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006709 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006711 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006714 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006716 }
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6719 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6722 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006723 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006724
6725#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6726 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6727 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6728 {
Hanno Becker90333da2017-10-10 11:27:13 +01006729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006730 /* Will be handled when trying to parse ServerHello */
6731 return( 0 );
6732 }
6733#endif
6734
6735#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006736 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006737 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6738 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006739 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6740 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6741 {
6742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6743 /* Will be handled in mbedtls_ssl_parse_certificate() */
6744 return( 0 );
6745 }
6746#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6747
6748 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006749 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006750 }
6751
Hanno Beckerc76c6192017-06-06 10:03:17 +01006752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006753 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006754 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006755 /* Drop unexpected ApplicationData records,
6756 * except at the beginning of renegotiations */
6757 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6758 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6759#if defined(MBEDTLS_SSL_RENEGOTIATION)
6760 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6761 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006762#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006763 )
6764 {
6765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6766 return( MBEDTLS_ERR_SSL_NON_FATAL );
6767 }
6768
6769 if( ssl->handshake != NULL &&
6770 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6771 {
6772 ssl_handshake_wrapup_free_hs_transform( ssl );
6773 }
6774 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006775#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006776
Paul Bakker5121ce52009-01-03 21:22:43 +00006777 return( 0 );
6778}
6779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006781{
6782 int ret;
6783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6785 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6786 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006787 {
6788 return( ret );
6789 }
6790
6791 return( 0 );
6792}
6793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006795 unsigned char level,
6796 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006797{
6798 int ret;
6799
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006800 if( ssl == NULL || ssl->conf == NULL )
6801 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006807 ssl->out_msglen = 2;
6808 ssl->out_msg[0] = level;
6809 ssl->out_msg[1] = message;
6810
Hanno Becker67bc7c32018-08-06 11:33:50 +01006811 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006814 return( ret );
6815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006817
6818 return( 0 );
6819}
6820
Hanno Becker17572472019-02-08 07:19:04 +00006821#if defined(MBEDTLS_X509_CRT_PARSE_C)
6822static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6823{
6824#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6825 if( session->peer_cert != NULL )
6826 {
6827 mbedtls_x509_crt_free( session->peer_cert );
6828 mbedtls_free( session->peer_cert );
6829 session->peer_cert = NULL;
6830 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006831#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006832 if( session->peer_cert_digest != NULL )
6833 {
6834 /* Zeroization is not necessary. */
6835 mbedtls_free( session->peer_cert_digest );
6836 session->peer_cert_digest = NULL;
6837 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6838 session->peer_cert_digest_len = 0;
6839 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006840#else
6841 ((void) session);
6842#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006843}
6844#endif /* MBEDTLS_X509_CRT_PARSE_C */
6845
Paul Bakker5121ce52009-01-03 21:22:43 +00006846/*
6847 * Handshake functions
6848 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006849#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006850/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006852{
Hanno Beckerdf645962019-06-26 13:02:22 +01006853 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6854 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006857
Hanno Becker5097cba2019-02-05 13:36:46 +00006858 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006861 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6862 {
6863 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6864 }
6865 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6866 {
6867 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6868 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006869 else
6870 {
6871 ssl->state = MBEDTLS_SSL_INVALID;
6872 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006873 return( 0 );
6874 }
6875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006878}
6879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006881{
Hanno Beckerdf645962019-06-26 13:02:22 +01006882 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6883 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006886
Hanno Becker5097cba2019-02-05 13:36:46 +00006887 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006890 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6891 {
6892 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6893 }
6894 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6895 {
6896 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6897 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006898 else
6899 {
6900 ssl->state = MBEDTLS_SSL_INVALID;
6901 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006902 return( 0 );
6903 }
6904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006907}
Gilles Peskinef9828522017-05-03 12:28:43 +02006908
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006909#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006910/* Some certificate support -> implement write and parse */
6911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006915 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006917 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6918 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006921
Hanno Becker5097cba2019-02-05 13:36:46 +00006922 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006925 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6926 {
6927 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6928 }
6929 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6930 {
6931 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6932 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006933 else
6934 {
6935 ssl->state = MBEDTLS_SSL_INVALID;
6936 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006937 return( 0 );
6938 }
6939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006941 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6942 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006943 {
6944 if( ssl->client_auth == 0 )
6945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Jarno Lamsa2b205162019-11-12 15:36:21 +02006947 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
6948 {
6949 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
6950 }
6951 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
6952 {
6953 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
6954 }
Jarno Lamsab0180092019-11-12 15:46:46 +02006955 else
6956 {
6957 ssl->state = MBEDTLS_SSL_INVALID;
6958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006959 return( 0 );
6960 }
6961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006963 /*
6964 * If using SSLv3 and got no cert, send an Alert message
6965 * (otherwise an empty Certificate message will be sent).
6966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006968 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006969 {
6970 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6972 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6973 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006976 goto write_msg;
6977 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006979 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#endif /* MBEDTLS_SSL_CLI_C */
6981#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006982 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6987 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006988 }
6989 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006990#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006993
6994 /*
6995 * 0 . 0 handshake type
6996 * 1 . 3 handshake length
6997 * 4 . 6 length of all certs
6998 * 7 . 9 length of cert. 1
6999 * 10 . n-1 peer certificate
7000 * n . n+2 length of cert. 2
7001 * n+3 . ... upper level cert, etc.
7002 */
7003 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00007005
Paul Bakker29087132010-03-21 21:03:34 +00007006 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 {
7008 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10007009 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00007010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10007012 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007014 }
7015
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007016 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[i], n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007017
Teppo Järvelin91d79382019-10-02 09:09:31 +03007018 i += 3; mbedtls_platform_memcpy( ssl->out_msg + i, crt->raw.p, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00007019 i += n; crt = crt->next;
7020 }
7021
Arto Kinnunen3d7439e2019-09-10 11:30:40 +03007022 (void)mbedtls_platform_put_uint24_be( &ssl->out_msg[4], ( i - 7 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007023
7024 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7026 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00007027
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02007028#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00007029write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007030#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007031
Jarno Lamsa2b205162019-11-12 15:36:21 +02007032 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7033 {
7034 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7035 }
7036 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7037 {
7038 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7039 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007040 else
7041 {
7042 ssl->state = MBEDTLS_SSL_INVALID;
7043 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007045 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007046 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007048 return( ret );
7049 }
7050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007052
Paul Bakkered27a042013-04-18 22:46:23 +02007053 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007054}
7055
Hanno Becker285ff0c2019-01-31 07:44:03 +00007056#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00007057
7058#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00007059static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7060 unsigned char *crt_buf,
7061 size_t crt_buf_len )
7062{
7063 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7064
7065 if( peer_crt == NULL )
7066 return( -1 );
7067
7068 if( peer_crt->raw.len != crt_buf_len )
7069 return( -1 );
7070
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007071 return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007072}
Hanno Becker5882dd02019-06-06 16:25:57 +01007073#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00007074static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
7075 unsigned char *crt_buf,
7076 size_t crt_buf_len )
7077{
7078 int ret;
7079 unsigned char const * const peer_cert_digest =
7080 ssl->session->peer_cert_digest;
7081 mbedtls_md_type_t const peer_cert_digest_type =
7082 ssl->session->peer_cert_digest_type;
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007083 mbedtls_md_handle_t digest_info =
Hanno Beckerdf759382019-02-05 17:02:46 +00007084 mbedtls_md_info_from_type( peer_cert_digest_type );
7085 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7086 size_t digest_len;
7087
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007088 if( peer_cert_digest == NULL ||
7089 digest_info == MBEDTLS_MD_INVALID_HANDLE )
7090 {
Hanno Beckerdf759382019-02-05 17:02:46 +00007091 return( -1 );
Hanno Beckera5cedbc2019-07-17 11:21:02 +01007092 }
Hanno Beckerdf759382019-02-05 17:02:46 +00007093
7094 digest_len = mbedtls_md_get_size( digest_info );
7095 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
7096 return( -1 );
7097
7098 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
7099 if( ret != 0 )
7100 return( -1 );
7101
Teppo Järvelin61f412e2019-10-03 12:25:22 +03007102 return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
Hanno Beckerdf759382019-02-05 17:02:46 +00007103}
Hanno Becker5882dd02019-06-06 16:25:57 +01007104#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00007105#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00007106
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007107/*
7108 * Once the certificate message is read, parse it into a cert chain and
7109 * perform basic checks, but leave actual verification to the caller
7110 */
Hanno Becker35e41772019-02-05 15:37:23 +00007111static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
7112 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00007113{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007114 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00007115#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7116 int crt_cnt=0;
7117#endif
Paul Bakker23986e52011-04-24 08:57:21 +00007118 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02007119 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00007120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007124 mbedtls_ssl_pend_fatal_alert( ssl,
7125 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007127 }
7128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
7130 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007133 mbedtls_ssl_pend_fatal_alert( ssl,
7134 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007135 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007136 }
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007139
Paul Bakker5121ce52009-01-03 21:22:43 +00007140 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00007142 */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007143 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007144
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00007145 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007149 mbedtls_ssl_pend_fatal_alert( ssl,
7150 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007152 }
7153
Hanno Becker33c3dc82019-01-30 14:46:46 +00007154 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7155 i += 3;
7156
Hanno Becker33c3dc82019-01-30 14:46:46 +00007157 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007158 while( i < ssl->in_hslen )
7159 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007160 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02007161 if ( i + 3 > ssl->in_hslen ) {
7162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007163 mbedtls_ssl_pend_fatal_alert( ssl,
7164 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02007165 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
7166 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007167 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7168 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007169 if( ssl->in_msg[i] != 0 )
7170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007172 mbedtls_ssl_pend_fatal_alert( ssl,
7173 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007175 }
7176
Hanno Becker33c3dc82019-01-30 14:46:46 +00007177 /* Read length of the next CRT in the chain. */
Arto Kinnunen0b62ce82019-09-04 14:04:57 +03007178 n = mbedtls_platform_get_uint16_be( &ssl->in_msg[i + 1] );
Paul Bakker5121ce52009-01-03 21:22:43 +00007179 i += 3;
7180
7181 if( n < 128 || i + n > ssl->in_hslen )
7182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007184 mbedtls_ssl_pend_fatal_alert( ssl,
7185 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007187 }
7188
Hanno Becker33c3dc82019-01-30 14:46:46 +00007189 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00007190#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7191 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01007192 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7193 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00007194 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007195 {
Hanno Becker68b856d2019-02-08 14:00:04 +00007196 /* During client-side renegotiation, check that the server's
7197 * end-CRTs hasn't changed compared to the initial handshake,
7198 * mitigating the triple handshake attack. On success, reuse
7199 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00007200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
7201 if( ssl_check_peer_crt_unchanged( ssl,
7202 &ssl->in_msg[i],
7203 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00007204 {
Hanno Becker35e41772019-02-05 15:37:23 +00007205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007206 mbedtls_ssl_pend_fatal_alert( ssl,
7207 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00007208 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007209 }
Hanno Becker35e41772019-02-05 15:37:23 +00007210
7211 /* Now we can safely free the original chain. */
7212 ssl_clear_peer_cert( ssl->session );
7213 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007214#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7215
Hanno Becker33c3dc82019-01-30 14:46:46 +00007216 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007217#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007218 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007219#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007220 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007221 * it in-place from the input buffer instead of making a copy. */
7222 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7223#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007224 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007225 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007226 case 0: /*ok*/
7227 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7228 /* Ignore certificate with an unknown algorithm: maybe a
7229 prior certificate was already trusted. */
7230 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007231
Hanno Becker33c3dc82019-01-30 14:46:46 +00007232 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7233 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7234 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007235
Hanno Becker33c3dc82019-01-30 14:46:46 +00007236 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7237 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7238 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007239
Hanno Becker33c3dc82019-01-30 14:46:46 +00007240 default:
7241 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7242 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007243 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007244 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7245 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007246 }
7247
7248 i += n;
7249 }
7250
Hanno Becker35e41772019-02-05 15:37:23 +00007251 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007252 return( 0 );
7253}
7254
Hanno Beckerb8a08572019-02-05 12:49:06 +00007255#if defined(MBEDTLS_SSL_SRV_C)
7256static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7257{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007258 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007259 return( -1 );
7260
7261#if defined(MBEDTLS_SSL_PROTO_SSL3)
7262 /*
7263 * Check if the client sent an empty certificate
7264 */
Hanno Becker2881d802019-05-22 14:44:53 +01007265 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007266 {
7267 if( ssl->in_msglen == 2 &&
7268 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7269 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7270 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7271 {
7272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7273 return( 0 );
7274 }
7275
7276 return( -1 );
7277 }
7278#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7279
7280#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7281 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7282 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7283 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7284 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Teppo Järvelin0efac532019-10-04 13:21:08 +03007285 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 +00007286 {
7287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7288 return( 0 );
7289 }
7290
Hanno Beckerb8a08572019-02-05 12:49:06 +00007291#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7292 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007293
7294 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007295}
7296#endif /* MBEDTLS_SSL_SRV_C */
7297
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007298/* Check if a certificate message is expected.
7299 * Return either
7300 * - SSL_CERTIFICATE_EXPECTED, or
7301 * - SSL_CERTIFICATE_SKIP
7302 * indicating whether a Certificate message is expected or not.
7303 */
7304#define SSL_CERTIFICATE_EXPECTED 0
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007305#define SSL_CERTIFICATE_SKIP 0xff
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007306static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7307 int authmode )
7308{
Hanno Becker473f98f2019-06-26 10:27:32 +01007309 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007310 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007311
7312 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7313 return( SSL_CERTIFICATE_SKIP );
7314
7315#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007316 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007317 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007318 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7319 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7320 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007321 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007322 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007323
7324 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7325 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007326 ssl->session_negotiate->verify_result =
7327 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7328 return( SSL_CERTIFICATE_SKIP );
7329 }
7330 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007331#else
7332 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007333#endif /* MBEDTLS_SSL_SRV_C */
7334
7335 return( SSL_CERTIFICATE_EXPECTED );
7336}
7337
Hanno Becker3cf50612019-02-05 14:36:34 +00007338static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007339 volatile int authmode,
Hanno Becker3cf50612019-02-05 14:36:34 +00007340 mbedtls_x509_crt *chain,
7341 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007342{
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007343 volatile int verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007344 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007345 volatile int flow_counter = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01007346 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007347 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007348 mbedtls_x509_crt *ca_chain;
7349 mbedtls_x509_crl *ca_crl;
7350
7351 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
Jarno Lamsae1621d42019-12-19 08:58:56 +02007352 {
Hanno Becker3cf50612019-02-05 14:36:34 +00007353 return( 0 );
Jarno Lamsae1621d42019-12-19 08:58:56 +02007354 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007355
Jarno Lamsae1621d42019-12-19 08:58:56 +02007356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
Hanno Becker3cf50612019-02-05 14:36:34 +00007357#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7358 if( ssl->handshake->sni_ca_chain != NULL )
7359 {
7360 ca_chain = ssl->handshake->sni_ca_chain;
7361 ca_crl = ssl->handshake->sni_ca_crl;
7362 }
7363 else
7364#endif
7365 {
7366 ca_chain = ssl->conf->ca_chain;
7367 ca_crl = ssl->conf->ca_crl;
7368 }
7369
7370 /*
7371 * Main check: verify certificate
7372 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007373 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007374 chain,
7375 ca_chain, ca_crl,
7376 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007377#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007378 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007379#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007380 &ssl->session_negotiate->verify_result,
Hanno Becker9ec3fe02019-07-01 17:36:12 +01007381#if !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
7382 ssl->conf->f_vrfy, ssl->conf->p_vrfy,
7383#endif /* MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
7384 rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007385
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007386 if( verify_ret == 0 )
7387 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007388 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007389 if( verify_ret == 0 )
7390 {
7391 flow_counter++;
7392 }
7393 else
7394 {
7395 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7396 }
7397 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007398 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007399 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007400 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007401 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007402 }
7403
7404#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007405 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007406 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7407#endif
7408
7409 /*
7410 * Secondary checks: always done, but change 'ret' only if it was 0
7411 */
7412
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007413#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker3cf50612019-02-05 14:36:34 +00007414 {
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007415#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeree902df2019-08-23 13:47:47 +01007416 ret = mbedtls_ssl_check_curve_uecc( ssl, MBEDTLS_UECC_DP_SECP256R1 );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007417#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007418 mbedtls_pk_context *pk;
7419 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7420 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007421 {
7422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007423 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007424 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007425
7426 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007427 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007428 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007429 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007430 }
Hanno Becker8c13ee62019-02-26 16:48:17 +00007431
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007432 mbedtls_x509_crt_pk_release( chain );
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007433#endif /* MBEDTLS_USE_TINYCRYPT */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007434
7435 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007436 {
7437 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7438
7439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007440 if( verify_ret == 0 )
7441 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007442 flow_counter++;
7443 }
7444 if( ret == 0 )
7445 {
7446 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007447 }
7448 }
Hanno Becker7e9c2e02019-08-21 17:05:20 +01007449#endif /* MBEDTLS_ECP_C || MEDTLS_USE_TINYCRYPT */
Hanno Becker3cf50612019-02-05 14:36:34 +00007450
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007451 ret = mbedtls_ssl_check_cert_usage( chain,
Hanno Becker3cf50612019-02-05 14:36:34 +00007452 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007453 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7454 MBEDTLS_SSL_IS_CLIENT ),
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007455 &ssl->session_negotiate->verify_result );
7456 if( ret == 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007457 {
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007458 flow_counter++;
7459 }
7460 else
7461 {
7462 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007464 if( verify_ret == 0 )
7465 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007466 }
7467
7468 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7469 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7470 * with details encoded in the verification flags. All other kinds
7471 * of error codes, including those from the user provided f_vrfy
7472 * functions, are treated as fatal and lead to a failure of
7473 * ssl_parse_certificate even if verification was optional. */
7474 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007475 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7476 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007477 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007478 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007479 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7480 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7481 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7482 {
7483 verify_ret = 0;
7484 flow_counter++;
7485 }
7486 else
7487 {
7488 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7489 }
7490 } else {
7491 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007492 }
7493
7494 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7495 {
7496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007497 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007498 flow_counter++;
Hanno Becker3cf50612019-02-05 14:36:34 +00007499 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007500 else
7501 {
7502 flow_counter++;
7503 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007504
Hanno Becker8c13ee62019-02-26 16:48:17 +00007505 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007506 {
7507 uint8_t alert;
7508
7509 /* The certificate may have been rejected for several reasons.
7510 Pick one and send the corresponding alert. Which alert to send
7511 may be a subject of debate in some cases. */
7512 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7513 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7514 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7515 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7516 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7517 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7518 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7519 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7520 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7521 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7522 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7523 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7524 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7525 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7526 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7527 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7528 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7529 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7530 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7531 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7532 else
7533 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007534 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007535 }
7536
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007537 if( verify_ret == 0 &&
7538#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7539 flow_counter == 5 )
7540#else
7541 flow_counter == 4 )
7542#endif
7543 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02007544 mbedtls_platform_random_delay();
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007545 if( verify_ret == 0 &&
7546#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
7547 flow_counter == 5 )
7548#else
7549 flow_counter == 4 )
7550#endif
7551 {
Jarno Lamsae1621d42019-12-19 08:58:56 +02007552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER AUTHENTICATED" ) );
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007553 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
7554 }
7555 else
7556 {
7557 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
7558 }
Jarno Lamsae1621d42019-12-19 08:58:56 +02007559 } else {
7560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PEER NOT AUTHENTICATED, %d", flow_counter));
Jarno Lamsaba4730f2019-12-19 08:42:03 +02007561 }
7562
Hanno Becker3cf50612019-02-05 14:36:34 +00007563#if defined(MBEDTLS_DEBUG_C)
7564 if( ssl->session_negotiate->verify_result != 0 )
7565 {
7566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7567 ssl->session_negotiate->verify_result ) );
7568 }
7569 else
7570 {
7571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7572 }
7573#endif /* MBEDTLS_DEBUG_C */
7574
Hanno Becker8c13ee62019-02-26 16:48:17 +00007575 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007576}
7577
Hanno Becker34106f62019-02-08 14:59:05 +00007578#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007579
7580#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007581static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7582 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007583{
7584 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007585 /* Remember digest of the peer's end-CRT. */
7586 ssl->session_negotiate->peer_cert_digest =
7587 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7588 if( ssl->session_negotiate->peer_cert_digest == NULL )
7589 {
7590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7591 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007592 mbedtls_ssl_pend_fatal_alert( ssl,
7593 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007594
7595 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7596 }
7597
7598 ret = mbedtls_md( mbedtls_md_info_from_type(
7599 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7600 start, len,
7601 ssl->session_negotiate->peer_cert_digest );
7602
7603 ssl->session_negotiate->peer_cert_digest_type =
7604 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7605 ssl->session_negotiate->peer_cert_digest_len =
7606 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7607
7608 return( ret );
7609}
Hanno Becker5882dd02019-06-06 16:25:57 +01007610#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007611
7612static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7613 unsigned char *start, size_t len )
7614{
7615 unsigned char *end = start + len;
7616 int ret;
7617
7618 /* Make a copy of the peer's raw public key. */
7619 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7620 ret = mbedtls_pk_parse_subpubkey( &start, end,
7621 &ssl->handshake->peer_pubkey );
7622 if( ret != 0 )
7623 {
7624 /* We should have parsed the public key before. */
7625 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7626 }
7627
Manuel Pégourié-Gonnard2829bbf2019-09-19 10:45:14 +02007628 ssl->handshake->got_peer_pubkey = 1;
Hanno Becker34106f62019-02-08 14:59:05 +00007629 return( 0 );
7630}
7631#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7632
Hanno Becker3cf50612019-02-05 14:36:34 +00007633int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7634{
7635 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007636 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007637#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7638 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7639 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007640 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007641#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007642 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007643#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007644 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007645 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007646
7647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7648
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007649 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7650 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007651 {
7652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007653 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007654 }
7655
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007656#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7657 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007658 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007659 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007660 chain = ssl->handshake->ecrs_peer_cert;
7661 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007662 goto crt_verify;
7663 }
7664#endif
7665
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007666 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007667 {
7668 /* mbedtls_ssl_read_record may have sent an alert already. We
7669 let it decide whether to alert. */
7670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007671 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007672 }
7673
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007674#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007675 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7676 {
7677 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007678
Hanno Beckerb8a08572019-02-05 12:49:06 +00007679 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007680 ret = 0;
7681 else
7682 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007683
Hanno Becker613d4902019-02-05 13:11:17 +00007684 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007685 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007686#endif /* MBEDTLS_SSL_SRV_C */
7687
Hanno Becker35e41772019-02-05 15:37:23 +00007688 /* Clear existing peer CRT structure in case we tried to
7689 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007690 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007691
7692 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7693 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007694 {
7695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7696 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007697 mbedtls_ssl_pend_fatal_alert( ssl,
7698 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007699
Hanno Beckere4aeb762019-02-05 17:19:52 +00007700 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7701 goto exit;
7702 }
7703 mbedtls_x509_crt_init( chain );
7704
7705 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007706 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007707 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007708
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007709#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7710 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007711 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007712
7713crt_verify:
7714 if( ssl->handshake->ecrs_enabled)
7715 rs_ctx = &ssl->handshake->ecrs_ctx;
7716#endif
7717
Hanno Becker3cf50612019-02-05 14:36:34 +00007718 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007719 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007720 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007721 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007722
Hanno Becker3008d282019-02-05 17:02:28 +00007723#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007725 size_t pk_len;
7726 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007727
Hanno Becker34106f62019-02-08 14:59:05 +00007728 /* We parse the CRT chain without copying, so
7729 * these pointers point into the input buffer,
7730 * and are hence still valid after freeing the
7731 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007732
Hanno Becker5882dd02019-06-06 16:25:57 +01007733#if defined(MBEDTLS_SSL_RENEGOTIATION)
7734 unsigned char *crt_start;
7735 size_t crt_len;
7736
Hanno Becker34106f62019-02-08 14:59:05 +00007737 crt_start = chain->raw.p;
7738 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007739#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007740
Hanno Becker8c13ee62019-02-26 16:48:17 +00007741 pk_start = chain->cache->pk_raw.p;
7742 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007743
7744 /* Free the CRT structures before computing
7745 * digest and copying the peer's public key. */
7746 mbedtls_x509_crt_free( chain );
7747 mbedtls_free( chain );
7748 chain = NULL;
7749
Hanno Becker5882dd02019-06-06 16:25:57 +01007750#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007751 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007753 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007754#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007755
Hanno Becker34106f62019-02-08 14:59:05 +00007756 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007757 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007758 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007759 }
Hanno Becker34106f62019-02-08 14:59:05 +00007760#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7761 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007762 ssl->session_negotiate->peer_cert = chain;
7763 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007764#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007767
Hanno Becker613d4902019-02-05 13:11:17 +00007768exit:
7769
Hanno Beckere4aeb762019-02-05 17:19:52 +00007770 if( ret == 0 )
Jarno Lamsa2b205162019-11-12 15:36:21 +02007771 {
7772 if( ssl->state == MBEDTLS_SSL_CLIENT_CERTIFICATE )
7773 {
7774 ssl->state = MBEDTLS_SSL_CLIENT_KEY_EXCHANGE;
7775 }
7776 else if( ssl->state == MBEDTLS_SSL_SERVER_CERTIFICATE )
7777 {
7778 ssl->state = MBEDTLS_SSL_SERVER_KEY_EXCHANGE;
7779 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007780 else
7781 {
7782 ssl->state = MBEDTLS_SSL_INVALID;
7783 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02007784 }
Hanno Beckere4aeb762019-02-05 17:19:52 +00007785
7786#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7787 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7788 {
7789 ssl->handshake->ecrs_peer_cert = chain;
7790 chain = NULL;
7791 }
7792#endif
7793
7794 if( chain != NULL )
7795 {
7796 mbedtls_x509_crt_free( chain );
7797 mbedtls_free( chain );
7798 }
7799
Paul Bakker5121ce52009-01-03 21:22:43 +00007800 return( ret );
7801}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007802#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007805{
7806 int ret;
7807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007811 ssl->out_msglen = 1;
7812 ssl->out_msg[0] = 1;
7813
Jarno Lamsa2b205162019-11-12 15:36:21 +02007814 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7815 {
7816 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7817 }
7818 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7819 {
7820 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7821 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007822 else
7823 {
7824 ssl->state = MBEDTLS_SSL_INVALID;
7825 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007826
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007827 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007830 return( ret );
7831 }
7832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007834
7835 return( 0 );
7836}
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007839{
7840 int ret;
7841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007843
Hanno Becker327c93b2018-08-15 13:56:18 +01007844 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 return( ret );
7848 }
7849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007853 mbedtls_ssl_pend_fatal_alert( ssl,
7854 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007856 }
7857
Hanno Beckere678eaa2018-08-21 14:57:46 +01007858 /* CCS records are only accepted if they have length 1 and content '1',
7859 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007860
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007861 /*
7862 * Switch to our negotiated transform and session parameters for inbound
7863 * data.
7864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007866 ssl->transform_in = ssl->transform_negotiate;
7867 ssl->session_in = ssl->session_negotiate;
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007870 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007873 ssl_dtls_replay_reset( ssl );
7874#endif
7875
7876 /* Increment epoch */
7877 if( ++ssl->in_epoch == 0 )
7878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007880 /* This is highly unlikely to happen for legitimate reasons, so
7881 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007883 }
7884 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007885 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007887#if defined(MBEDTLS_SSL_PROTO_TLS)
7888 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02007889 mbedtls_platform_memset( ssl->in_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007890 }
7891#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007892
Hanno Beckerf5970a02019-05-08 09:38:41 +01007893 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7896 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007901 mbedtls_ssl_pend_fatal_alert( ssl,
7902 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007904 }
7905 }
7906#endif
7907
Jarno Lamsa2b205162019-11-12 15:36:21 +02007908 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC )
7909 {
7910 ssl->state = MBEDTLS_SSL_CLIENT_FINISHED;
7911 }
7912 else if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
7913 {
7914 ssl->state = MBEDTLS_SSL_SERVER_FINISHED;
7915 }
Jarno Lamsab0180092019-11-12 15:46:46 +02007916 else
7917 {
7918 ssl->state = MBEDTLS_SSL_INVALID;
7919 }
Paul Bakker5121ce52009-01-03 21:22:43 +00007920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007922
7923 return( 0 );
7924}
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007927{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7929 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007930 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007931 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007932#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7934#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007935 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007938 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007941}
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007944{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007946
7947 /*
7948 * Free our handshake params
7949 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007950 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007952 ssl->handshake = NULL;
7953
7954 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007955 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007956 */
7957 if( ssl->transform )
7958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 mbedtls_ssl_transform_free( ssl->transform );
7960 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007961 }
7962 ssl->transform = ssl->transform_negotiate;
7963 ssl->transform_negotiate = NULL;
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007966}
7967
Jarno Lamsae1621d42019-12-19 08:58:56 +02007968int mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007969{
Andrzej Kurekfd56f402020-05-25 11:52:05 -04007970 volatile int ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007971
7972#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Jarno Lamsa015aa442019-12-20 12:09:37 +02007973 volatile const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007974 ? ssl->handshake->sni_authmode
7975 : mbedtls_ssl_conf_get_authmode( ssl->conf );
7976#else
Jarno Lamsa015aa442019-12-20 12:09:37 +02007977 volatile const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Jarno Lamsa489dccd2019-12-19 15:11:16 +02007978#endif
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02007979#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7980 volatile int crt_expected = SSL_CERTIFICATE_EXPECTED;
7981 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007985#if defined(MBEDTLS_SSL_RENEGOTIATION)
7986 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007989 ssl->renego_records_seen = 0;
7990 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007991#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007992
7993 /*
7994 * Free the previous session and switch in the current one
7995 */
Paul Bakker0a597072012-09-25 21:55:46 +00007996 if( ssl->session )
7997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007999 /* RFC 7366 3.1: keep the EtM state */
8000 ssl->session_negotiate->encrypt_then_mac =
8001 ssl->session->encrypt_then_mac;
8002#endif
8003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 mbedtls_ssl_session_free( ssl->session );
8005 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00008006 }
8007 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00008008 ssl->session_negotiate = NULL;
8009
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008010#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00008011 /*
8012 * Add cache entry
8013 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008014 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02008015 ssl->session->id_len != 0 &&
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008016 ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_UNSET )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008017 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008018 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02008020 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02008021#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00008022
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008023 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8024 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8025#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8026 crt_expected == SSL_CERTIFICATE_SKIP )
8027#else
8028 1 )
8029#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008030 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008031 mbedtls_platform_random_delay();
Jarno Lamsaaf60cd72019-12-19 16:45:23 +02008032 if( authmode == MBEDTLS_SSL_VERIFY_NONE ||
8033 authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ||
8034#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8035 crt_expected == SSL_CERTIFICATE_SKIP )
8036#else
8037 1 )
8038#endif
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008039 {
8040 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
8041 }
8042 else
8043 {
8044 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8045 goto cleanup;
8046 }
8047 }
8048
Jarno Lamsae1621d42019-12-19 08:58:56 +02008049#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008050 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008051 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008052 mbedtls_platform_random_delay();
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008053 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Jarno Lamsae1621d42019-12-19 08:58:56 +02008054 {
Jarno Lamsa06164052019-12-19 14:40:36 +02008055 /* When doing session resume, no premaster or peer authentication */
Jarno Lamsae1621d42019-12-19 08:58:56 +02008056 ssl->handshake->peer_authenticated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsa06164052019-12-19 14:40:36 +02008057 ssl->handshake->premaster_generated = MBEDTLS_SSL_FI_FLAG_SET;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008058 }
8059 else
8060 {
8061 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa489dccd2019-12-19 15:11:16 +02008062 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008063 }
8064 }
8065#endif
8066
8067 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8068 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008069 mbedtls_platform_random_delay();
Jarno Lamsae1621d42019-12-19 08:58:56 +02008070 if( ssl->handshake->peer_authenticated == MBEDTLS_SSL_FI_FLAG_SET )
8071 {
8072 ret = 0;
8073 }
8074 else
8075 {
8076 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008077 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008078 }
8079 }
8080 else
8081 {
8082 ret = MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED;
Jarno Lamsa06164052019-12-19 14:40:36 +02008083 goto cleanup;
Jarno Lamsae1621d42019-12-19 08:58:56 +02008084 }
8085
Jarno Lamsa06164052019-12-19 14:40:36 +02008086 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8087 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8088 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8089 {
Arto Kinnunenac6d2262020-01-09 10:11:20 +02008090 mbedtls_platform_random_delay();
Jarno Lamsa06164052019-12-19 14:40:36 +02008091 if( ssl->handshake->hello_random_set == MBEDTLS_SSL_FI_FLAG_SET &&
8092 ssl->handshake->key_derivation_done == MBEDTLS_SSL_FI_FLAG_SET &&
8093 ssl->handshake->premaster_generated == MBEDTLS_SSL_FI_FLAG_SET )
8094 {
8095 ret = 0;
8096 }
8097 else
8098 {
8099 ret = MBEDTLS_ERR_PLATFORM_FAULT_DETECTED;
8100 goto cleanup;
8101 }
8102 }
8103 else
8104 {
8105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "hello random %d", ssl->handshake->hello_random_set ) );
8106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "key_derivation_done %d", ssl->handshake->key_derivation_done ) );
8107 MBEDTLS_SSL_DEBUG_MSG( 3, ( "premaster_generated %d", ssl->handshake->premaster_generated ) );
8108 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8109 }
8110
8111cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008113 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008114 ssl->handshake->flight != NULL )
8115 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02008116 /* Cancel handshake timer */
8117 ssl_set_timer( ssl, 0 );
8118
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008119 /* Keep last flight around in case we need to resend it:
8120 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02008122 }
8123 else
8124#endif
8125 ssl_handshake_wrapup_free_hs_transform( ssl );
8126
Jarno Lamsa2b205162019-11-12 15:36:21 +02008127 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Paul Bakker48916f92012-09-16 19:57:18 +00008128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Jarno Lamsae1621d42019-12-19 08:58:56 +02008130 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00008131}
8132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008134{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008135 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008138
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008139 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008140
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008141 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8142 mbedtls_ssl_suite_get_mac(
8143 mbedtls_ssl_ciphersuite_from_id(
8144 mbedtls_ssl_session_get_ciphersuite(
8145 ssl->session_negotiate ) ) ),
8146 ssl, ssl->out_msg + 4,
8147 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008148
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008149 /*
8150 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8151 * may define some other value. Currently (early 2016), no defined
8152 * ciphersuite does this (and this is unlikely to change as activity has
8153 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8154 */
Hanno Becker2881d802019-05-22 14:44:53 +01008155 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008158 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008159 mbedtls_platform_memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008160#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008161
Paul Bakker5121ce52009-01-03 21:22:43 +00008162 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8164 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008165
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008166#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00008167 /*
8168 * In case of session resuming, invert the client and server
8169 * ChangeCipherSpec messages order.
8170 */
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008171 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008174 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8175 MBEDTLS_SSL_IS_CLIENT )
8176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008178 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008180#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008181 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
8182 MBEDTLS_SSL_IS_SERVER )
8183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01008185 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008186#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008187 }
8188 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008189#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008190 {
8191 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8192 {
8193 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8194 }
8195 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8196 {
8197 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8198 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008199 else
8200 {
8201 ssl->state = MBEDTLS_SSL_INVALID;
8202 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008203 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008204
Paul Bakker48916f92012-09-16 19:57:18 +00008205 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008206 * Switch to our negotiated transform and session parameters for outbound
8207 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008212 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008213 {
8214 unsigned char i;
8215
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008216 /* Remember current epoch settings for resending */
8217 ssl->handshake->alt_transform_out = ssl->transform_out;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008218 mbedtls_platform_memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008219
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008220 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008221 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008222
8223 /* Increment epoch */
8224 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008225 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008226 break;
8227
8228 /* The loop goes to its end iff the counter is wrapping */
8229 if( i == 0 )
8230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8232 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008233 }
8234 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008235 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008237#if defined(MBEDTLS_SSL_PROTO_TLS)
8238 {
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008239 mbedtls_platform_memset( ssl->cur_out_ctr, 0, 8 );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008240 }
8241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008242
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008243 ssl->transform_out = ssl->transform_negotiate;
8244 ssl->session_out = ssl->session_negotiate;
8245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008246#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8247 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008249 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8252 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008253 }
8254 }
8255#endif
8256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008258 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008260#endif
8261
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008262 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008263 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008265 return( ret );
8266 }
8267
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008269 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008270 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8271 {
8272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8273 return( ret );
8274 }
8275#endif
8276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008278
8279 return( 0 );
8280}
8281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008283#define SSL_MAX_HASH_LEN 36
8284#else
8285#define SSL_MAX_HASH_LEN 12
8286#endif
8287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008289{
Paul Bakker23986e52011-04-24 08:57:21 +00008290 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008291 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008292 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008295
Hanno Beckerc2fb7592019-08-15 16:31:23 +01008296 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
8297 mbedtls_ssl_suite_get_mac(
8298 mbedtls_ssl_ciphersuite_from_id(
8299 mbedtls_ssl_session_get_ciphersuite(
8300 ssl->session_negotiate ) ) ),
8301 ssl, buf,
8302 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008303
Hanno Becker327c93b2018-08-15 13:56:18 +01008304 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008307 return( ret );
8308 }
8309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008313 mbedtls_ssl_pend_fatal_alert( ssl,
8314 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008316 }
8317
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008318 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01008320 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008321 hash_len = 36;
8322 else
8323#endif
8324 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8327 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008330 mbedtls_ssl_pend_fatal_alert( ssl,
8331 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008332 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008333 }
8334
Teppo Järvelin707ceb82019-10-04 07:49:39 +03008335 if( mbedtls_platform_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008336 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01008339 mbedtls_ssl_pend_fatal_alert( ssl,
8340 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008342 }
8343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008345 ssl->verify_data_len = hash_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03008346 mbedtls_platform_memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008347#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008348
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008349#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Jarno Lamsa8d09e572019-12-19 15:20:19 +02008350 if( ssl->handshake->resume == MBEDTLS_SSL_FI_FLAG_SET )
Paul Bakker5121ce52009-01-03 21:22:43 +00008351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008353 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008355#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01008357 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008360 }
8361 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008362#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Jarno Lamsa2b205162019-11-12 15:36:21 +02008363 {
8364 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED )
8365 {
8366 ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
8367 }
8368 else if( ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
8369 {
8370 ssl->state = MBEDTLS_SSL_FLUSH_BUFFERS;
8371 }
Jarno Lamsab0180092019-11-12 15:46:46 +02008372 else
8373 {
8374 ssl->state = MBEDTLS_SSL_INVALID;
8375 }
Jarno Lamsa2b205162019-11-12 15:36:21 +02008376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008379 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008381#endif
8382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008384
8385 return( 0 );
8386}
8387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008390 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8393 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8394 mbedtls_md5_init( &handshake->fin_md5 );
8395 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008396 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8397 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008398#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8400#if defined(MBEDTLS_SHA256_C)
8401 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008402 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008403#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#if defined(MBEDTLS_SHA512_C)
8405 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008406 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008409
Hanno Becker7e5437a2017-04-28 17:15:26 +01008410#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8411 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8412 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8413#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#if defined(MBEDTLS_DHM_C)
8416 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008417#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418#if defined(MBEDTLS_ECDH_C)
8419 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008420#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008421#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008422 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008423#if defined(MBEDTLS_SSL_CLI_C)
8424 handshake->ecjpake_cache = NULL;
8425 handshake->ecjpake_cache_len = 0;
8426#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008427#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008428
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008429#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008430 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008431#endif
8432
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008433#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8434 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8435#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00008436
8437#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8438 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8439 mbedtls_pk_init( &handshake->peer_pubkey );
8440#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008441}
8442
Hanno Becker611a83b2018-01-03 14:27:32 +00008443void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008444{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008447 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8448 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008449
Hanno Becker92231322018-01-03 15:32:51 +00008450#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451 mbedtls_md_init( &transform->md_ctx_enc );
8452 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00008453#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008454}
8455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008457{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008459}
8460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008461static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008462{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008463 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008464 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008466 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008468 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008469 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008470
8471 /*
8472 * Either the pointers are now NULL or cleared properly and can be freed.
8473 * Now allocate missing structures.
8474 */
8475 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008476 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008477 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008478 }
Paul Bakker48916f92012-09-16 19:57:18 +00008479
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008480 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008481 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008482 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008483 }
Paul Bakker48916f92012-09-16 19:57:18 +00008484
Paul Bakker82788fb2014-10-20 13:59:19 +02008485 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008486 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008487 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008488 }
Paul Bakker48916f92012-09-16 19:57:18 +00008489
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008490 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008491 if( ssl->handshake == NULL ||
8492 ssl->transform_negotiate == NULL ||
8493 ssl->session_negotiate == NULL )
8494 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008497 mbedtls_free( ssl->handshake );
8498 mbedtls_free( ssl->transform_negotiate );
8499 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008500
8501 ssl->handshake = NULL;
8502 ssl->transform_negotiate = NULL;
8503 ssl->session_negotiate = NULL;
8504
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008505 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008506 }
8507
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008508 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008510 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008511 ssl_handshake_params_init( ssl->handshake );
8512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008514 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008515 {
8516 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008517
Hanno Becker2d9623f2019-06-13 12:07:05 +01008518 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008519 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8520 else
8521 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8522 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008523#endif
8524
Paul Bakker48916f92012-09-16 19:57:18 +00008525 return( 0 );
8526}
8527
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008528#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008529/* Dummy cookie callbacks for defaults */
8530static int ssl_cookie_write_dummy( void *ctx,
8531 unsigned char **p, unsigned char *end,
8532 const unsigned char *cli_id, size_t cli_id_len )
8533{
8534 ((void) ctx);
8535 ((void) p);
8536 ((void) end);
8537 ((void) cli_id);
8538 ((void) cli_id_len);
8539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008541}
8542
8543static int ssl_cookie_check_dummy( void *ctx,
8544 const unsigned char *cookie, size_t cookie_len,
8545 const unsigned char *cli_id, size_t cli_id_len )
8546{
8547 ((void) ctx);
8548 ((void) cookie);
8549 ((void) cookie_len);
8550 ((void) cli_id);
8551 ((void) cli_id_len);
8552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008554}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008555#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008556
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008557/* Once ssl->out_hdr as the address of the beginning of the
8558 * next outgoing record is set, deduce the other pointers.
8559 *
8560 * Note: For TLS, we save the implicit record sequence number
8561 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8562 * and the caller has to make sure there's space for this.
8563 */
8564
8565static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8566 mbedtls_ssl_transform *transform )
8567{
8568#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008569 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008570 {
8571 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008572#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008573 ssl->out_cid = ssl->out_ctr + 8;
8574 ssl->out_len = ssl->out_cid;
8575 if( transform != NULL )
8576 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008577#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008578 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008579#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008580 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008581 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008582 MBEDTLS_SSL_TRANSPORT_ELSE
8583#endif /* MBEDTLS_SSL_PROTO_DTLS */
8584#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008585 {
8586 ssl->out_ctr = ssl->out_hdr - 8;
8587 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008588#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008589 ssl->out_cid = ssl->out_len;
8590#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008591 ssl->out_iv = ssl->out_hdr + 5;
8592 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008593#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008594
8595 /* Adjust out_msg to make space for explicit IV, if used. */
8596 if( transform != NULL &&
Hanno Becker7bcf2b52019-07-26 09:02:40 +01008597 mbedtls_ssl_ver_geq(
8598 mbedtls_ssl_get_minor_ver( ssl ),
8599 MBEDTLS_SSL_MINOR_VERSION_2 ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008600 {
8601 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8602 }
8603 else
8604 ssl->out_msg = ssl->out_iv;
8605}
8606
8607/* Once ssl->in_hdr as the address of the beginning of the
8608 * next incoming record is set, deduce the other pointers.
8609 *
8610 * Note: For TLS, we save the implicit record sequence number
8611 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8612 * and the caller has to make sure there's space for this.
8613 */
8614
Hanno Beckerf5970a02019-05-08 09:38:41 +01008615static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008616{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008617 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008618 * of unprotected TLS/DTLS records, with ssl->in_msg
8619 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008620 *
8621 * When decrypting a protected record, ssl->in_msg
8622 * will be shifted to point to the beginning of the
8623 * record plaintext.
8624 */
8625
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008626#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008627 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008628 {
Hanno Becker70e79282019-05-03 14:34:53 +01008629 /* This sets the header pointers to match records
8630 * without CID. When we receive a record containing
8631 * a CID, the fields are shifted accordingly in
8632 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008633 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008634#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008635 ssl->in_cid = ssl->in_ctr + 8;
8636 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008637#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008638 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008639#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008640 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008641 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008642 MBEDTLS_SSL_TRANSPORT_ELSE
8643#endif /* MBEDTLS_SSL_PROTO_DTLS */
8644#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008645 {
8646 ssl->in_ctr = ssl->in_hdr - 8;
8647 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008648#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008649 ssl->in_cid = ssl->in_len;
8650#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008651 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008652 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008653#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008654}
8655
Paul Bakker5121ce52009-01-03 21:22:43 +00008656/*
8657 * Initialize an SSL context
8658 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008659void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8660{
8661 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8662}
8663
8664/*
8665 * Setup an SSL context
8666 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008667
8668static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8669{
8670 /* Set the incoming and outgoing record pointers. */
8671#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008672 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008673 {
8674 ssl->out_hdr = ssl->out_buf;
8675 ssl->in_hdr = ssl->in_buf;
8676 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008677 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008678#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008679#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008680 {
8681 ssl->out_hdr = ssl->out_buf + 8;
8682 ssl->in_hdr = ssl->in_buf + 8;
8683 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008684#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008685
8686 /* Derive other internal pointers. */
8687 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008688 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008689}
8690
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008691int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008692 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008693{
Paul Bakker48916f92012-09-16 19:57:18 +00008694 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008695
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008696 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008697
Hanno Beckeref982d52019-07-23 15:56:18 +01008698#if defined(MBEDTLS_USE_TINYCRYPT)
8699 uECC_set_rng( &uecc_rng_wrapper );
8700#endif
8701
Paul Bakker62f2dee2012-09-28 07:31:51 +00008702 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008703 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008704 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008705
8706 /* Set to NULL in case of an error condition */
8707 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008708
Angus Grattond8213d02016-05-25 20:56:48 +10008709 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8710 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008711 {
Angus Grattond8213d02016-05-25 20:56:48 +10008712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008713 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008714 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008715 }
8716
8717 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8718 if( ssl->out_buf == NULL )
8719 {
8720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008721 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008722 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008723 }
8724
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008725 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008726
Paul Bakker48916f92012-09-16 19:57:18 +00008727 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008728 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008729
Hanno Beckerc8f52992019-07-25 11:15:08 +01008730 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008731
Paul Bakker5121ce52009-01-03 21:22:43 +00008732 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008733
8734error:
8735 mbedtls_free( ssl->in_buf );
8736 mbedtls_free( ssl->out_buf );
8737
8738 ssl->conf = NULL;
8739
8740 ssl->in_buf = NULL;
8741 ssl->out_buf = NULL;
8742
8743 ssl->in_hdr = NULL;
8744 ssl->in_ctr = NULL;
8745 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008746 ssl->in_msg = NULL;
8747
8748 ssl->out_hdr = NULL;
8749 ssl->out_ctr = NULL;
8750 ssl->out_len = NULL;
8751 ssl->out_iv = NULL;
8752 ssl->out_msg = NULL;
8753
k-stachowiak9f7798e2018-07-31 16:52:32 +02008754 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008755}
8756
8757/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008758 * Reset an initialized and used SSL context for re-use while retaining
8759 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008760 *
8761 * If partial is non-zero, keep data in the input buffer and client ID.
8762 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008763 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008764static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008765{
Paul Bakker48916f92012-09-16 19:57:18 +00008766 int ret;
8767
Hanno Becker7e772132018-08-10 12:38:21 +01008768#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8769 !defined(MBEDTLS_SSL_SRV_C)
8770 ((void) partial);
8771#endif
8772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008773 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008774
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008775 /* Cancel any possibly running timer */
8776 ssl_set_timer( ssl, 0 );
8777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008778#if defined(MBEDTLS_SSL_RENEGOTIATION)
8779 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008780 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008781
8782 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008783 mbedtls_platform_memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8784 mbedtls_platform_memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008786 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008787
Paul Bakker7eb013f2011-10-06 12:37:39 +00008788 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008789 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008790
8791 ssl->in_msgtype = 0;
8792 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008793#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008794 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008795 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008797#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008798 ssl_dtls_replay_reset( ssl );
8799#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008800
8801 ssl->in_hslen = 0;
8802 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008803
8804 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008805
8806 ssl->out_msgtype = 0;
8807 ssl->out_msglen = 0;
8808 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8810 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008811 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008812#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008813
Hanno Becker19859472018-08-06 09:40:20 +01008814 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8815
Paul Bakker48916f92012-09-16 19:57:18 +00008816 ssl->transform_in = NULL;
8817 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008818
Hanno Becker78640902018-08-13 16:35:15 +01008819 ssl->session_in = NULL;
8820 ssl->session_out = NULL;
8821
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008822 mbedtls_platform_memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008823
8824#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008825 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008826#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8827 {
8828 ssl->in_left = 0;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +02008829 mbedtls_platform_memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008830 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8833 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8836 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8839 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008840 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008841 }
8842#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008843
Paul Bakker48916f92012-09-16 19:57:18 +00008844 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846 mbedtls_ssl_transform_free( ssl->transform );
8847 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008848 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008849 }
Paul Bakker48916f92012-09-16 19:57:18 +00008850
Paul Bakkerc0463502013-02-14 11:19:38 +01008851 if( ssl->session )
8852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 mbedtls_ssl_session_free( ssl->session );
8854 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008855 ssl->session = NULL;
8856 }
8857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008859 ssl->alpn_chosen = NULL;
8860#endif
8861
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008862#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008863#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008864 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008865#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008866 {
8867 mbedtls_free( ssl->cli_id );
8868 ssl->cli_id = NULL;
8869 ssl->cli_id_len = 0;
8870 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008871#endif
8872
Paul Bakker48916f92012-09-16 19:57:18 +00008873 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8874 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008875
8876 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008877}
8878
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008879/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008880 * Reset an initialized and used SSL context for re-use while retaining
8881 * all application-set variables, function pointers and data.
8882 */
8883int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8884{
8885 return( ssl_session_reset_int( ssl, 0 ) );
8886}
8887
8888/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008889 * SSL set accessors
8890 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008891#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008892void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008893{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008894 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008895}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008896#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008897
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008898void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008899{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008900 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008901}
8902
Hanno Becker7f376f42019-06-12 16:20:48 +01008903#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8904 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008905void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008906{
Hanno Becker7f376f42019-06-12 16:20:48 +01008907 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008908}
Hanno Becker7f376f42019-06-12 16:20:48 +01008909#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008910
Hanno Beckerde671542019-06-12 16:30:46 +01008911#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8912 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8913void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8914 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008915{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008916 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008917}
Hanno Beckerde671542019-06-12 16:30:46 +01008918#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008921
Hanno Becker1841b0a2018-08-24 11:13:57 +01008922void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8923 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008924{
8925 ssl->disable_datagram_packing = !allow_packing;
8926}
8927
Hanno Becker1f835fa2019-06-13 10:14:59 +01008928#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8929 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008930void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8931 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008932{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008933 conf->hs_timeout_min = min;
8934 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008935}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008936#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8937 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8938void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8939 uint32_t min, uint32_t max )
8940{
8941 ((void) conf);
8942 ((void) min);
8943 ((void) max);
8944}
8945#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8946 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8947
8948#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008949
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008950void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008951{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008952#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8953 conf->authmode = authmode;
8954#else
8955 ((void) conf);
8956 ((void) authmode);
8957#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008958}
8959
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008960#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8961 !defined(MBEDTLS_X509_REMOVE_VERIFY_CALLBACK)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008962void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008963 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008964 void *p_vrfy )
8965{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008966 conf->f_vrfy = f_vrfy;
8967 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008968}
Hanno Becker9ec3fe02019-07-01 17:36:12 +01008969#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_VERIFY_CALLBACK */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008970
Hanno Beckerece325c2019-06-13 15:39:27 +01008971#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008972void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008973 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008974 void *p_rng )
8975{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008976 conf->f_rng = f_rng;
8977 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008978}
Hanno Beckerece325c2019-06-13 15:39:27 +01008979#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008980
Hanno Becker14a4a442019-07-02 17:00:34 +01008981#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008982void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008983 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008984 void *p_dbg )
8985{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008986 conf->f_dbg = f_dbg;
8987 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008988}
Hanno Becker14a4a442019-07-02 17:00:34 +01008989#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008990
Hanno Beckera58a8962019-06-13 16:11:15 +01008991#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8992 !defined(MBEDTLS_SSL_CONF_SEND) && \
8993 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008994void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008995 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008996 mbedtls_ssl_send_t *f_send,
8997 mbedtls_ssl_recv_t *f_recv,
8998 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008999{
Hanno Beckera58a8962019-06-13 16:11:15 +01009000 ssl->p_bio = p_bio;
9001 ssl->f_send = f_send;
9002 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009003 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009004}
Hanno Beckera58a8962019-06-13 16:11:15 +01009005#else
9006void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
9007 void *p_bio )
9008{
9009 ssl->p_bio = p_bio;
9010}
9011#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009012
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02009013#if defined(MBEDTLS_SSL_PROTO_DTLS)
9014void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
9015{
9016 ssl->mtu = mtu;
9017}
9018#endif
9019
Hanno Becker1f835fa2019-06-13 10:14:59 +01009020#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009021void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01009022{
9023 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009024}
Hanno Becker1f835fa2019-06-13 10:14:59 +01009025#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02009026
Hanno Becker0ae6b242019-06-13 16:45:36 +01009027#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
9028 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009029void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
9030 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00009031 mbedtls_ssl_set_timer_t *f_set_timer,
9032 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009033{
9034 ssl->p_timer = p_timer;
9035 ssl->f_set_timer = f_set_timer;
9036 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009037 /* Make sure we start with no timer running */
9038 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009039}
Hanno Becker0ae6b242019-06-13 16:45:36 +01009040#else
9041void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
9042 void *p_timer )
9043{
9044 ssl->p_timer = p_timer;
9045 /* Make sure we start with no timer running */
9046 ssl_set_timer( ssl, 0 );
9047}
9048#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02009049
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009050#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009051void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009052 void *p_cache,
9053 int (*f_get_cache)(void *, mbedtls_ssl_session *),
9054 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00009055{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01009056 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009057 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009058 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00009059}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009060#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009061
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009062#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00009064{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009065 int ret;
9066
9067 if( ssl == NULL ||
9068 session == NULL ||
9069 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009070 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009073 }
9074
Hanno Becker58fccf22019-02-06 14:30:46 +00009075 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
9076 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009077 return( ret );
9078
Jarno Lamsa8d09e572019-12-19 15:20:19 +02009079 ssl->handshake->resume = MBEDTLS_SSL_FI_FLAG_SET;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02009080
9081 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00009082}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03009083#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009084
Hanno Becker73f4cb12019-06-27 13:51:07 +01009085#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009086void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009087 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00009088{
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009089 conf->ciphersuite_list[0] = ciphersuites;
9090 conf->ciphersuite_list[1] = ciphersuites;
9091 conf->ciphersuite_list[2] = ciphersuites;
9092 conf->ciphersuite_list[3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009093}
9094
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009095void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02009096 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009097 int major, int minor )
9098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009100 return;
9101
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009102 if( mbedtls_ssl_ver_lt( minor, MBEDTLS_SSL_MINOR_VERSION_0 ) ||
9103 mbedtls_ssl_ver_gt( minor, MBEDTLS_SSL_MINOR_VERSION_3 ) )
9104 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009105 return;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009106 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02009107
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009108 conf->ciphersuite_list[mbedtls_ssl_minor_ver_index( minor )] =
9109 ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00009110}
Hanno Becker73f4cb12019-06-27 13:51:07 +01009111#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00009112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009114void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01009115 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02009116{
9117 conf->cert_profile = profile;
9118}
9119
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009120/* Append a new keycert entry to a (possibly empty) list */
9121static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
9122 mbedtls_x509_crt *cert,
9123 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009124{
niisato8ee24222018-06-25 19:05:48 +09009125 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009126
niisato8ee24222018-06-25 19:05:48 +09009127 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
9128 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009129 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009130
niisato8ee24222018-06-25 19:05:48 +09009131 new_cert->cert = cert;
9132 new_cert->key = key;
9133 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009134
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009135 /* Update head is the list was null, else add to the end */
9136 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01009137 {
niisato8ee24222018-06-25 19:05:48 +09009138 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01009139 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009140 else
9141 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009142 mbedtls_ssl_key_cert *cur = *head;
9143 while( cur->next != NULL )
9144 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09009145 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009146 }
9147
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009148 return( 0 );
9149}
9150
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009151int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02009152 mbedtls_x509_crt *own_cert,
9153 mbedtls_pk_context *pk_key )
9154{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02009155 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02009156}
9157
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009158void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009159 mbedtls_x509_crt *ca_chain,
9160 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009161{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009162 conf->ca_chain = ca_chain;
9163 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00009164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00009166
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009167#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9168int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
9169 mbedtls_x509_crt *own_cert,
9170 mbedtls_pk_context *pk_key )
9171{
9172 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
9173 own_cert, pk_key ) );
9174}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02009175
9176void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
9177 mbedtls_x509_crt *ca_chain,
9178 mbedtls_x509_crl *ca_crl )
9179{
9180 ssl->handshake->sni_ca_chain = ca_chain;
9181 ssl->handshake->sni_ca_crl = ca_crl;
9182}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02009183
9184void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
9185 int authmode )
9186{
9187 ssl->handshake->sni_authmode = authmode;
9188}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02009189#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9190
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009191#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009192/*
9193 * Set EC J-PAKE password for current handshake
9194 */
9195int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
9196 const unsigned char *pw,
9197 size_t pw_len )
9198{
9199 mbedtls_ecjpake_role role;
9200
Janos Follath8eb64132016-06-03 15:40:57 +01009201 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9203
Hanno Becker2d9623f2019-06-13 12:07:05 +01009204 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009205 role = MBEDTLS_ECJPAKE_SERVER;
9206 else
9207 role = MBEDTLS_ECJPAKE_CLIENT;
9208
9209 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9210 role,
9211 MBEDTLS_MD_SHA256,
9212 MBEDTLS_ECP_DP_SECP256R1,
9213 pw, pw_len ) );
9214}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009215#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009218int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009219 const unsigned char *psk, size_t psk_len,
9220 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009221{
Paul Bakker6db455e2013-09-18 17:29:31 +02009222 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02009224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009225 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9226 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01009227
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009228 /* Identity len will be encoded on two bytes */
9229 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009230 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009231 {
9232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9233 }
9234
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009235 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02009236 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009237 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009238
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009239 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009240 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009241 conf->psk_len = 0;
9242 }
9243 if( conf->psk_identity != NULL )
9244 {
9245 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02009246 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009247 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02009248 }
9249
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009250 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
9251 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05009252 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009253 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009254 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009255 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02009256 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009257 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05009258 }
Paul Bakker6db455e2013-09-18 17:29:31 +02009259
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009260 conf->psk_len = psk_len;
9261 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02009262
Teppo Järvelin91d79382019-10-02 09:09:31 +03009263 mbedtls_platform_memcpy( conf->psk, psk, conf->psk_len );
9264 mbedtls_platform_memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02009265
9266 return( 0 );
9267}
9268
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009269int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9270 const unsigned char *psk, size_t psk_len )
9271{
9272 if( psk == NULL || ssl->handshake == NULL )
9273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9274
9275 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9276 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9277
9278 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009279 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009280 mbedtls_platform_zeroize( ssl->handshake->psk,
9281 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01009282 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01009283 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01009284 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009285
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009286 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009287 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009288
9289 ssl->handshake->psk_len = psk_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +03009290 mbedtls_platform_memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009291
9292 return( 0 );
9293}
9294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009295void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009297 size_t),
9298 void *p_psk )
9299{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009300 conf->f_psk = f_psk;
9301 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009302}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009304
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009305#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009306
9307#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009308int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009309{
9310 int ret;
9311
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009312 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9313 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9314 {
9315 mbedtls_mpi_free( &conf->dhm_P );
9316 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009317 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009318 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009319
9320 return( 0 );
9321}
Hanno Becker470a8c42017-10-04 15:28:46 +01009322#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009323
Hanno Beckera90658f2017-10-04 15:29:08 +01009324int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9325 const unsigned char *dhm_P, size_t P_len,
9326 const unsigned char *dhm_G, size_t G_len )
9327{
9328 int ret;
9329
9330 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9331 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9332 {
9333 mbedtls_mpi_free( &conf->dhm_P );
9334 mbedtls_mpi_free( &conf->dhm_G );
9335 return( ret );
9336 }
9337
9338 return( 0 );
9339}
Paul Bakker5121ce52009-01-03 21:22:43 +00009340
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009341int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009342{
9343 int ret;
9344
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009345 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9346 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9347 {
9348 mbedtls_mpi_free( &conf->dhm_P );
9349 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009350 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009351 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009352
9353 return( 0 );
9354}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009355#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009356
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009357#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9358/*
9359 * Set the minimum length for Diffie-Hellman parameters
9360 */
9361void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9362 unsigned int bitlen )
9363{
9364 conf->dhm_min_bitlen = bitlen;
9365}
9366#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9367
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009368#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009369/*
9370 * Set allowed/preferred hashes for handshake signatures
9371 */
9372void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9373 const int *hashes )
9374{
Hanno Becker56595f42019-06-19 16:31:38 +01009375#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009376 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01009377#else
9378 ((void) conf);
9379 ((void) hashes);
9380#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009381}
Hanno Becker947194e2017-04-07 13:25:49 +01009382#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009383
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009384#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01009385#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009386/*
9387 * Set the allowed elliptic curves
9388 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009389void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009390 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009391{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009392 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009393}
Hanno Beckerc1096e72019-06-19 12:30:41 +01009394#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01009395#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009396
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009397#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009398int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009399{
Hanno Becker947194e2017-04-07 13:25:49 +01009400 /* Initialize to suppress unnecessary compiler warning */
9401 size_t hostname_len = 0;
9402
9403 /* Check if new hostname is valid before
9404 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009405 if( hostname != NULL )
9406 {
9407 hostname_len = strlen( hostname );
9408
9409 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9411 }
9412
9413 /* Now it's clear that we will overwrite the old hostname,
9414 * so we can free it safely */
9415
9416 if( ssl->hostname != NULL )
9417 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009418 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009419 mbedtls_free( ssl->hostname );
9420 }
9421
9422 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009423
Paul Bakker5121ce52009-01-03 21:22:43 +00009424 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009425 {
9426 ssl->hostname = NULL;
9427 }
9428 else
9429 {
9430 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009431 if( ssl->hostname == NULL )
9432 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009433
Teppo Järvelin6f4e0302019-10-04 13:53:53 +03009434 /* Not using more secure mbedtls_platform_memcpy as hostname is public in initial handshake */
9435 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009436
Hanno Becker947194e2017-04-07 13:25:49 +01009437 ssl->hostname[hostname_len] = '\0';
9438 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009439
9440 return( 0 );
9441}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03009442#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009443
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009444#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009445void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009447 const unsigned char *, size_t),
9448 void *p_sni )
9449{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009450 conf->f_sni = f_sni;
9451 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009456int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009457{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009458 size_t cur_len, tot_len;
9459 const char **p;
9460
9461 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009462 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9463 * MUST NOT be truncated."
9464 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009465 */
9466 tot_len = 0;
9467 for( p = protos; *p != NULL; p++ )
9468 {
9469 cur_len = strlen( *p );
9470 tot_len += cur_len;
9471
9472 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009474 }
9475
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009476 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009477
9478 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009479}
9480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009481const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009482{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009483 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009485#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009486
Hanno Becker33b9b252019-07-05 11:23:25 +01009487#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9488 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9489void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9490 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009491{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009492 conf->max_major_ver = major;
9493 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009494}
Hanno Becker33b9b252019-07-05 11:23:25 +01009495#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9496 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009497
Hanno Becker33b9b252019-07-05 11:23:25 +01009498#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9499 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9500void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9501 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009502{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009503 conf->min_major_ver = major;
9504 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009505}
Hanno Becker33b9b252019-07-05 11:23:25 +01009506#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9507 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009510void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009511{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009512 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009513}
9514#endif
9515
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009516#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009517void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9518 char cert_req_ca_list )
9519{
9520 conf->cert_req_ca_list = cert_req_ca_list;
9521}
9522#endif
9523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009525void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009526{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009527 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009528}
9529#endif
9530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009532#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009533void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009534{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009535 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009536}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009537#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9538#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009539void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009540 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009541{
9542 conf->enforce_extended_master_secret = ems_enf;
9543}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009544#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009545#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009546
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009547#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009548void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009549{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009550 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009551}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009552#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009555int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009558 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009561 }
9562
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009563 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009564
9565 return( 0 );
9566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009570void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009572 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009577void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009578{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009579 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009580}
9581#endif
9582
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009583#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009584void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009585{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009586 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009587}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009588#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009591void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009593 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009594}
9595
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009596void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009598 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009599}
9600
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009601void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009602 const unsigned char period[8] )
9603{
Teppo Järvelin91d79382019-10-02 09:09:31 +03009604 mbedtls_platform_memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009609#if defined(MBEDTLS_SSL_CLI_C)
9610void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009611{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009612 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009613}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009614#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009615
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009616#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009617void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9618 mbedtls_ssl_ticket_write_t *f_ticket_write,
9619 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9620 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009621{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009622 conf->f_ticket_write = f_ticket_write;
9623 conf->f_ticket_parse = f_ticket_parse;
9624 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009625}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009626#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009628
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009629#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9630void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9631 mbedtls_ssl_export_keys_t *f_export_keys,
9632 void *p_export_keys )
9633{
9634 conf->f_export_keys = f_export_keys;
9635 conf->p_export_keys = p_export_keys;
9636}
9637#endif
9638
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009639#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009640void mbedtls_ssl_conf_async_private_cb(
9641 mbedtls_ssl_config *conf,
9642 mbedtls_ssl_async_sign_t *f_async_sign,
9643 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9644 mbedtls_ssl_async_resume_t *f_async_resume,
9645 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009646 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009647{
9648 conf->f_async_sign_start = f_async_sign;
9649 conf->f_async_decrypt_start = f_async_decrypt;
9650 conf->f_async_resume = f_async_resume;
9651 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009652 conf->p_async_config_data = async_config_data;
9653}
9654
Gilles Peskine8f97af72018-04-26 11:46:10 +02009655void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9656{
9657 return( conf->p_async_config_data );
9658}
9659
Gilles Peskine1febfef2018-04-30 11:54:39 +02009660void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009661{
9662 if( ssl->handshake == NULL )
9663 return( NULL );
9664 else
9665 return( ssl->handshake->user_async_ctx );
9666}
9667
Gilles Peskine1febfef2018-04-30 11:54:39 +02009668void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009669 void *ctx )
9670{
9671 if( ssl->handshake != NULL )
9672 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009673}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009674#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009675
Paul Bakker5121ce52009-01-03 21:22:43 +00009676/*
9677 * SSL get accessors
9678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009680{
9681 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9682}
9683
Hanno Becker8b170a02017-10-10 11:51:19 +01009684int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9685{
9686 /*
9687 * Case A: We're currently holding back
9688 * a message for further processing.
9689 */
9690
9691 if( ssl->keep_current_message == 1 )
9692 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009694 return( 1 );
9695 }
9696
9697 /*
9698 * Case B: Further records are pending in the current datagram.
9699 */
9700
9701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009702 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009703 ssl->in_left > ssl->next_record_offset )
9704 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009705 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009706 return( 1 );
9707 }
9708#endif /* MBEDTLS_SSL_PROTO_DTLS */
9709
9710 /*
9711 * Case C: A handshake message is being processed.
9712 */
9713
Hanno Becker8b170a02017-10-10 11:51:19 +01009714 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9715 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009717 return( 1 );
9718 }
9719
9720 /*
9721 * Case D: An application data message is being processed
9722 */
9723 if( ssl->in_offt != NULL )
9724 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009725 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009726 return( 1 );
9727 }
9728
9729 /*
9730 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009731 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009732 * we implement support for multiple alerts in single records.
9733 */
9734
9735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9736 return( 0 );
9737}
9738
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009739uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009740{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009741 if( ssl->session != NULL )
9742 return( ssl->session->verify_result );
9743
9744 if( ssl->session_negotiate != NULL )
9745 return( ssl->session_negotiate->verify_result );
9746
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009747 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009748}
9749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009751{
Hanno Beckere02758c2019-06-26 15:31:31 +01009752 int suite;
9753
Paul Bakker926c8e42013-03-06 10:23:34 +01009754 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009755 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009756
Hanno Beckere02758c2019-06-26 15:31:31 +01009757 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9758 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009759}
9760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009764 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009765 {
Hanno Becker2881d802019-05-22 14:44:53 +01009766 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009769 return( "DTLSv1.0" );
9770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009772 return( "DTLSv1.2" );
9773
9774 default:
9775 return( "unknown (DTLS)" );
9776 }
9777 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009778 MBEDTLS_SSL_TRANSPORT_ELSE
9779#endif /* MBEDTLS_SSL_PROTO_DTLS */
9780#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009781 {
Hanno Becker2881d802019-05-22 14:44:53 +01009782 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009783 {
9784 case MBEDTLS_SSL_MINOR_VERSION_0:
9785 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009786
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009787 case MBEDTLS_SSL_MINOR_VERSION_1:
9788 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009789
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009790 case MBEDTLS_SSL_MINOR_VERSION_2:
9791 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009792
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009793 case MBEDTLS_SSL_MINOR_VERSION_3:
9794 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009795
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009796 default:
9797 return( "unknown" );
9798 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009799 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009800#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009801}
9802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009804{
Hanno Becker3136ede2018-08-17 15:28:19 +01009805 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009807
Hanno Becker43395762019-05-03 14:46:38 +01009808 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9809
Hanno Becker78640902018-08-13 16:35:15 +01009810 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009811 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813#if defined(MBEDTLS_ZLIB_SUPPORT)
9814 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9815 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009816#endif
9817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009819 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009820#if defined(MBEDTLS_GCM_C) || \
9821 defined(MBEDTLS_CCM_C) || \
9822 defined(MBEDTLS_CHACHAPOLY_C)
9823#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009825#endif
9826#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009828#endif
9829#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009830 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009831#endif
9832 transform_expansion =
9833 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009834 break;
9835
Hanno Beckera9d5c452019-07-25 16:47:12 +01009836#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9837 MBEDTLS_CHACHAPOLY_C */
9838
9839#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9840 case MBEDTLS_MODE_STREAM:
9841 transform_expansion = transform->maclen;
9842 break;
9843#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9844
9845#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009847 {
9848 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009849
9850 block_size = mbedtls_cipher_get_block_size(
9851 &transform->cipher_ctx_enc );
9852
Hanno Becker3136ede2018-08-17 15:28:19 +01009853 /* Expansion due to the addition of the MAC. */
9854 transform_expansion += transform->maclen;
9855
9856 /* Expansion due to the addition of CBC padding;
9857 * Theoretically up to 256 bytes, but we never use
9858 * more than the block size of the underlying cipher. */
9859 transform_expansion += block_size;
9860
9861 /* For TLS 1.1 or higher, an explicit IV is added
9862 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009863#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009864 if( mbedtls_ssl_ver_geq(
9865 mbedtls_ssl_get_minor_ver( ssl ),
9866 MBEDTLS_SSL_MINOR_VERSION_2 ) )
9867 {
Hanno Becker3136ede2018-08-17 15:28:19 +01009868 transform_expansion += block_size;
Hanno Becker7bcf2b52019-07-26 09:02:40 +01009869 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01009870#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009871
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009872 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009873 }
9874#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009875
9876 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009879 }
9880
Hanno Beckera5a2b082019-05-15 14:03:01 +01009881#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009882 if( transform->out_cid_len != 0 )
9883 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009884#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009885
Hanno Becker43395762019-05-03 14:46:38 +01009886 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009887}
9888
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009889#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9890size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9891{
9892 size_t max_len;
9893
9894 /*
9895 * Assume mfl_code is correct since it was checked when set
9896 */
Angus Grattond8213d02016-05-25 20:56:48 +10009897 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009898
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009899 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009900 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009901 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009902 {
Angus Grattond8213d02016-05-25 20:56:48 +10009903 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009904 }
9905
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009906 /* During a handshake, use the value being negotiated */
9907 if( ssl->session_negotiate != NULL &&
9908 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9909 {
9910 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9911 }
9912
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009913 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009914}
9915#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9916
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009917#if defined(MBEDTLS_SSL_PROTO_DTLS)
9918static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9919{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009920 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009921 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009922 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9923 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
Arto Kinnunen4f4849a2019-09-09 10:21:18 +03009924 return( 0 );
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009925
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009926 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9927 return( ssl->mtu );
9928
9929 if( ssl->mtu == 0 )
9930 return( ssl->handshake->mtu );
9931
9932 return( ssl->mtu < ssl->handshake->mtu ?
9933 ssl->mtu : ssl->handshake->mtu );
9934}
9935#endif /* MBEDTLS_SSL_PROTO_DTLS */
9936
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009937int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9938{
9939 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9940
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009941#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9942 !defined(MBEDTLS_SSL_PROTO_DTLS)
9943 (void) ssl;
9944#endif
9945
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009946#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9947 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9948
9949 if( max_len > mfl )
9950 max_len = mfl;
9951#endif
9952
9953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009954 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009955 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009956 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009957 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9958 const size_t overhead = (size_t) ret;
9959
9960 if( ret < 0 )
9961 return( ret );
9962
9963 if( mtu <= overhead )
9964 {
9965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9966 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9967 }
9968
9969 if( max_len > mtu - overhead )
9970 max_len = mtu - overhead;
9971 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009972#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009973
Hanno Becker0defedb2018-08-10 12:35:02 +01009974#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9975 !defined(MBEDTLS_SSL_PROTO_DTLS)
9976 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009977#endif
9978
9979 return( (int) max_len );
9980}
9981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982#if defined(MBEDTLS_X509_CRT_PARSE_C)
9983const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009984{
9985 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009986 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009987
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009988#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009989 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009990#else
9991 return( NULL );
9992#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009993}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009997int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9998 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009999{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010000 if( ssl == NULL ||
10001 dst == NULL ||
10002 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +010010003 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010006 }
10007
Hanno Becker58fccf22019-02-06 14:30:46 +000010008 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010010#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +020010011
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +020010012const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
10013{
10014 if( ssl == NULL )
10015 return( NULL );
10016
10017 return( ssl->session );
10018}
10019
Paul Bakker5121ce52009-01-03 21:22:43 +000010020/*
Hanno Beckerb5352f02019-05-16 12:39:07 +010010021 * Define ticket header determining Mbed TLS version
10022 * and structure of the ticket.
10023 */
10024
Hanno Becker41527622019-05-16 12:50:45 +010010025/*
Hanno Becker26829e92019-05-28 14:30:45 +010010026 * Define bitflag determining compile-time settings influencing
10027 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +010010028 */
10029
Hanno Becker26829e92019-05-28 14:30:45 +010010030#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010031#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +010010032#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010033#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +010010034#endif /* MBEDTLS_HAVE_TIME */
10035
10036#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010037#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +010010038#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010039#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +010010040#endif /* MBEDTLS_X509_CRT_PARSE_C */
10041
10042#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010043#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +010010044#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010045#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +010010046#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
10047
10048#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010049#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +010010050#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010051#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +010010052#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
10053
10054#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010055#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +010010056#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010057#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +010010058#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
10059
10060#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010061#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +010010062#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010063#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +010010064#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
10065
Hanno Becker41527622019-05-16 12:50:45 +010010066#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10067#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
10068#else
10069#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
10070#endif /* MBEDTLS_SSL_SESSION_TICKETS */
10071
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010072#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10073#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
10074#else
10075#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
10076#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10077
Hanno Becker88440552019-07-03 14:16:13 +010010078#if defined(MBEDTLS_ZLIB_SUPPORT)
10079#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
10080#else
10081#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
10082#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10083
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010084#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
10085#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
10086#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
10087#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
10088#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
10089#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
10090#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010091#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +010010092#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010093
Hanno Becker26829e92019-05-28 14:30:45 +010010094#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +010010095 ( (uint16_t) ( \
10096 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
10097 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
10098 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
10099 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
10100 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
10101 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010102 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +010010103 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010104 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +010010105
Hanno Becker557fe9f2019-05-16 12:41:07 +010010106static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +010010107 MBEDTLS_VERSION_MAJOR,
10108 MBEDTLS_VERSION_MINOR,
10109 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +010010110 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
10111 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +010010112};
Hanno Beckerb5352f02019-05-16 12:39:07 +010010113
10114/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010115 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010116 * (in the presentation language of TLS, RFC 8446 section 3)
10117 *
Hanno Becker26829e92019-05-28 14:30:45 +010010118 * opaque mbedtls_version[3]; // major, minor, patch
10119 * opaque session_format[2]; // version-specific 16-bit field determining
10120 * // the format of the remaining
10121 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +010010122 *
10123 * Note: When updating the format, remember to keep
10124 * these version+format bytes.
10125 *
Hanno Becker7bf77102019-06-04 09:43:16 +010010126 * // In this version, `session_format` determines
10127 * // the setting of those compile-time
10128 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +010010129 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010130 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +010010131 * uint8 ciphersuite[2]; // defined by the standard
10132 * uint8 compression; // 0 or 1
10133 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010134 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +010010135 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010136 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +010010137 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
10138 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
10139 * case disabled: uint8_t peer_cert_digest_type;
10140 * opaque peer_cert_digest<0..2^8-1>;
10141 * }
Hanno Becker26829e92019-05-28 14:30:45 +010010142 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010143 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +010010144 * uint8 mfl_code; // up to 255 according to standard
10145 * uint8 trunc_hmac; // 0 or 1
10146 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010147 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010148 * The order is the same as in the definition of the structure, except
10149 * verify_result is put before peer_cert so that all mandatory fields come
10150 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010151 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010152static int ssl_session_save( const mbedtls_ssl_session *session,
10153 unsigned char omit_header,
10154 unsigned char *buf,
10155 size_t buf_len,
10156 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010157{
10158 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010159 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010160#if defined(MBEDTLS_HAVE_TIME)
10161 uint64_t start;
10162#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010163#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010164#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010165 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010166#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +000010167#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010168
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010169 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010170 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010171 /*
10172 * Add version identifier
10173 */
10174
10175 used += sizeof( ssl_serialized_session_header );
10176
10177 if( used <= buf_len )
10178 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010179 mbedtls_platform_memcpy( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010180 sizeof( ssl_serialized_session_header ) );
10181 p += sizeof( ssl_serialized_session_header );
10182 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010183 }
10184
10185 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010186 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010187 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010188#if defined(MBEDTLS_HAVE_TIME)
10189 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010190
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010191 if( used <= buf_len )
10192 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010193 start = (uint64_t) session->start;
10194
10195 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
10196 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
10197 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
10198 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
10199 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
10200 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
10201 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
10202 *p++ = (unsigned char)( ( start ) & 0xFF );
10203 }
10204#endif /* MBEDTLS_HAVE_TIME */
10205
10206 /*
10207 * Basic mandatory fields
10208 */
Hanno Becker88440552019-07-03 14:16:13 +010010209 {
10210 size_t const ciphersuite_len = 2;
10211#if defined(MBEDTLS_ZLIB_SUPPORT)
10212 size_t const compression_len = 1;
10213#else
10214 size_t const compression_len = 0;
10215#endif
10216 size_t const id_len_len = 1;
10217 size_t const id_len = 32;
10218 size_t const master_len = 48;
10219 size_t const verif_result_len = 4;
10220
10221 size_t const basic_len =
10222 ciphersuite_len +
10223 compression_len +
10224 id_len_len +
10225 id_len +
10226 master_len +
10227 verif_result_len;
10228
10229 used += basic_len;
10230 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010231
10232 if( used <= buf_len )
10233 {
Hanno Beckere02758c2019-06-26 15:31:31 +010010234 const int ciphersuite =
10235 mbedtls_ssl_session_get_ciphersuite( session );
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010236 p = mbedtls_platform_put_uint16_be( p, ciphersuite );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010237
Hanno Becker88440552019-07-03 14:16:13 +010010238#if defined(MBEDTLS_ZLIB_SUPPORT)
10239 *p++ = (unsigned char)(
10240 mbedtls_ssl_session_get_compression( session ) );
10241#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010242
10243 *p++ = (unsigned char)( session->id_len & 0xFF );
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010244 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10245 memcpy( p, session->id, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010246 p += 32;
10247
Teppo Järvelin91d79382019-10-02 09:09:31 +030010248 mbedtls_platform_memcpy( p, session->master, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010249 p += 48;
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010250 p = mbedtls_platform_put_uint32_be( p, session->verify_result );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010251 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010252
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010253 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010254 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010255 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010256#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010257#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010258 if( session->peer_cert == NULL )
10259 cert_len = 0;
10260 else
10261 cert_len = session->peer_cert->raw.len;
10262
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010263 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010264
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010265 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010266 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010267 p = mbedtls_platform_put_uint24_be( p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010268
10269 if( session->peer_cert != NULL )
10270 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010271 mbedtls_platform_memcpy( p, session->peer_cert->raw.p, cert_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010272 p += cert_len;
10273 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010274 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010275
Hanno Becker5882dd02019-06-06 16:25:57 +010010276#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010277 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010278 if( session->peer_cert_digest != NULL )
10279 {
10280 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
10281 if( used <= buf_len )
10282 {
10283 *p++ = (unsigned char) session->peer_cert_digest_type;
10284 *p++ = (unsigned char) session->peer_cert_digest_len;
Teppo Järvelin91d79382019-10-02 09:09:31 +030010285 mbedtls_platform_memcpy( p, session->peer_cert_digest,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010286 session->peer_cert_digest_len );
10287 p += session->peer_cert_digest_len;
10288 }
10289 }
10290 else
10291 {
10292 used += 2;
10293 if( used <= buf_len )
10294 {
10295 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10296 *p++ = 0;
10297 }
10298 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010299#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010300#endif /* MBEDTLS_X509_CRT_PARSE_C */
10301
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010302 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010303 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010304 */
10305#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010306 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010307
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010308 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010309 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010310 p = mbedtls_platform_put_uint24_be( p, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010311
10312 if( session->ticket != NULL )
10313 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030010314 mbedtls_platform_memcpy( p, session->ticket, session->ticket_len );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010315 p += session->ticket_len;
10316 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010317
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010318 p = mbedtls_platform_put_uint32_be( p, session->ticket_lifetime );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010319 }
10320#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10321
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010322 /*
10323 * Misc extension-related info
10324 */
10325#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10326 used += 1;
10327
10328 if( used <= buf_len )
10329 *p++ = session->mfl_code;
10330#endif
10331
10332#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10333 used += 1;
10334
10335 if( used <= buf_len )
10336 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10337#endif
10338
10339#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10340 used += 1;
10341
10342 if( used <= buf_len )
10343 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10344#endif
10345
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010346 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +020010347 *olen = used;
10348
10349 if( used > buf_len )
10350 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010351
10352 return( 0 );
10353}
10354
10355/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010356 * Public wrapper for ssl_session_save()
10357 */
10358int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
10359 unsigned char *buf,
10360 size_t buf_len,
10361 size_t *olen )
10362{
10363 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
10364}
10365
10366/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010367 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010368 *
10369 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010370 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010371 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010372static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010373 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010374 const unsigned char *buf,
10375 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010376{
10377 const unsigned char *p = buf;
10378 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +010010379 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010380#if defined(MBEDTLS_HAVE_TIME)
10381 uint64_t start;
10382#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010383#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010384#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010385 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +000010386#endif
10387#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010388
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010389 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +010010390 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010391 /*
10392 * Check version identifier
10393 */
10394
10395 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10397
Teppo Järvelin0efac532019-10-04 13:21:08 +030010398 // use regular memcmp as session header is public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010399 if( memcmp( p, ssl_serialized_session_header,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010400 sizeof( ssl_serialized_session_header ) ) != 0 )
10401 {
10402 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10403 }
10404 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +010010405 }
Hanno Beckerb5352f02019-05-16 12:39:07 +010010406
10407 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010408 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010409 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010410#if defined(MBEDTLS_HAVE_TIME)
10411 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10413
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010414 start = ( (uint64_t) p[0] << 56 ) |
10415 ( (uint64_t) p[1] << 48 ) |
10416 ( (uint64_t) p[2] << 40 ) |
10417 ( (uint64_t) p[3] << 32 ) |
10418 ( (uint64_t) p[4] << 24 ) |
10419 ( (uint64_t) p[5] << 16 ) |
10420 ( (uint64_t) p[6] << 8 ) |
10421 ( (uint64_t) p[7] );
10422 p += 8;
10423
10424 session->start = (time_t) start;
10425#endif /* MBEDTLS_HAVE_TIME */
10426
10427 /*
10428 * Basic mandatory fields
10429 */
Hanno Becker88440552019-07-03 14:16:13 +010010430 {
10431 size_t const ciphersuite_len = 2;
10432#if defined(MBEDTLS_ZLIB_SUPPORT)
10433 size_t const compression_len = 1;
10434#else
10435 size_t const compression_len = 0;
10436#endif
10437 size_t const id_len_len = 1;
10438 size_t const id_len = 32;
10439 size_t const master_len = 48;
10440 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +010010441
Hanno Becker88440552019-07-03 14:16:13 +010010442 size_t const basic_len =
10443 ciphersuite_len +
10444 compression_len +
10445 id_len_len +
10446 id_len +
10447 master_len +
10448 verif_result_len;
10449
10450 if( basic_len > (size_t)( end - p ) )
10451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10452 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010453
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010454 ciphersuite = (int)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010455 p += 2;
10456
Hanno Becker73f4cb12019-06-27 13:51:07 +010010457#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +010010458 session->ciphersuite = ciphersuite;
10459#else
10460 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010461 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010462 {
10463 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10464 }
10465#endif
10466
Hanno Becker88440552019-07-03 14:16:13 +010010467#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010468 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010469#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010470
10471 session->id_len = *p++;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030010472 /* Not using more secure mbedtls_platform_memcpy as session id is public */
10473 memcpy( session->id, p, 32 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010474 p += 32;
10475
Teppo Järvelin91d79382019-10-02 09:09:31 +030010476 mbedtls_platform_memcpy( session->master, p, 48 );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010477 p += 48;
10478
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010479 session->verify_result = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010480 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010481
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010482 /* Immediately clear invalid pointer values that have been read, in case
10483 * we exit early before we replaced them with valid ones. */
10484#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010485#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010486 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010487#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010488 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010489#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010490#endif
10491#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10492 session->ticket = NULL;
10493#endif
10494
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010495 /*
10496 * Peer certificate
10497 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010498#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010499#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010500 if( 3 > (size_t)( end - p ) )
10501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10502
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010503 cert_len = mbedtls_platform_get_uint24_be( &p[0] );
10504
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010505 p += 3;
10506
10507 if( cert_len == 0 )
10508 {
10509 session->peer_cert = NULL;
10510 }
10511 else
10512 {
10513 int ret;
10514
10515 if( cert_len > (size_t)( end - p ) )
10516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10517
10518 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10519
10520 if( session->peer_cert == NULL )
10521 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10522
10523 mbedtls_x509_crt_init( session->peer_cert );
10524
10525 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10526 p, cert_len ) ) != 0 )
10527 {
10528 mbedtls_x509_crt_free( session->peer_cert );
10529 mbedtls_free( session->peer_cert );
10530 session->peer_cert = NULL;
10531 return( ret );
10532 }
10533
10534 p += cert_len;
10535 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010536#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010537 /* Deserialize CRT digest from the end of the ticket. */
10538 if( 2 > (size_t)( end - p ) )
10539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10540
10541 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10542 session->peer_cert_digest_len = (size_t) *p++;
10543
10544 if( session->peer_cert_digest_len != 0 )
10545 {
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010546 mbedtls_md_handle_t md_info =
Hanno Becker2326d202019-06-06 14:54:55 +010010547 mbedtls_md_info_from_type( session->peer_cert_digest_type );
Hanno Beckera5cedbc2019-07-17 11:21:02 +010010548 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Hanno Becker2326d202019-06-06 14:54:55 +010010549 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10550 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10552
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010553 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10555
10556 session->peer_cert_digest =
10557 mbedtls_calloc( 1, session->peer_cert_digest_len );
10558 if( session->peer_cert_digest == NULL )
10559 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10560
Teppo Järvelin91d79382019-10-02 09:09:31 +030010561 mbedtls_platform_memcpy( session->peer_cert_digest, p,
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010562 session->peer_cert_digest_len );
10563 p += session->peer_cert_digest_len;
10564 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010565#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010566#endif /* MBEDTLS_X509_CRT_PARSE_C */
10567
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010568 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010569 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010570 */
10571#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10572 if( 3 > (size_t)( end - p ) )
10573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10574
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030010575 session->ticket_len = mbedtls_platform_get_uint24_be( &p[0] );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010576 p += 3;
10577
10578 if( session->ticket_len != 0 )
10579 {
10580 if( session->ticket_len > (size_t)( end - p ) )
10581 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10582
10583 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10584 if( session->ticket == NULL )
10585 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10586
Teppo Järvelin91d79382019-10-02 09:09:31 +030010587 mbedtls_platform_memcpy( session->ticket, p, session->ticket_len );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010588 p += session->ticket_len;
10589 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010590
10591 if( 4 > (size_t)( end - p ) )
10592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10593
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030010594 session->ticket_lifetime = (uint32_t)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010595 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010596#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10597
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010598 /*
10599 * Misc extension-related info
10600 */
10601#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10602 if( 1 > (size_t)( end - p ) )
10603 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10604
10605 session->mfl_code = *p++;
10606#endif
10607
10608#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10609 if( 1 > (size_t)( end - p ) )
10610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10611
10612 session->trunc_hmac = *p++;
10613#endif
10614
10615#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10616 if( 1 > (size_t)( end - p ) )
10617 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10618
10619 session->encrypt_then_mac = *p++;
10620#endif
10621
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010622 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010623 if( p != end )
10624 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10625
10626 return( 0 );
10627}
10628
10629/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010630 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010631 */
10632int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10633 const unsigned char *buf,
10634 size_t len )
10635{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010636 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010637
10638 if( ret != 0 )
10639 mbedtls_ssl_session_free( session );
10640
10641 return( ret );
10642}
10643
10644/*
Paul Bakker1961b702013-01-25 14:49:24 +010010645 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010648{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010650
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010651 if( ssl == NULL || ssl->conf == NULL )
10652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010655 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010657#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010658#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010659 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010661#endif
10662
Hanno Beckerb82350b2019-07-26 07:24:05 +010010663 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010664 return( ret );
10665}
10666
10667/*
10668 * Perform the SSL handshake
10669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010671{
10672 int ret = 0;
10673
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010674 if( ssl == NULL || ssl->conf == NULL )
10675 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010682
10683 if( ret != 0 )
10684 break;
10685 }
10686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010688
10689 return( ret );
10690}
10691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692#if defined(MBEDTLS_SSL_RENEGOTIATION)
10693#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010694/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010695 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010698{
10699 int ret;
10700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010702
10703 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10705 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010706
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010707 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010708 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010709 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010710 return( ret );
10711 }
10712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010714
10715 return( 0 );
10716}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010717#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010718
10719/*
10720 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010721 * - any side: calling mbedtls_ssl_renegotiate(),
10722 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10723 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010724 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010725 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010726 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010729{
10730 int ret;
10731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010733
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010734 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10735 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010736
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010737 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10738 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010740 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010741 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010742 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010743 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10744 MBEDTLS_SSL_IS_SERVER )
10745 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010746 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010747 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010748 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010749 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010750 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010751 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010752 }
10753#endif
10754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10756 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010761 return( ret );
10762 }
10763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010765
10766 return( 0 );
10767}
10768
10769/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010770 * Renegotiate current connection on client,
10771 * or request renegotiation on server
10772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010776
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010777 if( ssl == NULL || ssl->conf == NULL )
10778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010781 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010782 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010784 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010788
10789 /* Did we already try/start sending HelloRequest? */
10790 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010792
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010793 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010797#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010798 /*
10799 * On client, either start the renegotiation process or,
10800 * if already in progress, continue the handshake
10801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010802 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010804 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010806
10807 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010810 return( ret );
10811 }
10812 }
10813 else
10814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010815 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010818 return( ret );
10819 }
10820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010822
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010823 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010824}
10825
10826/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010827 * Check record counters and renegotiate if they're above the limit.
10828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010829static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010830{
Andres AG2196c7f2016-12-15 17:01:16 +000010831 size_t ep_len = ssl_ep_len( ssl );
10832 int in_ctr_cmp;
10833 int out_ctr_cmp;
10834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010835 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10836 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010837 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010838 {
10839 return( 0 );
10840 }
10841
Teppo Järvelin0efac532019-10-04 13:21:08 +030010842 // use regular memcmp as counters are public data
Teppo Järvelin650343c2019-10-03 15:36:59 +030010843 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010844 ssl->conf->renego_period + ep_len, 8 - ep_len );
Teppo Järvelin650343c2019-10-03 15:36:59 +030010845 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010846 ssl->conf->renego_period + ep_len, 8 - ep_len );
10847
10848 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010849 {
10850 return( 0 );
10851 }
10852
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010857
10858/*
10859 * Receive application data decrypted from the SSL layer
10860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010861int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010862{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010863 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010864 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010865
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010866 if( ssl == NULL || ssl->conf == NULL )
10867 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010872 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010875 return( ret );
10876
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010877 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010878 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010879 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010880 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010881 return( ret );
10882 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010883 }
10884#endif
10885
Hanno Becker4a810fb2017-05-24 16:27:30 +010010886 /*
10887 * Check if renegotiation is necessary and/or handshake is
10888 * in process. If yes, perform/continue, and fall through
10889 * if an unexpected packet is received while the client
10890 * is waiting for the ServerHello.
10891 *
10892 * (There is no equivalent to the last condition on
10893 * the server-side as it is not treated as within
10894 * a handshake while waiting for the ClientHello
10895 * after a renegotiation request.)
10896 */
10897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010899 ret = ssl_check_ctr_renegotiate( ssl );
10900 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10901 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010903 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010904 return( ret );
10905 }
10906#endif
10907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010911 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10912 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010915 return( ret );
10916 }
10917 }
10918
Hanno Beckere41158b2017-10-23 13:30:32 +010010919 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010920 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010921 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010922 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010923 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10924 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010925 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010926 ssl_set_timer( ssl,
10927 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010928 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010929
Hanno Becker327c93b2018-08-15 13:56:18 +010010930 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010931 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010932 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10933 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010934
Hanno Becker4a810fb2017-05-24 16:27:30 +010010935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10936 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010937 }
10938
10939 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010940 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010941 {
10942 /*
10943 * OpenSSL sends empty messages to randomize the IV
10944 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010945 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010948 return( 0 );
10949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010951 return( ret );
10952 }
10953 }
10954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010958
Hanno Becker4a810fb2017-05-24 16:27:30 +010010959 /*
10960 * - For client-side, expect SERVER_HELLO_REQUEST.
10961 * - For server-side, expect CLIENT_HELLO.
10962 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10963 */
10964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010965#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010966 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10967 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010969 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010972
10973 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010975 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010976 {
10977 continue;
10978 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010979 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010980#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010981#if defined(MBEDTLS_SSL_PROTO_TLS)
10982 {
10983 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10984 }
10985#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010986 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010987#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010988
Hanno Becker4a810fb2017-05-24 16:27:30 +010010989#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010990 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10991 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010995
10996 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010997#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010998 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010999 {
11000 continue;
11001 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011002 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020011003#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020011004#if defined(MBEDTLS_SSL_PROTO_TLS)
11005 {
11006 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
11007 }
11008#endif
Paul Bakker48916f92012-09-16 19:57:18 +000011009 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010011010#endif /* MBEDTLS_SSL_SRV_C */
11011
Hanno Becker21df7f92017-10-17 11:03:26 +010011012#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010011013 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011014 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
11015 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010011016 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011017 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
11018 {
11019 /*
11020 * Accept renegotiation request
11021 */
Paul Bakker48916f92012-09-16 19:57:18 +000011022
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011023 /* DTLS clients need to know renego is server-initiated */
11024#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011025 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010011026 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11027 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010011028 {
11029 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
11030 }
11031#endif
11032 ret = ssl_start_renegotiation( ssl );
11033 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
11034 ret != 0 )
11035 {
11036 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
11037 return( ret );
11038 }
11039 }
11040 else
Hanno Becker21df7f92017-10-17 11:03:26 +010011041#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000011042 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010011043 /*
11044 * Refuse renegotiation
11045 */
11046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011047 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010011050 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000011051 {
Gilles Peskine92e44262017-05-10 17:27:49 +020011052 /* SSLv3 does not have a "no_renegotiation" warning, so
11053 we send a fatal alert and abort the connection. */
11054 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11055 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
11056 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011057 }
11058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011059#endif /* MBEDTLS_SSL_PROTO_SSL3 */
11060#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11061 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011062 if( mbedtls_ssl_ver_geq(
11063 mbedtls_ssl_get_minor_ver( ssl ),
11064 MBEDTLS_SSL_MINOR_VERSION_1 ) )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000011065 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010011066 ret = mbedtls_ssl_send_alert_message( ssl,
11067 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11068 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
11069 if( ret != 0 )
11070 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000011071 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020011072 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011073#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
11074 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020011075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
11077 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020011078 }
Paul Bakker48916f92012-09-16 19:57:18 +000011079 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011080
Hanno Becker90333da2017-10-10 11:27:13 +010011081 /* At this point, we don't know whether the renegotiation has been
11082 * completed or not. The cases to consider are the following:
11083 * 1) The renegotiation is complete. In this case, no new record
11084 * has been read yet.
11085 * 2) The renegotiation is incomplete because the client received
11086 * an application data record while awaiting the ServerHello.
11087 * 3) The renegotiation is incomplete because the client received
11088 * a non-handshake, non-application data message while awaiting
11089 * the ServerHello.
11090 * In each of these case, looping will be the proper action:
11091 * - For 1), the next iteration will read a new record and check
11092 * if it's application data.
11093 * - For 2), the loop condition isn't satisfied as application data
11094 * is present, hence continue is the same as break
11095 * - For 3), the loop condition is satisfied and read_record
11096 * will re-deliver the message that was held back by the client
11097 * when expecting the ServerHello.
11098 */
11099 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000011100 }
Hanno Becker21df7f92017-10-17 11:03:26 +010011101#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011102 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011103 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011104 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011105 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011106 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011109 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011110 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011111 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020011112 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010011113 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011114#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011116 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
11117 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010011120 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020011121 }
11122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011123 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000011124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
11126 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000011127 }
11128
11129 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020011130
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011131 /* We're going to return something now, cancel timer,
11132 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011133 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020011134 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011135
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020011136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011137 /* If we requested renego but received AppData, resend HelloRequest.
11138 * Do it now, after setting in_offt, to avoid taking this branch
11139 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011140#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010011141 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
11142 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011144 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020011145 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011147 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020011148 return( ret );
11149 }
11150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011151#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010011152#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000011153 }
11154
11155 n = ( len < ssl->in_msglen )
11156 ? len : ssl->in_msglen;
11157
Teppo Järvelin91d79382019-10-02 09:09:31 +030011158 mbedtls_platform_memcpy( buf, ssl->in_offt, n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011159 ssl->in_msglen -= n;
11160
Teppo Järvelincafb6c92020-01-08 09:19:07 +020011161 // clear incoming data after it's copied to buffer
11162 mbedtls_platform_memset(ssl->in_offt, 0, n);
11163
Paul Bakker5121ce52009-01-03 21:22:43 +000011164 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010011165 {
11166 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000011167 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010011168 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011169 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011170 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010011171 {
Paul Bakker5121ce52009-01-03 21:22:43 +000011172 /* more data available */
11173 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010011174 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011177
Paul Bakker23986e52011-04-24 08:57:21 +000011178 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000011179}
11180
11181/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011182 * Send application data to be encrypted by the SSL layer, taking care of max
11183 * fragment length and buffer size.
11184 *
11185 * According to RFC 5246 Section 6.2.1:
11186 *
11187 * Zero-length fragments of Application data MAY be sent as they are
11188 * potentially useful as a traffic analysis countermeasure.
11189 *
11190 * Therefore, it is possible that the input message length is 0 and the
11191 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000011192 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011193static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011194 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000011195{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020011196 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
11197 const size_t max_len = (size_t) ret;
11198
11199 if( ret < 0 )
11200 {
11201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
11202 return( ret );
11203 }
11204
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011205 if( len > max_len )
11206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011208 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011211 "maximum fragment length: %d > %d",
11212 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011214 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011215 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011216#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011217#if defined(MBEDTLS_SSL_PROTO_TLS)
11218 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011219 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011220 }
11221#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011222 }
Paul Bakker887bd502011-06-08 13:10:54 +000011223
Paul Bakker5121ce52009-01-03 21:22:43 +000011224 if( ssl->out_left != 0 )
11225 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011226 /*
11227 * The user has previously tried to send the data and
11228 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
11229 * written. In this case, we expect the high-level write function
11230 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
11231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011232 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011235 return( ret );
11236 }
11237 }
Paul Bakker887bd502011-06-08 13:10:54 +000011238 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000011239 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010011240 /*
11241 * The user is trying to send a message the first time, so we need to
11242 * copy the data into the internal buffers and setup the data structure
11243 * to keep track of partial writes
11244 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011245 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011246 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Teppo Järvelin91d79382019-10-02 09:09:31 +030011247 mbedtls_platform_memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000011248
Hanno Becker67bc7c32018-08-06 11:33:50 +010011249 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000011250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000011252 return( ret );
11253 }
Paul Bakker5121ce52009-01-03 21:22:43 +000011254 }
11255
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020011256 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000011257}
11258
11259/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011260 * Write application data, doing 1/n-1 splitting if necessary.
11261 *
11262 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011263 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010011264 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011266#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011267static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011268 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011269{
11270 int ret;
11271
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011272 if( ssl->conf->cbc_record_splitting ==
11273 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010011274 len <= 1 ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011275 mbedtls_ssl_ver_gt(
11276 mbedtls_ssl_get_minor_ver( ssl ),
11277 MBEDTLS_SSL_MINOR_VERSION_1 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011278 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
11279 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011280 {
11281 return( ssl_write_real( ssl, buf, len ) );
11282 }
11283
11284 if( ssl->split_done == 0 )
11285 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011286 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011287 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011288 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011289 }
11290
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010011291 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
11292 return( ret );
11293 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011294
11295 return( ret + 1 );
11296}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011297#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010011298
11299/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011300 * Write application data (public-facing wrapper)
11301 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011302int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011303{
11304 int ret;
11305
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011307
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011308 if( ssl == NULL || ssl->conf == NULL )
11309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11310
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011311#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011312 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
11313 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011314 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011315 return( ret );
11316 }
11317#endif
11318
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011319 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011320 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011321 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011322 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020011323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011324 return( ret );
11325 }
11326 }
11327
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011328#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011329 ret = ssl_write_split( ssl, buf, len );
11330#else
11331 ret = ssl_write_real( ssl, buf, len );
11332#endif
11333
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020011334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020011335
11336 return( ret );
11337}
11338
11339/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011340 * Notify the peer that the connection is being closed
11341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011342int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011343{
11344 int ret;
11345
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020011346 if( ssl == NULL || ssl->conf == NULL )
11347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011350
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011351 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011352 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011354 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000011355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011356 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
11357 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
11358 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000011359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000011361 return( ret );
11362 }
11363 }
11364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011366
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011367 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011368}
11369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011370void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011371{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011372 if( transform == NULL )
11373 return;
11374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011375#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011376 deflateEnd( &transform->ctx_deflate );
11377 inflateEnd( &transform->ctx_inflate );
11378#endif
11379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011380 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11381 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011382
Hanno Becker92231322018-01-03 15:32:51 +000011383#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011384 mbedtls_md_free( &transform->md_ctx_enc );
11385 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000011386#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011387
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011388 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011389}
11390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011391#if defined(MBEDTLS_X509_CRT_PARSE_C)
11392static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011394 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011395
11396 while( cur != NULL )
11397 {
11398 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011399 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011400 cur = next;
11401 }
11402}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011403#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011404
Hanno Becker0271f962018-08-16 13:23:47 +010011405#if defined(MBEDTLS_SSL_PROTO_DTLS)
11406
11407static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11408{
11409 unsigned offset;
11410 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11411
11412 if( hs == NULL )
11413 return;
11414
Hanno Becker283f5ef2018-08-24 09:34:47 +010011415 ssl_free_buffered_record( ssl );
11416
Hanno Becker0271f962018-08-16 13:23:47 +010011417 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011418 ssl_buffering_free_slot( ssl, offset );
11419}
11420
11421static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11422 uint8_t slot )
11423{
11424 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11425 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011426
11427 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11428 return;
11429
Hanno Beckere605b192018-08-21 15:59:07 +010011430 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011431 {
Hanno Beckere605b192018-08-21 15:59:07 +010011432 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011433 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011434 mbedtls_free( hs_buf->data );
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020011435 mbedtls_platform_memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011436 }
11437}
11438
11439#endif /* MBEDTLS_SSL_PROTO_DTLS */
11440
Gilles Peskine9b562d52018-04-25 20:32:43 +020011441void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011442{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011443 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11444
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011445 if( handshake == NULL )
11446 return;
11447
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011448#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11449 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11450 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011451 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011452 handshake->async_in_progress = 0;
11453 }
11454#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11455
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011456#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11457 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11458 mbedtls_md5_free( &handshake->fin_md5 );
11459 mbedtls_sha1_free( &handshake->fin_sha1 );
11460#endif
11461#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11462#if defined(MBEDTLS_SHA256_C)
11463 mbedtls_sha256_free( &handshake->fin_sha256 );
11464#endif
11465#if defined(MBEDTLS_SHA512_C)
11466 mbedtls_sha512_free( &handshake->fin_sha512 );
11467#endif
11468#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011470#if defined(MBEDTLS_DHM_C)
11471 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011472#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011473#if defined(MBEDTLS_ECDH_C)
11474 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011475#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011476#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011477 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011478#if defined(MBEDTLS_SSL_CLI_C)
11479 mbedtls_free( handshake->ecjpake_cache );
11480 handshake->ecjpake_cache = NULL;
11481 handshake->ecjpake_cache_len = 0;
11482#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011483#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011484
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011485#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11486 if( handshake->psk != NULL )
11487 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011488 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011489 mbedtls_free( handshake->psk );
11490 }
11491#endif
11492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011493#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11494 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011495 /*
11496 * Free only the linked list wrapper, not the keys themselves
11497 * since the belong to the SNI callback
11498 */
11499 if( handshake->sni_key_cert != NULL )
11500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011501 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011502
11503 while( cur != NULL )
11504 {
11505 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011506 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011507 cur = next;
11508 }
11509 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011510#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011511
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011512#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011513 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011514 if( handshake->ecrs_peer_cert != NULL )
11515 {
11516 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11517 mbedtls_free( handshake->ecrs_peer_cert );
11518 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011519#endif
11520
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011521#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11522 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11523 mbedtls_pk_free( &handshake->peer_pubkey );
11524#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011526#if defined(MBEDTLS_SSL_PROTO_DTLS)
11527 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011528 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011529 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011530#endif
11531
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011532 mbedtls_platform_zeroize( handshake,
11533 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011534}
11535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011536void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011537{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011538 if( session == NULL )
11539 return;
11540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011541#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011542 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011543#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011544
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011545#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011546 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011547#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011548
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011549 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011550}
11551
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011552#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011553
11554#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11555#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11556#else
11557#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11558#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11559
11560#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11561#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11562#else
11563#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11564#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11565
11566#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11567#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11568#else
11569#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11570#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11571
11572#if defined(MBEDTLS_SSL_ALPN)
11573#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11574#else
11575#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11576#endif /* MBEDTLS_SSL_ALPN */
11577
11578#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11579#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11580#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11581#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11582
11583#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11584 ( (uint32_t) ( \
11585 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11586 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11587 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11588 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11589 0u ) )
11590
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011591static unsigned char ssl_serialized_context_header[] = {
11592 MBEDTLS_VERSION_MAJOR,
11593 MBEDTLS_VERSION_MINOR,
11594 MBEDTLS_VERSION_PATCH,
11595 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11596 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011597 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11598 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11599 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011600};
11601
Paul Bakker5121ce52009-01-03 21:22:43 +000011602/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011603 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011604 *
11605 * The format of the serialized data is:
11606 * (in the presentation language of TLS, RFC 8446 section 3)
11607 *
11608 * // header
11609 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011610 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011611 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011612 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011613 * Note: When updating the format, remember to keep these
11614 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011615 *
11616 * // session sub-structure
11617 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11618 * // transform sub-structure
11619 * uint8 random[64]; // ServerHello.random+ClientHello.random
11620 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11621 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11622 * // fields from ssl_context
11623 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11624 * uint64 in_window_top; // DTLS: last validated record seq_num
11625 * uint64 in_window; // DTLS: bitmask for replay protection
11626 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11627 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11628 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11629 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11630 *
11631 * Note that many fields of the ssl_context or sub-structures are not
11632 * serialized, as they fall in one of the following categories:
11633 *
11634 * 1. forced value (eg in_left must be 0)
11635 * 2. pointer to dynamically-allocated memory (eg session, transform)
11636 * 3. value can be re-derived from other data (eg session keys from MS)
11637 * 4. value was temporary (eg content of input buffer)
11638 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011639 */
11640int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11641 unsigned char *buf,
11642 size_t buf_len,
11643 size_t *olen )
11644{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011645 unsigned char *p = buf;
11646 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011647 size_t session_len;
11648 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011649
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011650 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011651 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11652 * this function's documentation.
11653 *
11654 * These are due to assumptions/limitations in the implementation. Some of
11655 * them are likely to stay (no handshake in progress) some might go away
11656 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011657 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011658 /* The initial handshake must be over */
11659 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011660 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011661 if( ssl->handshake != NULL )
11662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11663 /* Double-check that sub-structures are indeed ready */
11664 if( ssl->transform == NULL || ssl->session == NULL )
11665 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11666 /* There must be no pending incoming or outgoing data */
11667 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11669 if( ssl->out_left != 0 )
11670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11671 /* Protocol must be DLTS, not TLS */
11672 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11674 /* Version must be 1.2 */
11675 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11677 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11678 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11679 /* We must be using an AEAD ciphersuite */
11680 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11681 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11682 /* Renegotiation must not be enabled */
11683 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011685
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011686 /*
11687 * Version and format identifier
11688 */
11689 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011690
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011691 if( used <= buf_len )
11692 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011693 mbedtls_platform_memcpy( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011694 sizeof( ssl_serialized_context_header ) );
11695 p += sizeof( ssl_serialized_context_header );
11696 }
11697
11698 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011699 * Session (length + data)
11700 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011701 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011702 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11703 return( ret );
11704
11705 used += 4 + session_len;
11706 if( used <= buf_len )
11707 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011708 p = mbedtls_platform_put_uint32_be( p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011709
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011710 ret = ssl_session_save( ssl->session, 1,
11711 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011712 if( ret != 0 )
11713 return( ret );
11714
11715 p += session_len;
11716 }
11717
11718 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011719 * Transform
11720 */
11721 used += sizeof( ssl->transform->randbytes );
11722 if( used <= buf_len )
11723 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011724 mbedtls_platform_memcpy( p, ssl->transform->randbytes,
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011725 sizeof( ssl->transform->randbytes ) );
11726 p += sizeof( ssl->transform->randbytes );
11727 }
11728
11729#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11730 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11731 if( used <= buf_len )
11732 {
11733 *p++ = ssl->transform->in_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011734 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11735 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011736 p += ssl->transform->in_cid_len;
11737
11738 *p++ = ssl->transform->out_cid_len;
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011739 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11740 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011741 p += ssl->transform->out_cid_len;
11742 }
11743#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11744
11745 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011746 * Saved fields from top-level ssl_context structure
11747 */
11748#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11749 used += 4;
11750 if( used <= buf_len )
11751 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011752 p = mbedtls_platform_put_uint32_be( p, ssl->badmac_seen );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011753 }
11754#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11755
11756#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11757 used += 16;
11758 if( used <= buf_len )
11759 {
11760 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11761 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11762 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11763 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11764 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11765 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11766 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11767 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11768
11769 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11770 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11771 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11772 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11773 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11774 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11775 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11776 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11777 }
11778#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11779
11780#if defined(MBEDTLS_SSL_PROTO_DTLS)
11781 used += 1;
11782 if( used <= buf_len )
11783 {
11784 *p++ = ssl->disable_datagram_packing;
11785 }
11786#endif /* MBEDTLS_SSL_PROTO_DTLS */
11787
11788 used += 8;
11789 if( used <= buf_len )
11790 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011791 mbedtls_platform_memcpy( p, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011792 p += 8;
11793 }
11794
11795#if defined(MBEDTLS_SSL_PROTO_DTLS)
11796 used += 2;
11797 if( used <= buf_len )
11798 {
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011799 p = mbedtls_platform_put_uint16_be( p, ssl->mtu );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011800 }
11801#endif /* MBEDTLS_SSL_PROTO_DTLS */
11802
11803#if defined(MBEDTLS_SSL_ALPN)
11804 {
11805 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011806 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011807 : 0;
11808
11809 used += 1 + alpn_len;
11810 if( used <= buf_len )
11811 {
11812 *p++ = alpn_len;
11813
11814 if( ssl->alpn_chosen != NULL )
11815 {
Teppo Järvelin91d79382019-10-02 09:09:31 +030011816 mbedtls_platform_memcpy( p, ssl->alpn_chosen, alpn_len );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011817 p += alpn_len;
11818 }
11819 }
11820 }
11821#endif /* MBEDTLS_SSL_ALPN */
11822
11823 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011824 * Done
11825 */
11826 *olen = used;
11827
11828 if( used > buf_len )
11829 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011830
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011831 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11832
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011833 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011834}
11835
11836/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011837 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011838 *
11839 * This internal version is wrapped by a public function that cleans up in
11840 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011841 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011842static int ssl_context_load( mbedtls_ssl_context *ssl,
11843 const unsigned char *buf,
11844 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011845{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011846 const unsigned char *p = buf;
11847 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011848 size_t session_len;
11849 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011850
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011851 /*
11852 * The context should have been freshly setup or reset.
11853 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011854 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011855 * renegotiating, or if the user mistakenly loaded a session first.)
11856 */
11857 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11858 ssl->session != NULL )
11859 {
11860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11861 }
11862
11863 /*
11864 * We can't check that the config matches the initial one, but we can at
11865 * least check it matches the requirements for serializing.
11866 */
11867 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker7bcf2b52019-07-26 09:02:40 +010011868 mbedtls_ssl_ver_lt(
11869 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ),
11870 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11871 mbedtls_ssl_ver_gt(
11872 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ),
11873 MBEDTLS_SSL_MAJOR_VERSION_3 ) ||
11874 mbedtls_ssl_ver_lt(
11875 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ),
11876 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
11877 mbedtls_ssl_ver_gt(
11878 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ),
11879 MBEDTLS_SSL_MINOR_VERSION_3 ) ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011880 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011881 {
11882 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11883 }
11884
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011885 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11886
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011887 /*
11888 * Check version identifier
11889 */
11890 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11891 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11892
Teppo Järvelin650343c2019-10-03 15:36:59 +030011893 // use regular memcmp as header is not that critical
11894 if( memcmp( p, ssl_serialized_context_header,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011895 sizeof( ssl_serialized_context_header ) ) != 0 )
11896 {
11897 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11898 }
11899 p += sizeof( ssl_serialized_context_header );
11900
11901 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011902 * Session
11903 */
11904 if( (size_t)( end - p ) < 4 )
11905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11906
Arto Kinnunen0b62ce82019-09-04 14:04:57 +030011907 session_len = mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011908 p += 4;
11909
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011910 /* This has been allocated by ssl_handshake_init(), called by
11911 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11912 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011913 ssl->session_in = ssl->session;
11914 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011915 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011916
11917 if( (size_t)( end - p ) < session_len )
11918 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11919
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011920 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011921 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011922 {
11923 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011924 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011925 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011926
11927 p += session_len;
11928
11929 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011930 * Transform
11931 */
11932
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011933 /* This has been allocated by ssl_handshake_init(), called by
11934 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11935 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011936 ssl->transform_in = ssl->transform;
11937 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011938 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011939
11940 /* Read random bytes and populate structure */
11941 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11943
11944 ret = ssl_populate_transform( ssl->transform,
11945 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11946 ssl->session->master,
11947#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11948#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11949 ssl->session->encrypt_then_mac,
11950#endif
11951#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11952 ssl->session->trunc_hmac,
11953#endif
11954#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11955#if defined(MBEDTLS_ZLIB_SUPPORT)
11956 ssl->session->compression,
11957#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011958 p, /* currently pointing to randbytes */
11959 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11960 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11961 ssl );
11962 if( ret != 0 )
11963 return( ret );
11964
11965 p += sizeof( ssl->transform->randbytes );
11966
11967#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11968 /* Read connection IDs and store them */
11969 if( (size_t)( end - p ) < 1 )
11970 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11971
11972 ssl->transform->in_cid_len = *p++;
11973
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011974 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11976
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011977 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11978 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011979 p += ssl->transform->in_cid_len;
11980
11981 ssl->transform->out_cid_len = *p++;
11982
11983 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11985
Teppo Järvelin6f4e0302019-10-04 13:53:53 +030011986 /* Not using more secure mbedtls_platform_memcpy as cid is public */
11987 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011988 p += ssl->transform->out_cid_len;
11989#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11990
11991 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011992 * Saved fields from top-level ssl_context structure
11993 */
11994#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11995 if( (size_t)( end - p ) < 4 )
11996 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11997
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030011998 ssl->badmac_seen = (unsigned)mbedtls_platform_get_uint32_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011999 p += 4;
12000#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
12001
12002#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
12003 if( (size_t)( end - p ) < 16 )
12004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12005
12006 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
12007 ( (uint64_t) p[1] << 48 ) |
12008 ( (uint64_t) p[2] << 40 ) |
12009 ( (uint64_t) p[3] << 32 ) |
12010 ( (uint64_t) p[4] << 24 ) |
12011 ( (uint64_t) p[5] << 16 ) |
12012 ( (uint64_t) p[6] << 8 ) |
12013 ( (uint64_t) p[7] );
12014 p += 8;
12015
12016 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
12017 ( (uint64_t) p[1] << 48 ) |
12018 ( (uint64_t) p[2] << 40 ) |
12019 ( (uint64_t) p[3] << 32 ) |
12020 ( (uint64_t) p[4] << 24 ) |
12021 ( (uint64_t) p[5] << 16 ) |
12022 ( (uint64_t) p[6] << 8 ) |
12023 ( (uint64_t) p[7] );
12024 p += 8;
12025#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12026
12027#if defined(MBEDTLS_SSL_PROTO_DTLS)
12028 if( (size_t)( end - p ) < 1 )
12029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12030
12031 ssl->disable_datagram_packing = *p++;
12032#endif /* MBEDTLS_SSL_PROTO_DTLS */
12033
12034 if( (size_t)( end - p ) < 8 )
12035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12036
Teppo Järvelin91d79382019-10-02 09:09:31 +030012037 mbedtls_platform_memcpy( ssl->cur_out_ctr, p, 8 );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012038 p += 8;
12039
12040#if defined(MBEDTLS_SSL_PROTO_DTLS)
12041 if( (size_t)( end - p ) < 2 )
12042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Arto Kinnunen4f4849a2019-09-09 10:21:18 +030012043 ssl->mtu = (uint16_t)mbedtls_platform_get_uint16_be( p );
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012044 p += 2;
12045#endif /* MBEDTLS_SSL_PROTO_DTLS */
12046
12047#if defined(MBEDTLS_SSL_ALPN)
12048 {
12049 uint8_t alpn_len;
12050 const char **cur;
12051
12052 if( (size_t)( end - p ) < 1 )
12053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12054
12055 alpn_len = *p++;
12056
12057 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
12058 {
12059 /* alpn_chosen should point to an item in the configured list */
12060 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
12061 {
12062 if( strlen( *cur ) == alpn_len &&
Teppo Järvelin61f412e2019-10-03 12:25:22 +030012063 mbedtls_platform_memcmp( p, cur, alpn_len ) == 0 )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020012064 {
12065 ssl->alpn_chosen = *cur;
12066 break;
12067 }
12068 }
12069 }
12070
12071 /* can only happen on conf mismatch */
12072 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
12073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
12074
12075 p += alpn_len;
12076 }
12077#endif /* MBEDTLS_SSL_ALPN */
12078
12079 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012080 * Forced fields from top-level ssl_context structure
12081 *
12082 * Most of them already set to the correct value by mbedtls_ssl_init() and
12083 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
12084 */
12085 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
12086
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012087#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012088 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012089#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
12090#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012091 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020012092#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012093
Hanno Becker83985822019-08-30 10:42:49 +010012094 /* Adjust pointers for header fields of outgoing records to
12095 * the given transform, accounting for explicit IV and CID. */
12096 ssl_update_out_pointers( ssl, ssl->transform );
12097
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020012098#if defined(MBEDTLS_SSL_PROTO_DTLS)
12099 ssl->in_epoch = 1;
12100#endif
12101
12102 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
12103 * which we don't want - otherwise we'd end up freeing the wrong transform
12104 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
12105 if( ssl->handshake != NULL )
12106 {
12107 mbedtls_ssl_handshake_free( ssl );
12108 mbedtls_free( ssl->handshake );
12109 ssl->handshake = NULL;
12110 }
12111
12112 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020012113 * Done - should have consumed entire buffer
12114 */
12115 if( p != end )
12116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012117
12118 return( 0 );
12119}
12120
12121/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020012122 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012123 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012124int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012125 const unsigned char *buf,
12126 size_t len )
12127{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012128 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012129
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020012130 if( ret != 0 )
12131 mbedtls_ssl_free( context );
12132
12133 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012134}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020012135#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020012136
12137/*
Paul Bakker5121ce52009-01-03 21:22:43 +000012138 * Free an SSL context
12139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012140void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000012141{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020012142 if( ssl == NULL )
12143 return;
12144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012146
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012147 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012148 {
Angus Grattond8213d02016-05-25 20:56:48 +100012149 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012150 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012151 }
12152
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010012153 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012154 {
Angus Grattond8213d02016-05-25 20:56:48 +100012155 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012156 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000012157 }
12158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012159#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020012160 if( ssl->compress_buf != NULL )
12161 {
Angus Grattond8213d02016-05-25 20:56:48 +100012162 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012163 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020012164 }
12165#endif
12166
Paul Bakker48916f92012-09-16 19:57:18 +000012167 if( ssl->transform )
12168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012169 mbedtls_ssl_transform_free( ssl->transform );
12170 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000012171 }
12172
12173 if( ssl->handshake )
12174 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020012175 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012176 mbedtls_ssl_transform_free( ssl->transform_negotiate );
12177 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012179 mbedtls_free( ssl->handshake );
12180 mbedtls_free( ssl->transform_negotiate );
12181 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000012182 }
12183
Paul Bakkerc0463502013-02-14 11:19:38 +010012184 if( ssl->session )
12185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012186 mbedtls_ssl_session_free( ssl->session );
12187 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010012188 }
12189
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030012190#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020012191 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000012192 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012193 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012194 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000012195 }
Paul Bakker0be444a2013-08-27 21:55:01 +020012196#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000012197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012198#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
12199 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000012200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
12202 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000012203 }
12204#endif
12205
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012206#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012207 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020012208#endif
12209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000012211
Paul Bakker86f04f42013-02-14 11:20:09 +010012212 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012213 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000012214}
12215
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012216/*
12217 * Initialze mbedtls_ssl_config
12218 */
12219void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
12220{
12221 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010012222
12223#if !defined(MBEDTLS_SSL_PROTO_TLS)
12224 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
12225#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012226}
12227
Simon Butcherc97b6972015-12-27 23:48:17 +000012228#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012229#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012230static int ssl_preset_default_hashes[] = {
12231#if defined(MBEDTLS_SHA512_C)
12232 MBEDTLS_MD_SHA512,
12233 MBEDTLS_MD_SHA384,
12234#endif
12235#if defined(MBEDTLS_SHA256_C)
12236 MBEDTLS_MD_SHA256,
12237 MBEDTLS_MD_SHA224,
12238#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020012239#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012240 MBEDTLS_MD_SHA1,
12241#endif
12242 MBEDTLS_MD_NONE
12243};
Simon Butcherc97b6972015-12-27 23:48:17 +000012244#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012245#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012246
Hanno Becker73f4cb12019-06-27 13:51:07 +010012247#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012248static int ssl_preset_suiteb_ciphersuites[] = {
12249 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
12250 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
12251 0
12252};
Hanno Becker73f4cb12019-06-27 13:51:07 +010012253#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012254
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012255#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012256#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012257static int ssl_preset_suiteb_hashes[] = {
12258 MBEDTLS_MD_SHA256,
12259 MBEDTLS_MD_SHA384,
12260 MBEDTLS_MD_NONE
12261};
12262#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012263#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012264
Hanno Beckerc1096e72019-06-19 12:30:41 +010012265#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012266static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010012267#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012268 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012269#endif
12270#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012271 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010012272#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012273 MBEDTLS_ECP_DP_NONE
12274};
12275#endif
12276
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012277/*
Tillmann Karras588ad502015-09-25 04:27:22 +020012278 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012279 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012280int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012281 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012282{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012283#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012284 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020012285#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012286
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020012287 /* Use the functions here so that they are covered in tests,
12288 * but otherwise access member directly for efficiency */
12289 mbedtls_ssl_conf_endpoint( conf, endpoint );
12290 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012291
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012292 /*
12293 * Things that are common to all presets
12294 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012295#if defined(MBEDTLS_SSL_CLI_C)
12296 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
12297 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012298#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012299 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010012300#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020012301#if defined(MBEDTLS_SSL_SESSION_TICKETS)
12302 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
12303#endif
12304 }
12305#endif
12306
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012307#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012308 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020012309#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012310
12311#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
12312 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
12313#endif
12314
12315#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010012316#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012317 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012318#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
12319#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030012320 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030012321 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010012322#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012323#endif
12324
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010012325#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
12326 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
12327#endif
12328
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020012329#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012330 conf->f_cookie_write = ssl_cookie_write_dummy;
12331 conf->f_cookie_check = ssl_cookie_check_dummy;
12332#endif
12333
Hanno Becker7f376f42019-06-12 16:20:48 +010012334#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
12335 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012336 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
12337#endif
12338
Janos Follath088ce432017-04-10 12:42:31 +010012339#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012340#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010012341 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010012342#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
12343#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010012344
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012345#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010012346#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012347 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012348#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
12349#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012350 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010012351#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
12352#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012353
12354#if defined(MBEDTLS_SSL_RENEGOTIATION)
12355 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +020012356 mbedtls_platform_memset( conf->renego_period, 0x00, 2 );
12357 mbedtls_platform_memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012358#endif
12359
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012360#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
12361 if( endpoint == MBEDTLS_SSL_IS_SERVER )
12362 {
Hanno Becker00d0a682017-10-04 13:14:29 +010012363 const unsigned char dhm_p[] =
12364 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
12365 const unsigned char dhm_g[] =
12366 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
12367
Hanno Beckera90658f2017-10-04 15:29:08 +010012368 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
12369 dhm_p, sizeof( dhm_p ),
12370 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012371 {
12372 return( ret );
12373 }
12374 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020012375#endif
12376
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012377 /*
12378 * Preset-specific defaults
12379 */
12380 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012381 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012382 /*
12383 * NSA Suite B
12384 */
12385 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010012386#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012387 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010012388#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12389#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012390 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010012391#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12392#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012393 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012394#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12395#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012396 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012397#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012398
Hanno Becker73f4cb12019-06-27 13:51:07 +010012399#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012400 conf->ciphersuite_list[0] =
12401 conf->ciphersuite_list[1] =
12402 conf->ciphersuite_list[2] =
12403 conf->ciphersuite_list[3] =
12404 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010012405#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012406
12407#if defined(MBEDTLS_X509_CRT_PARSE_C)
12408 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012409#endif
12410
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012411#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012412#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012413 conf->sig_hashes = ssl_preset_suiteb_hashes;
12414#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012415#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012416
12417#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012418#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012419 conf->curve_list = ssl_preset_suiteb_curves;
12420#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012421#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020012422 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012423
12424 /*
12425 * Default
12426 */
12427 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010012428#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012429 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
12430 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
12431 MBEDTLS_SSL_MIN_MAJOR_VERSION :
12432 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010012433#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
12434#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030012435 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
12436 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
12437 MBEDTLS_SSL_MIN_MINOR_VERSION :
12438 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012439#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020012440 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012441 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
12442#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010012443#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
12444#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
12445 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
12446#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
12447#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
12448 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
12449#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012450
Hanno Becker73f4cb12019-06-27 13:51:07 +010012451#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Becker7bcf2b52019-07-26 09:02:40 +010012452 conf->ciphersuite_list[0] =
12453 conf->ciphersuite_list[1] =
12454 conf->ciphersuite_list[2] =
12455 conf->ciphersuite_list[3] =
12456 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010012457#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012458
12459#if defined(MBEDTLS_X509_CRT_PARSE_C)
12460 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12461#endif
12462
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012463#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012464#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012465 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012466#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012467#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012468
12469#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012470#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012471 conf->curve_list = mbedtls_ecp_grp_id_list();
12472#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012473#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012474
12475#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12476 conf->dhm_min_bitlen = 1024;
12477#endif
12478 }
12479
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012480 return( 0 );
12481}
12482
12483/*
12484 * Free mbedtls_ssl_config
12485 */
12486void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12487{
12488#if defined(MBEDTLS_DHM_C)
12489 mbedtls_mpi_free( &conf->dhm_P );
12490 mbedtls_mpi_free( &conf->dhm_G );
12491#endif
12492
12493#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12494 if( conf->psk != NULL )
12495 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012496 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012497 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012498 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012499 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012500 }
12501
12502 if( conf->psk_identity != NULL )
12503 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012504 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012505 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012506 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012507 conf->psk_identity_len = 0;
12508 }
12509#endif
12510
12511#if defined(MBEDTLS_X509_CRT_PARSE_C)
12512 ssl_key_cert_free( conf->key_cert );
12513#endif
12514
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012515 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012516}
12517
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012518#if defined(MBEDTLS_PK_C) && \
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012519 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) ) || \
12520 ( defined(MBEDTLS_USE_TINYCRYPT) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012521/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012522 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012524unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012526#if defined(MBEDTLS_RSA_C)
12527 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12528 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012529#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012530#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012531 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12532 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012534 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012535}
12536
Hanno Becker7e5437a2017-04-28 17:15:26 +010012537unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12538{
12539 switch( type ) {
12540 case MBEDTLS_PK_RSA:
12541 return( MBEDTLS_SSL_SIG_RSA );
12542 case MBEDTLS_PK_ECDSA:
12543 case MBEDTLS_PK_ECKEY:
12544 return( MBEDTLS_SSL_SIG_ECDSA );
12545 default:
12546 return( MBEDTLS_SSL_SIG_ANON );
12547 }
12548}
12549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012550mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012551{
12552 switch( sig )
12553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012554#if defined(MBEDTLS_RSA_C)
12555 case MBEDTLS_SSL_SIG_RSA:
12556 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012557#endif
Jarno Lamsa7cb5c112019-04-23 15:54:56 +030012558#if defined(MBEDTLS_ECDSA_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012559 case MBEDTLS_SSL_SIG_ECDSA:
12560 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012561#endif
12562 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012563 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012564 }
12565}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012566#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012567
Hanno Becker7e5437a2017-04-28 17:15:26 +010012568#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12569 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12570
12571/* Find an entry in a signature-hash set matching a given hash algorithm. */
12572mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12573 mbedtls_pk_type_t sig_alg )
12574{
12575 switch( sig_alg )
12576 {
12577 case MBEDTLS_PK_RSA:
12578 return( set->rsa );
12579 case MBEDTLS_PK_ECDSA:
12580 return( set->ecdsa );
12581 default:
12582 return( MBEDTLS_MD_NONE );
12583 }
12584}
12585
12586/* Add a signature-hash-pair to a signature-hash set */
12587void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12588 mbedtls_pk_type_t sig_alg,
12589 mbedtls_md_type_t md_alg )
12590{
12591 switch( sig_alg )
12592 {
12593 case MBEDTLS_PK_RSA:
12594 if( set->rsa == MBEDTLS_MD_NONE )
12595 set->rsa = md_alg;
12596 break;
12597
12598 case MBEDTLS_PK_ECDSA:
12599 if( set->ecdsa == MBEDTLS_MD_NONE )
12600 set->ecdsa = md_alg;
12601 break;
12602
12603 default:
12604 break;
12605 }
12606}
12607
12608/* Allow exactly one hash algorithm for each signature. */
12609void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12610 mbedtls_md_type_t md_alg )
12611{
12612 set->rsa = md_alg;
12613 set->ecdsa = md_alg;
12614}
12615
12616#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12617 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12618
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012619/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012620 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012622mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012623{
12624 switch( hash )
12625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012626#if defined(MBEDTLS_MD5_C)
12627 case MBEDTLS_SSL_HASH_MD5:
12628 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012630#if defined(MBEDTLS_SHA1_C)
12631 case MBEDTLS_SSL_HASH_SHA1:
12632 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012633#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012634#if defined(MBEDTLS_SHA256_C)
12635 case MBEDTLS_SSL_HASH_SHA224:
12636 return( MBEDTLS_MD_SHA224 );
12637 case MBEDTLS_SSL_HASH_SHA256:
12638 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012639#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012640#if defined(MBEDTLS_SHA512_C)
12641 case MBEDTLS_SSL_HASH_SHA384:
12642 return( MBEDTLS_MD_SHA384 );
12643 case MBEDTLS_SSL_HASH_SHA512:
12644 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012645#endif
12646 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012647 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012648 }
12649}
12650
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012651/*
12652 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12653 */
12654unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12655{
12656 switch( md )
12657 {
12658#if defined(MBEDTLS_MD5_C)
12659 case MBEDTLS_MD_MD5:
12660 return( MBEDTLS_SSL_HASH_MD5 );
12661#endif
12662#if defined(MBEDTLS_SHA1_C)
12663 case MBEDTLS_MD_SHA1:
12664 return( MBEDTLS_SSL_HASH_SHA1 );
12665#endif
12666#if defined(MBEDTLS_SHA256_C)
12667 case MBEDTLS_MD_SHA224:
12668 return( MBEDTLS_SSL_HASH_SHA224 );
12669 case MBEDTLS_MD_SHA256:
12670 return( MBEDTLS_SSL_HASH_SHA256 );
12671#endif
12672#if defined(MBEDTLS_SHA512_C)
12673 case MBEDTLS_MD_SHA384:
12674 return( MBEDTLS_SSL_HASH_SHA384 );
12675 case MBEDTLS_MD_SHA512:
12676 return( MBEDTLS_SSL_HASH_SHA512 );
12677#endif
12678 default:
12679 return( MBEDTLS_SSL_HASH_NONE );
12680 }
12681}
12682
Hanno Beckeree902df2019-08-23 13:47:47 +010012683#if defined(MBEDTLS_USE_TINYCRYPT)
12684/*
12685 * Check if a curve proposed by the peer is in our list.
12686 * Return 0 if we're willing to use it, -1 otherwise.
12687 */
12688int mbedtls_ssl_check_curve_uecc( const mbedtls_ssl_context *ssl,
12689 mbedtls_uecc_group_id grp_id )
12690{
12691 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_UECC_GRP_ID( own_ec_id )
12692 if( own_ec_id == grp_id )
12693 return( 0 );
12694 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_UECC_GRP_ID
12695
12696 return( -1 );
12697}
12698#endif /* MBEDTLS_USE_TINYCRYPT */
12699
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012700#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012701/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012702 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012703 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012704 */
Hanno Beckeree902df2019-08-23 13:47:47 +010012705int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl,
12706 mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012707{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012708 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12709 if( own_ec_id == grp_id )
12710 return( 0 );
12711 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012712
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012713 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012714}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012715#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012716
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012717#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012718/*
12719 * Check if a hash proposed by the peer is in our list.
12720 * Return 0 if we're willing to use it, -1 otherwise.
12721 */
12722int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12723 mbedtls_md_type_t md )
12724{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012725 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12726 if( md_alg == md )
12727 return( 0 );
12728 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012729
12730 return( -1 );
12731}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012732#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012734#if defined(MBEDTLS_X509_CRT_PARSE_C)
12735int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012736 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012737 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012738 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012739{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012740 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012741#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012742 int usage = 0;
12743#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012744#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012745 const char *ext_oid;
12746 size_t ext_len;
12747#endif
12748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012749#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12750 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012751 ((void) cert);
12752 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012753 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012754#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012756#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12757 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012758 {
12759 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012760 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012762 case MBEDTLS_KEY_EXCHANGE_RSA:
12763 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012764 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012765 break;
12766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012767 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12768 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12769 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12770 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012771 break;
12772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012773 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12774 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012775 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012776 break;
12777
12778 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012779 case MBEDTLS_KEY_EXCHANGE_NONE:
12780 case MBEDTLS_KEY_EXCHANGE_PSK:
12781 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12782 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012783 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012784 usage = 0;
12785 }
12786 }
12787 else
12788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012789 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12790 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012791 }
12792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012793 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012794 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012795 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012796 ret = -1;
12797 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012798#else
12799 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012800#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012802#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12803 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012805 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12806 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012807 }
12808 else
12809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012810 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12811 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012812 }
12813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012814 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012815 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012816 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012817 ret = -1;
12818 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012819#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012820
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012821 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012823#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012824
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012825#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12826 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12827int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12828 unsigned char *output,
12829 unsigned char *data, size_t data_len )
12830{
12831 int ret = 0;
12832 mbedtls_md5_context mbedtls_md5;
12833 mbedtls_sha1_context mbedtls_sha1;
12834
12835 mbedtls_md5_init( &mbedtls_md5 );
12836 mbedtls_sha1_init( &mbedtls_sha1 );
12837
12838 /*
12839 * digitally-signed struct {
12840 * opaque md5_hash[16];
12841 * opaque sha_hash[20];
12842 * };
12843 *
12844 * md5_hash
12845 * MD5(ClientHello.random + ServerHello.random
12846 * + ServerParams);
12847 * sha_hash
12848 * SHA(ClientHello.random + ServerHello.random
12849 * + ServerParams);
12850 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012851 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012852 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012854 goto exit;
12855 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012856 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012857 ssl->handshake->randbytes, 64 ) ) != 0 )
12858 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012860 goto exit;
12861 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012862 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012863 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012865 goto exit;
12866 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012867 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012868 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012870 goto exit;
12871 }
12872
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012873 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012874 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012875 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012876 goto exit;
12877 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012878 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012879 ssl->handshake->randbytes, 64 ) ) != 0 )
12880 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012882 goto exit;
12883 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012884 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012885 data_len ) ) != 0 )
12886 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012888 goto exit;
12889 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012890 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012891 output + 16 ) ) != 0 )
12892 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012893 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012894 goto exit;
12895 }
12896
12897exit:
12898 mbedtls_md5_free( &mbedtls_md5 );
12899 mbedtls_sha1_free( &mbedtls_sha1 );
12900
12901 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012902 mbedtls_ssl_pend_fatal_alert( ssl,
12903 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012904
12905 return( ret );
12906
12907}
12908#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12909 MBEDTLS_SSL_PROTO_TLS1_1 */
12910
12911#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12912 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12913int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012914 unsigned char *hash, size_t *hashlen,
12915 unsigned char *data, size_t data_len,
12916 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012917{
12918 int ret = 0;
12919 mbedtls_md_context_t ctx;
Hanno Beckera5cedbc2019-07-17 11:21:02 +010012920 mbedtls_md_handle_t md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012921 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012922
12923 mbedtls_md_init( &ctx );
12924
12925 /*
12926 * digitally-signed struct {
12927 * opaque client_random[32];
12928 * opaque server_random[32];
12929 * ServerDHParams params;
12930 * };
12931 */
12932 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12933 {
12934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12935 goto exit;
12936 }
12937 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12938 {
12939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12940 goto exit;
12941 }
12942 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12943 {
12944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12945 goto exit;
12946 }
12947 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12948 {
12949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12950 goto exit;
12951 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012952 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012953 {
12954 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12955 goto exit;
12956 }
12957
12958exit:
12959 mbedtls_md_free( &ctx );
12960
12961 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012962 mbedtls_ssl_pend_fatal_alert( ssl,
12963 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012964
12965 return( ret );
12966}
12967#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12968 MBEDTLS_SSL_PROTO_TLS1_2 */
12969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012970#endif /* MBEDTLS_SSL_TLS_C */