blob: a5bd4581911a1fb1d0985f68f8298cfb88d1da3c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckeref982d52019-07-23 15:56:18 +010058#if defined(MBEDTLS_USE_TINYCRYPT)
59static int uecc_rng_wrapper( uint8_t *dest, unsigned int size )
60{
Hanno Beckerd089fad2019-07-24 09:05:05 +010061 int ret;
62 ret = mbedtls_ssl_conf_rng_func( NULL, dest, size );
63 if( ret == 0 )
64 return( (int) size );
65
66 return( 0 );
Hanno Beckeref982d52019-07-23 15:56:18 +010067}
Hanno Becker75f12d12019-07-23 16:16:15 +010068
69int mbedtls_ssl_ecdh_read_peerkey( mbedtls_ssl_context *ssl,
70 unsigned char **p, unsigned char *end )
71{
72 size_t const secp256r1_uncompressed_point_length =
73 1 /* length */ + 1 /* length */ + 2 * NUM_ECC_BYTES /* data */;
74
75 if( (size_t)( end - *p ) < secp256r1_uncompressed_point_length )
76 {
77 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Bad ECDH peer pubkey (too short)" ) );
78 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
79 }
80
81 if( (*p)[0] != 2 * NUM_ECC_BYTES + 1 ||
82 (*p)[1] != 0x04 )
83 {
84 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Unexpected ECDH peer pubkey header - expected { %#02x, %#02x }, got { %#02x, %#02x }",
85 2 * NUM_ECC_BYTES + 1,
86 0x04,
87 (unsigned) (*p)[0],
88 (unsigned) (*p)[1] ) );
89 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
90 }
91
92 memcpy( ssl->handshake->ecdh_peerkey, *p + 2, 2 * NUM_ECC_BYTES );
93
94 *p += secp256r1_uncompressed_point_length;
95 return( 0 );
96}
Hanno Beckeref982d52019-07-23 15:56:18 +010097#endif /* MBEDTLS_USE_TINYCRYPT */
98
Hanno Becker2a43f6f2018-08-10 11:12:52 +010099static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +0100100static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100101
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100102/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100104{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200105#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +0100106 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100107#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200108
109#if defined(MBEDTLS_SSL_PROTO_DTLS)
110 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
111 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200112 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +0200113#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200114#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100115 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +0200116#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +0100117}
118
Hanno Beckerb82350b2019-07-26 07:24:05 +0100119static void ssl_send_pending_fatal_alert( mbedtls_ssl_context *ssl )
120{
121 if( ssl->pending_fatal_alert_msg == MBEDTLS_SSL_ALERT_MSG_NONE )
122 return;
123
124 mbedtls_ssl_send_alert_message( ssl,
125 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
126 ssl->pending_fatal_alert_msg );
127 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
128}
129
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200130/*
131 * Start a timer.
132 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200135{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100136 if( mbedtls_ssl_get_set_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200137 return;
138
139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
Hanno Becker0ae6b242019-06-13 16:45:36 +0100140 mbedtls_ssl_get_set_timer( ssl )( ssl->p_timer,
141 millisecs / 4,
142 millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200143}
144
145/*
146 * Return -1 is timer is expired, 0 if it isn't.
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200149{
Hanno Becker0ae6b242019-06-13 16:45:36 +0100150 if( mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200151 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200152
Hanno Becker0ae6b242019-06-13 16:45:36 +0100153 if( mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154 {
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200156 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200157 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200158
159 return( 0 );
160}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200161
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100162static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
163 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100164static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100165
Hanno Becker02f26092019-07-03 16:13:00 +0100166#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker03e2db62019-07-12 14:40:00 +0100167static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
168 unsigned char *buf,
169 size_t len,
170 mbedtls_record *rec );
171
Hanno Becker02f26092019-07-03 16:13:00 +0100172int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
173 unsigned char *buf,
174 size_t buflen )
175{
Hanno Becker03e2db62019-07-12 14:40:00 +0100176 int ret = 0;
177 mbedtls_record rec;
178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
179 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
180
181 /* We don't support record checking in TLS because
182 * (a) there doesn't seem to be a usecase for it, and
183 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
184 * and we'd need to backup the transform here.
185 */
186#if defined(MBEDTLS_SSL_PROTO_TLS)
187 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
188 {
189 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
190 goto exit;
191 }
192 MBEDTLS_SSL_TRANSPORT_ELSE
193#endif /* MBEDTLS_SSL_PROTO_TLS */
194#if defined(MBEDTLS_SSL_PROTO_DTLS)
195 {
196 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
197 if( ret != 0 )
198 {
199 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
200 goto exit;
201 }
202
203 if( ssl->transform_in != NULL )
204 {
205 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
206 if( ret != 0 )
207 {
208 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
209 goto exit;
210 }
211 }
212 }
213#endif /* MBEDTLS_SSL_PROTO_DTLS */
214
215exit:
216 /* On success, we have decrypted the buffer in-place, so make
217 * sure we don't leak any plaintext data. */
218 mbedtls_platform_zeroize( buf, buflen );
219
220 /* For the purpose of this API, treat messages with unexpected CID
221 * as well as such from future epochs as unexpected. */
222 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
223 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
224 {
225 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
226 }
227
228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
229 return( ret );
Hanno Becker02f26092019-07-03 16:13:00 +0100230}
231#endif /* MBEDTLS_SSL_RECORD_CHECKING */
232
Hanno Becker67bc7c32018-08-06 11:33:50 +0100233#define SSL_DONT_FORCE_FLUSH 0
234#define SSL_FORCE_FLUSH 1
235
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100237
Hanno Beckera5a2b082019-05-15 14:03:01 +0100238#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100239/* Top-level Connection ID API */
240
Hanno Beckere0200da2019-06-13 09:23:43 +0100241#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
242 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
243 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100244int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
245 size_t len,
246 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100247{
248 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
250
Hanno Becker791ec6b2019-05-14 11:45:26 +0100251 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
252 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
253 {
254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
255 }
256
257 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100258 conf->cid_len = len;
259 return( 0 );
260}
Hanno Beckere0200da2019-06-13 09:23:43 +0100261#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
262 !MBEDTLS_SSL_CONF_CID_LEN &&
263 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
264
265#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
266#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
267#endif
268#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
269 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
270#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
271#endif
272
273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
274 !MBEDTLS_SSL_CONF_CID_LEN &&
275 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100276
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100277int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
278 int enable,
279 unsigned char const *own_cid,
280 size_t own_cid_len )
281{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200282 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
284
Hanno Becker07489862019-04-25 16:01:49 +0100285 ssl->negotiate_cid = enable;
286 if( enable == MBEDTLS_SSL_CID_DISABLED )
287 {
288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
289 return( 0 );
290 }
291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100292 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100293
Hanno Beckere0200da2019-06-13 09:23:43 +0100294 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100295 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
297 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100298 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
300 }
301
302 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100303 /* Truncation is not an issue here because
304 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
305 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100306
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100307 return( 0 );
308}
309
310int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
311 int *enabled,
312 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
313 size_t *peer_cid_len )
314{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100315 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100316
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200317 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100318 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
319 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100321 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100322
Hanno Beckercb063f52019-05-03 12:54:52 +0100323 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
324 * were used, but client and server requested the empty CID.
325 * This is indistinguishable from not using the CID extension
326 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100327 if( ssl->transform_in->in_cid_len == 0 &&
328 ssl->transform_in->out_cid_len == 0 )
329 {
330 return( 0 );
331 }
332
Hanno Becker633d6042019-05-22 16:50:35 +0100333 if( peer_cid_len != NULL )
334 {
335 *peer_cid_len = ssl->transform_in->out_cid_len;
336 if( peer_cid != NULL )
337 {
338 memcpy( peer_cid, ssl->transform_in->out_cid,
339 ssl->transform_in->out_cid_len );
340 }
341 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100342
343 *enabled = MBEDTLS_SSL_CID_ENABLED;
344
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100345 return( 0 );
346}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100348
Hanno Beckerd5847772018-08-28 10:09:23 +0100349/* Forward declarations for functions related to message buffering. */
350static void ssl_buffering_free( mbedtls_ssl_context *ssl );
351static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
352 uint8_t slot );
353static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
354static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
355static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
356static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +0100357static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
358 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100359static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100360
Hanno Beckera67dee22018-08-22 10:05:20 +0100361static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100362static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100363{
Hanno Becker11682cc2018-08-22 14:41:02 +0100364 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100365
366 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100367 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100368
369 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
370}
371
Hanno Becker67bc7c32018-08-06 11:33:50 +0100372static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
373{
Hanno Becker11682cc2018-08-22 14:41:02 +0100374 size_t const bytes_written = ssl->out_left;
375 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100376
377 /* Double-check that the write-index hasn't gone
378 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100379 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100380 {
381 /* Should never happen... */
382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
383 }
384
385 return( (int) ( mtu - bytes_written ) );
386}
387
388static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
389{
390 int ret;
391 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100393
394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
395 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
396
397 if( max_len > mfl )
398 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100399
400 /* By the standard (RFC 6066 Sect. 4), the MFL extension
401 * only limits the maximum record payload size, so in theory
402 * we would be allowed to pack multiple records of payload size
403 * MFL into a single datagram. However, this would mean that there's
404 * no way to explicitly communicate MTU restrictions to the peer.
405 *
406 * The following reduction of max_len makes sure that we never
407 * write datagrams larger than MFL + Record Expansion Overhead.
408 */
409 if( max_len <= ssl->out_left )
410 return( 0 );
411
412 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100413#endif
414
415 ret = ssl_get_remaining_space_in_datagram( ssl );
416 if( ret < 0 )
417 return( ret );
418 remaining = (size_t) ret;
419
420 ret = mbedtls_ssl_get_record_expansion( ssl );
421 if( ret < 0 )
422 return( ret );
423 expansion = (size_t) ret;
424
425 if( remaining <= expansion )
426 return( 0 );
427
428 remaining -= expansion;
429 if( remaining >= max_len )
430 remaining = max_len;
431
432 return( (int) remaining );
433}
434
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200435/*
436 * Double the retransmit timeout value, within the allowed range,
437 * returning -1 if the maximum value has already been reached.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200440{
441 uint32_t new_timeout;
442
Hanno Becker1f835fa2019-06-13 10:14:59 +0100443 if( ssl->handshake->retransmit_timeout >=
444 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
445 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200446 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100447 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200448
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200449 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
450 * in the following way: after the initial transmission and a first
451 * retransmission, back off to a temporary estimated MTU of 508 bytes.
452 * This value is guaranteed to be deliverable (if not guaranteed to be
453 * delivered) of any compliant IPv4 (and IPv6) network, and should work
454 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100455 if( ssl->handshake->retransmit_timeout !=
456 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400457 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200458 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
460 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200461
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200462 new_timeout = 2 * ssl->handshake->retransmit_timeout;
463
464 /* Avoid arithmetic overflow and range overflow */
465 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100466 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200467 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100468 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200469 }
470
471 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200473 ssl->handshake->retransmit_timeout ) );
474
475 return( 0 );
476}
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200479{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100480 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200482 ssl->handshake->retransmit_timeout ) );
483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200487/*
488 * Convert max_fragment_length codes to length.
489 * RFC 6066 says:
490 * enum{
491 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
492 * } MaxFragmentLength;
493 * and we add 0 -> extension unused
494 */
Angus Grattond8213d02016-05-25 20:56:48 +1000495static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200496{
Angus Grattond8213d02016-05-25 20:56:48 +1000497 switch( mfl )
498 {
499 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
500 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
501 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
502 return 512;
503 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
504 return 1024;
505 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
506 return 2048;
507 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
508 return 4096;
509 default:
510 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
511 }
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200514
Hanno Becker58fccf22019-02-06 14:30:46 +0000515int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
516 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200517{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_ssl_session_free( dst );
519 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000522
523#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200524 if( src->peer_cert != NULL )
525 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200526 int ret;
527
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200528 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200529 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200535 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200538 dst->peer_cert = NULL;
539 return( ret );
540 }
541 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100542#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000543 if( src->peer_cert_digest != NULL )
544 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000545 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000546 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000547 if( dst->peer_cert_digest == NULL )
548 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
549
550 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
551 src->peer_cert_digest_len );
552 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000553 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000554 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100555#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200558
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200559#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200560 if( src->ticket != NULL )
561 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200562 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200563 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200565
566 memcpy( dst->ticket, src->ticket, src->ticket_len );
567 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200568#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200569
570 return( 0 );
571}
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
574int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200575 const unsigned char *key_enc, const unsigned char *key_dec,
576 size_t keylen,
577 const unsigned char *iv_enc, const unsigned char *iv_dec,
578 size_t ivlen,
579 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200580 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
582int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
583int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
584int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
585int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
586#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000587
Paul Bakker5121ce52009-01-03 21:22:43 +0000588/*
589 * Key material generation
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200592static int ssl3_prf( const unsigned char *secret, size_t slen,
593 const char *label,
594 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000595 unsigned char *dstbuf, size_t dlen )
596{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100597 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000598 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200599 mbedtls_md5_context md5;
600 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000601 unsigned char padding[16];
602 unsigned char sha1sum[20];
603 ((void)label);
604
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200605 mbedtls_md5_init( &md5 );
606 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200607
Paul Bakker5f70b252012-09-13 14:23:06 +0000608 /*
609 * SSLv3:
610 * block =
611 * MD5( secret + SHA1( 'A' + secret + random ) ) +
612 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
613 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
614 * ...
615 */
616 for( i = 0; i < dlen / 16; i++ )
617 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200618 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000619
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100620 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100621 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100622 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100623 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100624 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100625 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100626 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100627 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100628 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100629 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100631 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100632 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100633 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100634 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100635 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100636 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100637 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100638 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000639 }
640
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100641exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200642 mbedtls_md5_free( &md5 );
643 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500645 mbedtls_platform_zeroize( padding, sizeof( padding ) );
646 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000647
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100648 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000649}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200653static int tls1_prf( const unsigned char *secret, size_t slen,
654 const char *label,
655 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000656 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000657{
Paul Bakker23986e52011-04-24 08:57:21 +0000658 size_t nb, hs;
659 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200660 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 unsigned char tmp[128];
662 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 const mbedtls_md_info_t *md_info;
664 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100665 int ret;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
669 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 hs = ( slen + 1 ) / 2;
673 S1 = secret;
674 S2 = secret + slen - hs;
675
676 nb = strlen( label );
677 memcpy( tmp + 20, label, nb );
678 memcpy( tmp + 20 + nb, random, rlen );
679 nb += rlen;
680
681 /*
682 * First compute P_md5(secret,label+random)[0..dlen]
683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100688 return( ret );
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
691 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
692 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 for( i = 0; i < dlen; i += 16 )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_md_hmac_reset ( &md_ctx );
697 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
698 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_md_hmac_reset ( &md_ctx );
701 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
702 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
705
706 for( j = 0; j < k; j++ )
707 dstbuf[i + j] = h_i[j];
708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100711
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 /*
713 * XOR out with P_sha1(secret,label+random)[0..dlen]
714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100719 return( ret );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
722 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
723 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725 for( i = 0; i < dlen; i += 20 )
726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md_hmac_reset ( &md_ctx );
728 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
729 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_md_hmac_reset ( &md_ctx );
732 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
733 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
735 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
736
737 for( j = 0; j < k; j++ )
738 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
739 }
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500743 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
744 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
746 return( 0 );
747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
751static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100752 const unsigned char *secret, size_t slen,
753 const char *label,
754 const unsigned char *random, size_t rlen,
755 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000756{
757 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100758 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000759 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
761 const mbedtls_md_info_t *md_info;
762 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100763 int ret;
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
768 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100771
772 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000774
775 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776 memcpy( tmp + md_len, label, nb );
777 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000778 nb += rlen;
779
780 /*
781 * Compute P_<hash>(secret, label + random)[0..dlen]
782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100784 return( ret );
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
787 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
788 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100789
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100790 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_md_hmac_reset ( &md_ctx );
793 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
794 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_md_hmac_reset ( &md_ctx );
797 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
798 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000799
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000801
802 for( j = 0; j < k; j++ )
803 dstbuf[i + j] = h_i[j];
804 }
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100807
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500808 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
809 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000810
811 return( 0 );
812}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100815static int tls_prf_sha256( const unsigned char *secret, size_t slen,
816 const char *label,
817 const unsigned char *random, size_t rlen,
818 unsigned char *dstbuf, size_t dlen )
819{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100821 label, random, rlen, dstbuf, dlen ) );
822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200826static int tls_prf_sha384( const unsigned char *secret, size_t slen,
827 const char *label,
828 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000829 unsigned char *dstbuf, size_t dlen )
830{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100832 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000833}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_SHA512_C */
835#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100837/*
838 * Call the appropriate PRF function
839 */
840MBEDTLS_ALWAYS_INLINE
841static inline int ssl_prf( int minor_ver,
842 mbedtls_md_type_t hash,
843 const unsigned char *secret, size_t slen,
844 const char *label,
845 const unsigned char *random, size_t rlen,
846 unsigned char *dstbuf, size_t dlen )
847{
848#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
849 (void) hash;
850#endif
851
852#if defined(MBEDTLS_SSL_PROTO_SSL3)
853 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
854 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
855 else
856#endif
857#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
858 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
859 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
860 else
861#endif
862#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
863#if defined(MBEDTLS_SHA512_C)
864 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
865 hash == MBEDTLS_MD_SHA384 )
866 {
867 return( tls_prf_sha384( secret, slen, label, random, rlen,
868 dstbuf, dlen ) );
869 }
870 else
871#endif
872#if defined(MBEDTLS_SHA256_C)
873 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
874 {
875 return( tls_prf_sha256( secret, slen, label, random, rlen,
876 dstbuf, dlen ) );
877 }
878#endif
879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
880
881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
882}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200883
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100884#if defined(MBEDTLS_SSL_PROTO_SSL3)
885static void ssl_calc_finished_ssl(
886 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
887{
888 const char *sender;
889 mbedtls_md5_context md5;
890 mbedtls_sha1_context sha1;
891
892 unsigned char padbuf[48];
893 unsigned char md5sum[16];
894 unsigned char sha1sum[20];
895
896 mbedtls_ssl_session *session = ssl->session_negotiate;
897 if( !session )
898 session = ssl->session;
899
900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
901
902 mbedtls_md5_init( &md5 );
903 mbedtls_sha1_init( &sha1 );
904
905 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
906 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
907
908 /*
909 * SSLv3:
910 * hash =
911 * MD5( master + pad2 +
912 * MD5( handshake + sender + master + pad1 ) )
913 * + SHA1( master + pad2 +
914 * SHA1( handshake + sender + master + pad1 ) )
915 */
916
917#if !defined(MBEDTLS_MD5_ALT)
918 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
919 md5.state, sizeof( md5.state ) );
920#endif
921
922#if !defined(MBEDTLS_SHA1_ALT)
923 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
924 sha1.state, sizeof( sha1.state ) );
925#endif
926
927 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
928 : "SRVR";
929
930 memset( padbuf, 0x36, 48 );
931
932 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
933 mbedtls_md5_update_ret( &md5, session->master, 48 );
934 mbedtls_md5_update_ret( &md5, padbuf, 48 );
935 mbedtls_md5_finish_ret( &md5, md5sum );
936
937 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
938 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
939 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
940 mbedtls_sha1_finish_ret( &sha1, sha1sum );
941
942 memset( padbuf, 0x5C, 48 );
943
944 mbedtls_md5_starts_ret( &md5 );
945 mbedtls_md5_update_ret( &md5, session->master, 48 );
946 mbedtls_md5_update_ret( &md5, padbuf, 48 );
947 mbedtls_md5_update_ret( &md5, md5sum, 16 );
948 mbedtls_md5_finish_ret( &md5, buf );
949
950 mbedtls_sha1_starts_ret( &sha1 );
951 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
952 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
953 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
954 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
955
956 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
957
958 mbedtls_md5_free( &md5 );
959 mbedtls_sha1_free( &sha1 );
960
961 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
962 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
963 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
964
965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
966}
967#endif /* MBEDTLS_SSL_PROTO_SSL3 */
968
969#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
970static void ssl_calc_finished_tls(
971 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
972{
973 int len = 12;
974 const char *sender;
975 mbedtls_md5_context md5;
976 mbedtls_sha1_context sha1;
977 unsigned char padbuf[36];
978
979 mbedtls_ssl_session *session = ssl->session_negotiate;
980 if( !session )
981 session = ssl->session;
982
983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
984
985 mbedtls_md5_init( &md5 );
986 mbedtls_sha1_init( &sha1 );
987
988 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
989 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
990
991 /*
992 * TLSv1:
993 * hash = PRF( master, finished_label,
994 * MD5( handshake ) + SHA1( handshake ) )[0..11]
995 */
996
997#if !defined(MBEDTLS_MD5_ALT)
998 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
999 md5.state, sizeof( md5.state ) );
1000#endif
1001
1002#if !defined(MBEDTLS_SHA1_ALT)
1003 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1004 sha1.state, sizeof( sha1.state ) );
1005#endif
1006
1007 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1008 ? "client finished"
1009 : "server finished";
1010
1011 mbedtls_md5_finish_ret( &md5, padbuf );
1012 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1013
1014 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1015 mbedtls_ssl_suite_get_mac(
1016 mbedtls_ssl_ciphersuite_from_id(
1017 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1018 session->master, 48, sender,
1019 padbuf, 36, buf, len );
1020
1021 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1022
1023 mbedtls_md5_free( &md5 );
1024 mbedtls_sha1_free( &sha1 );
1025
1026 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1027
1028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1029}
1030#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1031
1032#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1033#if defined(MBEDTLS_SHA256_C)
1034static void ssl_calc_finished_tls_sha256(
1035 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1036{
1037 int len = 12;
1038 const char *sender;
1039 mbedtls_sha256_context sha256;
1040 unsigned char padbuf[32];
1041
1042 mbedtls_ssl_session *session = ssl->session_negotiate;
1043 if( !session )
1044 session = ssl->session;
1045
1046 mbedtls_sha256_init( &sha256 );
1047
1048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1049
1050 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1051
1052 /*
1053 * TLSv1.2:
1054 * hash = PRF( master, finished_label,
1055 * Hash( handshake ) )[0.11]
1056 */
1057
1058#if !defined(MBEDTLS_SHA256_ALT)
1059 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1060 sha256.state, sizeof( sha256.state ) );
1061#endif
1062
1063 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1064 ? "client finished"
1065 : "server finished";
1066
1067 mbedtls_sha256_finish_ret( &sha256, padbuf );
1068
1069 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1070 mbedtls_ssl_suite_get_mac(
1071 mbedtls_ssl_ciphersuite_from_id(
1072 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1073 session->master, 48, sender,
1074 padbuf, 32, buf, len );
1075
1076 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1077
1078 mbedtls_sha256_free( &sha256 );
1079
1080 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1081
1082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1083}
1084#endif /* MBEDTLS_SHA256_C */
1085
1086#if defined(MBEDTLS_SHA512_C)
1087static void ssl_calc_finished_tls_sha384(
1088 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1089{
1090 int len = 12;
1091 const char *sender;
1092 mbedtls_sha512_context sha512;
1093 unsigned char padbuf[48];
1094
1095 mbedtls_ssl_session *session = ssl->session_negotiate;
1096 if( !session )
1097 session = ssl->session;
1098
1099 mbedtls_sha512_init( &sha512 );
1100
1101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1102
1103 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1104
1105 /*
1106 * TLSv1.2:
1107 * hash = PRF( master, finished_label,
1108 * Hash( handshake ) )[0.11]
1109 */
1110
1111#if !defined(MBEDTLS_SHA512_ALT)
1112 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1113 sha512.state, sizeof( sha512.state ) );
1114#endif
1115
1116 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1117 ? "client finished"
1118 : "server finished";
1119
1120 mbedtls_sha512_finish_ret( &sha512, padbuf );
1121
1122 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1123 mbedtls_ssl_suite_get_mac(
1124 mbedtls_ssl_ciphersuite_from_id(
1125 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1126 session->master, 48, sender,
1127 padbuf, 48, buf, len );
1128
1129 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1130
1131 mbedtls_sha512_free( &sha512 );
1132
1133 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1134
1135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1136}
1137#endif /* MBEDTLS_SHA512_C */
1138#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1139
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001140MBEDTLS_ALWAYS_INLINE
1141static inline int ssl_calc_finished( int minor_ver,
1142 mbedtls_md_type_t hash,
1143 mbedtls_ssl_context *ssl,
1144 unsigned char *buf,
1145 int from )
1146{
1147#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1148 (void) hash;
1149#endif
1150
1151#if defined(MBEDTLS_SSL_PROTO_SSL3)
1152 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1153 ssl_calc_finished_ssl( ssl, buf, from );
1154 else
1155#endif
1156#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1157 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1158 ssl_calc_finished_tls( ssl, buf, from );
1159 else
1160#endif
1161#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1162#if defined(MBEDTLS_SHA512_C)
1163 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1164 hash == MBEDTLS_MD_SHA384 )
1165 {
1166 ssl_calc_finished_tls_sha384( ssl, buf, from );
1167 }
1168 else
1169#endif
1170#if defined(MBEDTLS_SHA256_C)
1171 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1172 ssl_calc_finished_tls_sha256( ssl, buf, from );
1173 else
1174#endif
1175#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1177
1178 return( 0 );
1179}
1180
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001181/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001182 * Populate a transform structure with session keys and all the other
1183 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001184 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001185 * Parameters:
1186 * - [in/out]: transform: structure to populate
1187 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001188 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001189 * - [in] ciphersuite
1190 * - [in] master
1191 * - [in] encrypt_then_mac
1192 * - [in] trunc_hmac
1193 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001194 * - [in] tls_prf: pointer to PRF to use for key derivation
1195 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001196 * - [in] minor_ver: SSL/TLS minor version
1197 * - [in] endpoint: client or server
1198 * - [in] ssl: optionally used for:
1199 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1200 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1201 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001202 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001203static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001204 int ciphersuite,
1205 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001206#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001207#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1208 int encrypt_then_mac,
1209#endif
1210#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1211 int trunc_hmac,
1212#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001213#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001214#if defined(MBEDTLS_ZLIB_SUPPORT)
1215 int compression,
1216#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001217 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001218 int minor_ver,
1219 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001220 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001221{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001222 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001223 unsigned char keyblk[256];
1224 unsigned char *key1;
1225 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001226 unsigned char *mac_enc;
1227 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001228 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001229 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001230 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001231 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 const mbedtls_cipher_info_t *cipher_info;
1233 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001234
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001235#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1236 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1237 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001238 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001239 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001240#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001241
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001242 /*
1243 * Some data just needs copying into the structure
1244 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001245#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1246 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001247 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001248#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001249
1250#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001251 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001252#else
1253 ((void) minor_ver);
1254#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001256#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1257 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1258#endif
1259
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001260 /*
1261 * Get various info structures
1262 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001263 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001264 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001265 {
1266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001267 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1269 }
1270
Hanno Becker473f98f2019-06-26 10:27:32 +01001271 cipher_info = mbedtls_cipher_info_from_type(
1272 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001273 if( cipher_info == NULL )
1274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001276 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001278 }
1279
Hanno Becker473f98f2019-06-26 10:27:32 +01001280 md_info = mbedtls_md_info_from_type(
1281 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001282 if( md_info == NULL )
1283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001285 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 }
1288
Hanno Beckera5a2b082019-05-15 14:03:01 +01001289#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001290 /* Copy own and peer's CID if the use of the CID
1291 * extension has been negotiated. */
1292 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1293 {
1294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001295
Hanno Becker4932f9f2019-05-03 15:23:51 +01001296 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001297 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001298 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001299 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001300
1301 transform->out_cid_len = ssl->handshake->peer_cid_len;
1302 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1303 ssl->handshake->peer_cid_len );
1304 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1305 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001306 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001308
Paul Bakker5121ce52009-01-03 21:22:43 +00001309 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001310 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001311 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001312 ret = ssl_prf( minor_ver,
1313 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1314 master, 48, "key expansion", randbytes, 64,
1315 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001316 if( ret != 0 )
1317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001319 return( ret );
1320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001323 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001324 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001325 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
Paul Bakker5121ce52009-01-03 21:22:43 +00001328 /*
1329 * Determine the appropriate key, IV and MAC length.
1330 */
Paul Bakker68884e32013-01-07 18:20:04 +01001331
Hanno Beckere7f2df02017-12-27 08:17:40 +00001332 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001333
Hanno Beckerf1229442018-01-03 15:32:31 +00001334#if defined(MBEDTLS_GCM_C) || \
1335 defined(MBEDTLS_CCM_C) || \
1336 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001338 cipher_info->mode == MBEDTLS_MODE_CCM ||
1339 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001341 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001342 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001343 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1344 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001345
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001346 /* All modes haves 96-bit IVs;
1347 * GCM and CCM has 4 implicit and 8 explicit bytes
1348 * ChachaPoly has all 12 bytes implicit
1349 */
Paul Bakker68884e32013-01-07 18:20:04 +01001350 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001351 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1352 transform->fixed_ivlen = 12;
1353 else
1354 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001355 }
1356 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001357#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1358#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1359 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1360 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001361 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001362 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1364 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001367 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001370 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001371 mac_key_len = mbedtls_md_get_size( md_info );
1372 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001375 /*
1376 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1377 * (rfc 6066 page 13 or rfc 2104 section 4),
1378 * so we only need to adjust the length here.
1379 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001380 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001383
1384#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1385 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001386 * HMAC implementation which also truncates the key
1387 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001388 mac_key_len = transform->maclen;
1389#endif
1390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001392
1393 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001394 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001396 else
1397#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1398 {
1399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1400 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Hanno Beckera9d5c452019-07-25 16:47:12 +01001403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001404 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001405 (unsigned) transform->ivlen,
1406 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
1408 /*
1409 * Finally setup the cipher contexts, IVs and MAC secrets.
1410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001412 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001414 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001415 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Paul Bakker68884e32013-01-07 18:20:04 +01001417 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001418 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001420 /*
1421 * This is not used in TLS v1.1.
1422 */
Paul Bakker48916f92012-09-16 19:57:18 +00001423 iv_copy_len = ( transform->fixed_ivlen ) ?
1424 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001425 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1426 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001427 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 }
1429 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_SSL_CLI_C */
1431#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001432 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001433 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001434 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001435 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
Hanno Becker81c7b182017-11-09 18:39:33 +00001437 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001438 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001439
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001440 /*
1441 * This is not used in TLS v1.1.
1442 */
Paul Bakker48916f92012-09-16 19:57:18 +00001443 iv_copy_len = ( transform->fixed_ivlen ) ?
1444 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001445 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1446 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001447 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001449 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
Hanno Becker92231322018-01-03 15:32:51 +00001456#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001458 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001459 {
Hanno Becker92231322018-01-03 15:32:51 +00001460 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001464 }
1465
Hanno Becker81c7b182017-11-09 18:39:33 +00001466 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1467 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001468 }
1469 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1471#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1472 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001473 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001474 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001475 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1476 For AEAD-based ciphersuites, there is nothing to do here. */
1477 if( mac_key_len != 0 )
1478 {
1479 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1480 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1481 }
Paul Bakker68884e32013-01-07 18:20:04 +01001482 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001483 else
1484#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1487 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001488 }
Hanno Becker92231322018-01-03 15:32:51 +00001489#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1492 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001493 {
1494 int ret = 0;
1495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001497
Hanno Beckere7f2df02017-12-27 08:17:40 +00001498 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001499 transform->iv_enc, transform->iv_dec,
1500 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001501 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001502 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1505 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001506 }
1507 }
Hanno Becker92231322018-01-03 15:32:51 +00001508#else
1509 ((void) mac_dec);
1510 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001512
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001513#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1514 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001515 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001516 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001517 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001518 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001519 iv_copy_len );
1520 }
1521#endif
1522
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001523 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001524 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001527 return( ret );
1528 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001529
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001530 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001531 cipher_info ) ) != 0 )
1532 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001534 return( ret );
1535 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001538 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001542 return( ret );
1543 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001546 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001550 return( ret );
1551 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_CIPHER_MODE_CBC)
1554 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1557 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001561 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1564 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001567 return( ret );
1568 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001572 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001573
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001574 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001576 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001579
Paul Bakker48916f92012-09-16 19:57:18 +00001580 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1581 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001582
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001583 if( deflateInit( &transform->ctx_deflate,
1584 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001585 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1588 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001589 }
1590 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001592
Paul Bakker5121ce52009-01-03 21:22:43 +00001593 return( 0 );
1594}
1595
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001596#if defined(MBEDTLS_SSL_PROTO_SSL3)
1597static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1598 unsigned char hash[36],
1599 size_t *hlen )
1600{
1601 mbedtls_md5_context md5;
1602 mbedtls_sha1_context sha1;
1603 unsigned char pad_1[48];
1604 unsigned char pad_2[48];
1605
1606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1607
1608 mbedtls_md5_init( &md5 );
1609 mbedtls_sha1_init( &sha1 );
1610
1611 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1612 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1613
1614 memset( pad_1, 0x36, 48 );
1615 memset( pad_2, 0x5C, 48 );
1616
1617 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1618 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1619 mbedtls_md5_finish_ret( &md5, hash );
1620
1621 mbedtls_md5_starts_ret( &md5 );
1622 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1623 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1624 mbedtls_md5_update_ret( &md5, hash, 16 );
1625 mbedtls_md5_finish_ret( &md5, hash );
1626
1627 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1628 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1629 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1630
1631 mbedtls_sha1_starts_ret( &sha1 );
1632 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1633 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1634 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1635 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1636
1637 *hlen = 36;
1638
1639 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1641
1642 mbedtls_md5_free( &md5 );
1643 mbedtls_sha1_free( &sha1 );
1644
1645 return;
1646}
1647#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1648
1649#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1650static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1651 unsigned char hash[36],
1652 size_t *hlen )
1653{
1654 mbedtls_md5_context md5;
1655 mbedtls_sha1_context sha1;
1656
1657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1658
1659 mbedtls_md5_init( &md5 );
1660 mbedtls_sha1_init( &sha1 );
1661
1662 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1663 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1664
1665 mbedtls_md5_finish_ret( &md5, hash );
1666 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1667
1668 *hlen = 36;
1669
1670 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1672
1673 mbedtls_md5_free( &md5 );
1674 mbedtls_sha1_free( &sha1 );
1675
1676 return;
1677}
1678#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1679
1680#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1681#if defined(MBEDTLS_SHA256_C)
1682static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1683 unsigned char hash[32],
1684 size_t *hlen )
1685{
1686 mbedtls_sha256_context sha256;
1687
1688 mbedtls_sha256_init( &sha256 );
1689
1690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1691
1692 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1693 mbedtls_sha256_finish_ret( &sha256, hash );
1694
1695 *hlen = 32;
1696
1697 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1699
1700 mbedtls_sha256_free( &sha256 );
1701
1702 return;
1703}
1704#endif /* MBEDTLS_SHA256_C */
1705
1706#if defined(MBEDTLS_SHA512_C)
1707static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1708 unsigned char hash[48],
1709 size_t *hlen )
1710{
1711 mbedtls_sha512_context sha512;
1712
1713 mbedtls_sha512_init( &sha512 );
1714
1715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1716
1717 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1718 mbedtls_sha512_finish_ret( &sha512, hash );
1719
1720 *hlen = 48;
1721
1722 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1724
1725 mbedtls_sha512_free( &sha512 );
1726
1727 return;
1728}
1729#endif /* MBEDTLS_SHA512_C */
1730#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1731
Hanno Becker2f41b242019-08-15 17:29:43 +01001732int mbedtls_ssl_calc_verify( int minor_ver,
1733 mbedtls_md_type_t hash,
1734 mbedtls_ssl_context const *ssl,
1735 unsigned char *dst,
1736 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001737{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001738#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1739 (void) hash;
1740#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001741
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001742#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001743 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001744 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001745 else
1746#endif
1747#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001748 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001749 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001750 else
1751#endif
1752#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1753#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001754 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1755 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001756 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001757 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001758 }
1759 else
1760#endif
1761#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001762 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001763 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001764 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001765 }
1766 else
1767#endif
1768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1769 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1771 }
1772
1773 return( 0 );
1774}
1775
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001776/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001777 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001778 *
1779 * Parameters:
1780 * [in/out] handshake
1781 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1782 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001783 * [out] master
1784 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001785 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001786static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001787 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001788 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001789{
1790 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001791
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001792/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1793/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1794/* (void) ssl; */
1795/* #endif */
1796
1797 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1798 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001800#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001801 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001802 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1804 return( 0 );
1805 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001806#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001807
1808 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1809 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001810
1811#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001812 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1813 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001814 {
1815 unsigned char session_hash[48];
1816 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001817
Hanno Becker2f41b242019-08-15 17:29:43 +01001818 mbedtls_ssl_calc_verify(
1819 mbedtls_ssl_get_minor_ver( ssl ),
1820 mbedtls_ssl_suite_get_mac( ciphersuite ),
1821 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001822
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001823 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1824 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001825
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001826 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1827 mbedtls_ssl_suite_get_mac( ciphersuite ),
1828 handshake->premaster, handshake->pmslen,
1829 "extended master secret",
1830 session_hash, hash_len,
1831 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001832 }
1833 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001834#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001835 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001836 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1837 mbedtls_ssl_suite_get_mac( ciphersuite ),
1838 handshake->premaster, handshake->pmslen,
1839 "master secret",
1840 handshake->randbytes, 64,
1841 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001842 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001843 if( ret != 0 )
1844 {
1845 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1846 return( ret );
1847 }
1848
1849 mbedtls_platform_zeroize( handshake->premaster,
1850 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001851
1852 return( 0 );
1853}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001854
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001855int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1856{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001857 int ret;
1858
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1860
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001861 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001862 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001863 ssl->session_negotiate->master,
1864 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001865 if( ret != 0 )
1866 {
1867 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1868 return( ret );
1869 }
1870
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001871 /* Swap the client and server random values:
1872 * - MS derivation wanted client+server (RFC 5246 8.1)
1873 * - key derivation wants server+client (RFC 5246 6.3) */
1874 {
1875 unsigned char tmp[64];
1876 memcpy( tmp, ssl->handshake->randbytes, 64 );
1877 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1878 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1879 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1880 }
1881
1882 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001883 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001884 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1885 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001886#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001887#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001888 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001889#endif
1890#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001891 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001892#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001893#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001894#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001895 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001896#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001897 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001898 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001899 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1900 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001901 if( ret != 0 )
1902 {
1903 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1904 return( ret );
1905 }
1906
1907 /* We no longer need Server/ClientHello.random values */
1908 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1909 sizeof( ssl->handshake->randbytes ) );
1910
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001911 /* Allocate compression buffer */
1912#if defined(MBEDTLS_ZLIB_SUPPORT)
1913 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1914 ssl->compress_buf == NULL )
1915 {
1916 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1917 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1918 if( ssl->compress_buf == NULL )
1919 {
1920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001921 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001922 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1923 }
1924 }
1925#endif
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1928
1929 return( 0 );
1930}
1931
Hanno Becker09d23642019-07-22 17:18:18 +01001932int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1933{
1934 int ret;
1935
1936 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1937 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1938
Hanno Beckera3c2c172019-07-23 16:51:57 +01001939#if defined(MBEDTLS_USE_TINYCRYPT)
1940 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1941 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1942 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1943 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA )
1944 {
1945 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001946 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001947
1948 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1949 ssl->handshake->ecdh_privkey,
1950 ssl->handshake->premaster,
1951 uecc_curve ) )
1952 {
1953 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1954 }
1955
1956 ssl->handshake->pmslen = NUM_ECC_BYTES;
1957 }
1958 else
1959#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001960#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1961 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1962 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1963 {
1964 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1965 ssl->handshake->premaster,
1966 MBEDTLS_PREMASTER_SIZE,
1967 &ssl->handshake->pmslen,
1968 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001969 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001970 {
1971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1972 return( ret );
1973 }
1974
1975 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1976 }
1977 else
1978#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001979#if defined(MBEDTLS_ECDH_C) && \
1980 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1981 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1982 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1983 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01001984 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1985 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
1986 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1987 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1988 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1989 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1990 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1991 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
1992 {
1993 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
1994 &ssl->handshake->pmslen,
1995 ssl->handshake->premaster,
1996 MBEDTLS_MPI_MAX_SIZE,
1997 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001998 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001999 {
2000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2001 return( ret );
2002 }
2003
2004 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2005 }
2006 else
2007#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2008 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2009 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2010 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2011#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2012 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2013 {
2014 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002015 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002016 {
2017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2018 return( ret );
2019 }
2020 }
2021 else
2022#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2023#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2024 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2025 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2026 {
2027 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2028 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2029 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002030 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002031 if( ret != 0 )
2032 {
2033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2034 return( ret );
2035 }
2036 }
2037 else
2038#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2039#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2040 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2041 == MBEDTLS_KEY_EXCHANGE_RSA )
2042 {
2043 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002044 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002045 * ssl_rsa_generate_partial_pms(). Only the
2046 * PMS length needs to be set. */
2047 ssl->handshake->pmslen = 48;
2048 }
2049 else
2050#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2051 {
2052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2054 }
2055
2056 return( 0 );
2057}
2058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2060int 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 +02002061{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002062 unsigned char *p = ssl->handshake->premaster;
2063 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002064 const unsigned char *psk = ssl->conf->psk;
2065 size_t psk_len = ssl->conf->psk_len;
2066
2067 /* If the psk callback was called, use its result */
2068 if( ssl->handshake->psk != NULL )
2069 {
2070 psk = ssl->handshake->psk;
2071 psk_len = ssl->handshake->psk_len;
2072 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002073
2074 /*
2075 * PMS = struct {
2076 * opaque other_secret<0..2^16-1>;
2077 * opaque psk<0..2^16-1>;
2078 * };
2079 * with "other_secret" depending on the particular key exchange
2080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2082 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002083 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002084 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002086
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002087 *(p++) = (unsigned char)( psk_len >> 8 );
2088 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002089
2090 if( end < p || (size_t)( end - p ) < psk_len )
2091 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2092
2093 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002094 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002095 }
2096 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2098#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2099 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002100 {
2101 /*
2102 * other_secret already set by the ClientKeyExchange message,
2103 * and is 48 bytes long
2104 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002105 if( end - p < 2 )
2106 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2107
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002108 *p++ = 0;
2109 *p++ = 48;
2110 p += 48;
2111 }
2112 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2114#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2115 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002116 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002117 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002118 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002119
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002120 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002122 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002123 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002124 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002127 return( ret );
2128 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002129 *(p++) = (unsigned char)( len >> 8 );
2130 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002131 p += len;
2132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002134 }
2135 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2137#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2138 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002139 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002140 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002141 size_t zlen;
2142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002144 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002145 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002146 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002149 return( ret );
2150 }
2151
2152 *(p++) = (unsigned char)( zlen >> 8 );
2153 *(p++) = (unsigned char)( zlen );
2154 p += zlen;
2155
Janos Follath3fbdada2018-08-15 10:26:53 +01002156 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2157 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002158 }
2159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002164 }
2165
2166 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002167 if( end - p < 2 )
2168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002169
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002170 *(p++) = (unsigned char)( psk_len >> 8 );
2171 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002172
2173 if( end < p || (size_t)( end - p ) < psk_len )
2174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2175
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002176 memcpy( p, psk, psk_len );
2177 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002178
2179 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2180
2181 return( 0 );
2182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002186/*
2187 * SSLv3.0 MAC functions
2188 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002189#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002190static void ssl_mac( mbedtls_md_context_t *md_ctx,
2191 const unsigned char *secret,
2192 const unsigned char *buf, size_t len,
2193 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002194 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002195{
2196 unsigned char header[11];
2197 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002198 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2200 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002201
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002202 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002204 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002205 else
Paul Bakker68884e32013-01-07 18:20:04 +01002206 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002207
2208 memcpy( header, ctr, 8 );
2209 header[ 8] = (unsigned char) type;
2210 header[ 9] = (unsigned char)( len >> 8 );
2211 header[10] = (unsigned char)( len );
2212
Paul Bakker68884e32013-01-07 18:20:04 +01002213 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 mbedtls_md_starts( md_ctx );
2215 mbedtls_md_update( md_ctx, secret, md_size );
2216 mbedtls_md_update( md_ctx, padding, padlen );
2217 mbedtls_md_update( md_ctx, header, 11 );
2218 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002219 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002220
Paul Bakker68884e32013-01-07 18:20:04 +01002221 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 mbedtls_md_starts( md_ctx );
2223 mbedtls_md_update( md_ctx, secret, md_size );
2224 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002225 mbedtls_md_update( md_ctx, out, md_size );
2226 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002229
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002230/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002231 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002232#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002233 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2234 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2235 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2236/* This function makes sure every byte in the memory region is accessed
2237 * (in ascending addresses order) */
2238static void ssl_read_memory( unsigned char *p, size_t len )
2239{
2240 unsigned char acc = 0;
2241 volatile unsigned char force;
2242
2243 for( ; len != 0; p++, len-- )
2244 acc ^= *p;
2245
2246 force = acc;
2247 (void) force;
2248}
2249#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2250
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002251/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002253 */
Hanno Becker3307b532017-12-27 21:37:21 +00002254
Hanno Beckera5a2b082019-05-15 14:03:01 +01002255#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002256/* This functions transforms a DTLS plaintext fragment and a record content
2257 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002258 *
2259 * struct {
2260 * opaque content[DTLSPlaintext.length];
2261 * ContentType real_type;
2262 * uint8 zeros[length_of_padding];
2263 * } DTLSInnerPlaintext;
2264 *
2265 * Input:
2266 * - `content`: The beginning of the buffer holding the
2267 * plaintext to be wrapped.
2268 * - `*content_size`: The length of the plaintext in Bytes.
2269 * - `max_len`: The number of Bytes available starting from
2270 * `content`. This must be `>= *content_size`.
2271 * - `rec_type`: The desired record content type.
2272 *
2273 * Output:
2274 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2275 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2276 *
2277 * Returns:
2278 * - `0` on success.
2279 * - A negative error code if `max_len` didn't offer enough space
2280 * for the expansion.
2281 */
2282static int ssl_cid_build_inner_plaintext( unsigned char *content,
2283 size_t *content_size,
2284 size_t remaining,
2285 uint8_t rec_type )
2286{
2287 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002288 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2289 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2290 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002291
2292 /* Write real content type */
2293 if( remaining == 0 )
2294 return( -1 );
2295 content[ len ] = rec_type;
2296 len++;
2297 remaining--;
2298
2299 if( remaining < pad )
2300 return( -1 );
2301 memset( content + len, 0, pad );
2302 len += pad;
2303 remaining -= pad;
2304
2305 *content_size = len;
2306 return( 0 );
2307}
2308
Hanno Becker7dc25772019-05-20 15:08:01 +01002309/* This function parses a DTLSInnerPlaintext structure.
2310 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002311static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2312 size_t *content_size,
2313 uint8_t *rec_type )
2314{
2315 size_t remaining = *content_size;
2316
2317 /* Determine length of padding by skipping zeroes from the back. */
2318 do
2319 {
2320 if( remaining == 0 )
2321 return( -1 );
2322 remaining--;
2323 } while( content[ remaining ] == 0 );
2324
2325 *content_size = remaining;
2326 *rec_type = content[ remaining ];
2327
2328 return( 0 );
2329}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002330#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002331
Hanno Becker99abf512019-05-20 14:50:53 +01002332/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002333 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002334static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002335 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002336 mbedtls_record *rec )
2337{
Hanno Becker99abf512019-05-20 14:50:53 +01002338 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002339 *
2340 * additional_data = seq_num + TLSCompressed.type +
2341 * TLSCompressed.version + TLSCompressed.length;
2342 *
Hanno Becker99abf512019-05-20 14:50:53 +01002343 * For the CID extension, this is extended as follows
2344 * (quoting draft-ietf-tls-dtls-connection-id-05,
2345 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002346 *
2347 * additional_data = seq_num + DTLSPlaintext.type +
2348 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002349 * cid +
2350 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002351 * length_of_DTLSInnerPlaintext;
2352 */
2353
Hanno Becker3307b532017-12-27 21:37:21 +00002354 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2355 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002356 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002357
Hanno Beckera5a2b082019-05-15 14:03:01 +01002358#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002359 if( rec->cid_len != 0 )
2360 {
2361 memcpy( add_data + 11, rec->cid, rec->cid_len );
2362 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2363 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2364 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2365 *add_data_len = 13 + 1 + rec->cid_len;
2366 }
2367 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002368#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002369 {
2370 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2371 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2372 *add_data_len = 13;
2373 }
Hanno Becker3307b532017-12-27 21:37:21 +00002374}
2375
Hanno Becker611a83b2018-01-03 14:27:32 +00002376int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2377 mbedtls_ssl_transform *transform,
2378 mbedtls_record *rec,
2379 int (*f_rng)(void *, unsigned char *, size_t),
2380 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002383 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002384 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002385 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002386 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002387 size_t post_avail;
2388
2389 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002390#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002391 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002392 ((void) ssl);
2393#endif
2394
2395 /* The PRNG is used for dynamic IV generation that's used
2396 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2397#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2398 ( defined(MBEDTLS_AES_C) || \
2399 defined(MBEDTLS_ARIA_C) || \
2400 defined(MBEDTLS_CAMELLIA_C) ) && \
2401 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2402 ((void) f_rng);
2403 ((void) p_rng);
2404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
Hanno Becker3307b532017-12-27 21:37:21 +00002408 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002409 {
Hanno Becker3307b532017-12-27 21:37:21 +00002410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2412 }
Hanno Becker505089d2019-05-01 09:45:57 +01002413 if( rec == NULL
2414 || rec->buf == NULL
2415 || rec->buf_len < rec->data_offset
2416 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002417#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002418 || rec->cid_len != 0
2419#endif
2420 )
Hanno Becker3307b532017-12-27 21:37:21 +00002421 {
2422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002424 }
2425
Hanno Becker3307b532017-12-27 21:37:21 +00002426 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002427 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002429 data, rec->data_len );
2430
2431 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2432
2433 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2434 {
2435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2436 (unsigned) rec->data_len,
2437 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2439 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002440
Hanno Beckera5a2b082019-05-15 14:03:01 +01002441#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002442 /*
2443 * Add CID information
2444 */
2445 rec->cid_len = transform->out_cid_len;
2446 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2447 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002448
2449 if( rec->cid_len != 0 )
2450 {
2451 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002452 * Wrap plaintext into DTLSInnerPlaintext structure.
2453 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002454 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002455 * Note that this changes `rec->data_len`, and hence
2456 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002457 */
2458 if( ssl_cid_build_inner_plaintext( data,
2459 &rec->data_len,
2460 post_avail,
2461 rec->type ) != 0 )
2462 {
2463 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2464 }
2465
2466 rec->type = MBEDTLS_SSL_MSG_CID;
2467 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002468#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002469
Hanno Becker92c930f2019-04-29 17:31:37 +01002470 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2471
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002473 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002475#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476 if( mode == MBEDTLS_MODE_STREAM ||
2477 ( mode == MBEDTLS_MODE_CBC
2478#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002479 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002480#endif
2481 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 {
Hanno Becker3307b532017-12-27 21:37:21 +00002483 if( post_avail < transform->maclen )
2484 {
2485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2486 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2487 }
2488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002490 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2491 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002492 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002493 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002494 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2495 data, rec->data_len, rec->ctr, rec->type, mac );
2496 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002497 }
2498 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2501 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002502 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2503 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002504 {
Hanno Becker992b6872017-11-09 18:57:39 +00002505 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2506
Hanno Beckere83efe62019-04-29 13:52:53 +01002507 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002508
Hanno Becker3307b532017-12-27 21:37:21 +00002509 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002510 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002511 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2512 data, rec->data_len );
2513 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2514 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2515
2516 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002517 }
2518 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002519#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2522 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002523 }
2524
Hanno Becker3307b532017-12-27 21:37:21 +00002525 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2526 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002527
Hanno Becker3307b532017-12-27 21:37:21 +00002528 rec->data_len += transform->maclen;
2529 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002530 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002531 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002532#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002534 /*
2535 * Encrypt
2536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2538 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002540 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002541 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002543 "including %d bytes of padding",
2544 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Hanno Becker3307b532017-12-27 21:37:21 +00002546 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2547 transform->iv_enc, transform->ivlen,
2548 data, rec->data_len,
2549 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002552 return( ret );
2553 }
2554
Hanno Becker3307b532017-12-27 21:37:21 +00002555 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
Paul Bakker68884e32013-01-07 18:20:04 +01002561 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002563
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002564#if defined(MBEDTLS_GCM_C) || \
2565 defined(MBEDTLS_CCM_C) || \
2566 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002568 mode == MBEDTLS_MODE_CCM ||
2569 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002570 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002571 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002572 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002573 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002574
Hanno Becker3307b532017-12-27 21:37:21 +00002575 /* Check that there's space for both the authentication tag
2576 * and the explicit IV before and after the record content. */
2577 if( post_avail < transform->taglen ||
2578 rec->data_offset < explicit_iv_len )
2579 {
2580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2581 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2582 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002583
Paul Bakker68884e32013-01-07 18:20:04 +01002584 /*
2585 * Generate IV
2586 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002587 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2588 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002589 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002590 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002591 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2592 explicit_iv_len );
2593 /* Prefix record content with explicit IV. */
2594 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002595 }
2596 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2597 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002598 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002599 unsigned char i;
2600
2601 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2602
2603 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002604 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002605 }
2606 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002607 {
2608 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2610 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002611 }
2612
Hanno Beckere83efe62019-04-29 13:52:53 +01002613 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002614
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002615 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2616 iv, transform->ivlen );
2617 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002618 data - explicit_iv_len, explicit_iv_len );
2619 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002620 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002622 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002623 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002624
Paul Bakker68884e32013-01-07 18:20:04 +01002625 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002626 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002627 */
Hanno Becker3307b532017-12-27 21:37:21 +00002628
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002630 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002631 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002632 data, rec->data_len, /* source */
2633 data, &rec->data_len, /* destination */
2634 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002637 return( ret );
2638 }
2639
Hanno Becker3307b532017-12-27 21:37:21 +00002640 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2641 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002642
Hanno Becker3307b532017-12-27 21:37:21 +00002643 rec->data_len += transform->taglen + explicit_iv_len;
2644 rec->data_offset -= explicit_iv_len;
2645 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002646 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002647 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002648 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2650#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002651 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002654 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002655 size_t padlen, i;
2656 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002657
Hanno Becker3307b532017-12-27 21:37:21 +00002658 /* Currently we're always using minimal padding
2659 * (up to 255 bytes would be allowed). */
2660 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2661 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 padlen = 0;
2663
Hanno Becker3307b532017-12-27 21:37:21 +00002664 /* Check there's enough space in the buffer for the padding. */
2665 if( post_avail < padlen + 1 )
2666 {
2667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2668 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2669 }
2670
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002672 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673
Hanno Becker3307b532017-12-27 21:37:21 +00002674 rec->data_len += padlen + 1;
2675 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002678 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002679 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2680 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002681 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002682 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2683 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002684 {
Hanno Becker3307b532017-12-27 21:37:21 +00002685 if( f_rng == NULL )
2686 {
2687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2689 }
2690
2691 if( rec->data_offset < transform->ivlen )
2692 {
2693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2694 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2695 }
2696
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002697 /*
2698 * Generate IV
2699 */
Hanno Becker3307b532017-12-27 21:37:21 +00002700 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002701 if( ret != 0 )
2702 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002703
Hanno Becker3307b532017-12-27 21:37:21 +00002704 memcpy( data - transform->ivlen, transform->iv_enc,
2705 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002706
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002707 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002711 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002712 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002713 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Hanno Becker3307b532017-12-27 21:37:21 +00002715 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2716 transform->iv_enc,
2717 transform->ivlen,
2718 data, rec->data_len,
2719 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002722 return( ret );
2723 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002724
Hanno Becker3307b532017-12-27 21:37:21 +00002725 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002726 {
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 );
Paul Bakkercca5b812013-08-31 17:40:26 +02002729 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002732 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2733 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002734 {
2735 /*
2736 * Save IV in SSL3 and TLS1
2737 */
Hanno Becker3307b532017-12-27 21:37:21 +00002738 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2739 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 }
Hanno Becker3307b532017-12-27 21:37:21 +00002741 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002742#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002743 {
2744 data -= transform->ivlen;
2745 rec->data_offset -= transform->ivlen;
2746 rec->data_len += transform->ivlen;
2747 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002750 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002751 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002752 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2753
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002754 /*
2755 * MAC(MAC_write_key, seq_num +
2756 * TLSCipherText.type +
2757 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002758 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002759 * IV + // except for TLS 1.0
2760 * ENC(content + padding + padding_length));
2761 */
Hanno Becker3307b532017-12-27 21:37:21 +00002762
2763 if( post_avail < transform->maclen)
2764 {
2765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2766 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2767 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002768
Hanno Beckere83efe62019-04-29 13:52:53 +01002769 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002772 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002773 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002774
Hanno Becker3307b532017-12-27 21:37:21 +00002775 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002776 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002777 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2778 data, rec->data_len );
2779 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2780 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002781
Hanno Becker3307b532017-12-27 21:37:21 +00002782 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002783
Hanno Becker3307b532017-12-27 21:37:21 +00002784 rec->data_len += transform->maclen;
2785 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002786 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002789 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002790 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002792 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2795 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002796 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002797
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002798 /* Make extra sure authentication was performed, exactly once */
2799 if( auth_done != 1 )
2800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2802 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002803 }
2804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
2807 return( 0 );
2808}
2809
Hanno Becker40478be2019-07-12 08:23:59 +01002810int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002811 mbedtls_ssl_transform *transform,
2812 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002813{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002814 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002816 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002817#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002818 size_t padlen = 0, correct = 1;
2819#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002821 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002822 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002823
Hanno Becker611a83b2018-01-03 14:27:32 +00002824#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002825 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002826 ((void) ssl);
2827#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002830 if( rec == NULL ||
2831 rec->buf == NULL ||
2832 rec->buf_len < rec->data_offset ||
2833 rec->buf_len - rec->data_offset < rec->data_len )
2834 {
2835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837 }
2838
Hanno Becker4c6876b2017-12-27 21:28:58 +00002839 data = rec->buf + rec->data_offset;
2840 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Hanno Beckera5a2b082019-05-15 14:03:01 +01002842#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002843 /*
2844 * Match record's CID with incoming CID.
2845 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002846 if( rec->cid_len != transform->in_cid_len ||
2847 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2848 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002849 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002850 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002851#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002853#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2854 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002855 {
2856 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002857 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2858 transform->iv_dec,
2859 transform->ivlen,
2860 data, rec->data_len,
2861 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002864 return( ret );
2865 }
2866
Hanno Becker4c6876b2017-12-27 21:28:58 +00002867 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2870 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002871 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 }
Paul Bakker68884e32013-01-07 18:20:04 +01002873 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002875#if defined(MBEDTLS_GCM_C) || \
2876 defined(MBEDTLS_CCM_C) || \
2877 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002879 mode == MBEDTLS_MODE_CCM ||
2880 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002881 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002882 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002883 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002884
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002885 /*
2886 * Prepare IV from explicit and implicit data.
2887 */
2888
2889 /* Check that there's enough space for the explicit IV
2890 * (at the beginning of the record) and the MAC (at the
2891 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002892 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002895 "+ taglen (%d)", rec->data_len,
2896 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002898 }
Paul Bakker68884e32013-01-07 18:20:04 +01002899
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002900#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002901 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2902 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002903 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002904
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002905 /* Fixed */
2906 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2907 /* Explicit */
2908 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002909 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002910 else
2911#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2912#if defined(MBEDTLS_CHACHAPOLY_C)
2913 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002914 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002915 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002916 unsigned char i;
2917
2918 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2919
2920 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002921 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002922 }
2923 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002924#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002925 {
2926 /* Reminder if we ever add an AEAD mode with a different size */
2927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2929 }
2930
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002931 /* Group changes to data, data_len, and add_data, because
2932 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002933 data += explicit_iv_len;
2934 rec->data_offset += explicit_iv_len;
2935 rec->data_len -= explicit_iv_len + transform->taglen;
2936
Hanno Beckere83efe62019-04-29 13:52:53 +01002937 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002938 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002939 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002940
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002941 /* Because of the check above, we know that there are
2942 * explicit_iv_len Bytes preceeding data, and taglen
2943 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002944 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002945 * mbedtls_cipher_auth_decrypt() below. */
2946
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002947 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002949 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002950
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002951 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002952 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002953 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002954 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2955 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002956 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002957 data, rec->data_len,
2958 data, &olen,
2959 data + rec->data_len,
2960 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2965 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002966
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002967 return( ret );
2968 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002969 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002970
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002971 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002972 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002976 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002978 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2980#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002981 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002984 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002985
Paul Bakker5121ce52009-01-03 21:22:43 +00002986 /*
Paul Bakker45829992013-01-03 14:52:21 +01002987 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002990 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2991 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002992 {
2993 /* The ciphertext is prefixed with the CBC IV. */
2994 minlen += transform->ivlen;
2995 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002996#endif
Paul Bakker45829992013-01-03 14:52:21 +01002997
Hanno Becker4c6876b2017-12-27 21:28:58 +00002998 /* Size considerations:
2999 *
3000 * - The CBC cipher text must not be empty and hence
3001 * at least of size transform->ivlen.
3002 *
3003 * Together with the potential IV-prefix, this explains
3004 * the first of the two checks below.
3005 *
3006 * - The record must contain a MAC, either in plain or
3007 * encrypted, depending on whether Encrypt-then-MAC
3008 * is used or not.
3009 * - If it is, the message contains the IV-prefix,
3010 * the CBC ciphertext, and the MAC.
3011 * - If it is not, the padded plaintext, and hence
3012 * the CBC ciphertext, has at least length maclen + 1
3013 * because there is at least the padding length byte.
3014 *
3015 * As the CBC ciphertext is not empty, both cases give the
3016 * lower bound minlen + maclen + 1 on the record size, which
3017 * we test for in the second check below.
3018 */
3019 if( rec->data_len < minlen + transform->ivlen ||
3020 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003023 "+ 1 ) ( + expl IV )", rec->data_len,
3024 transform->ivlen,
3025 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003027 }
3028
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003029 /*
3030 * Authenticate before decrypt if enabled
3031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003033 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003034 {
Hanno Becker992b6872017-11-09 18:57:39 +00003035 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003038
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003039 /* Update data_len in tandem with add_data.
3040 *
3041 * The subtraction is safe because of the previous check
3042 * data_len >= minlen + maclen + 1.
3043 *
3044 * Afterwards, we know that data + data_len is followed by at
3045 * least maclen Bytes, which justifies the call to
3046 * mbedtls_ssl_safer_memcmp() below.
3047 *
3048 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003049 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003050 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003051
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003052 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003053 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3054 add_data_len );
3055 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3056 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3058 data, rec->data_len );
3059 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3060 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003061
Hanno Becker4c6876b2017-12-27 21:28:58 +00003062 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3063 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003064 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003065 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003066
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003067 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003068 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3069 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003073 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003074 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003075 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003077
3078 /*
3079 * Check length sanity
3080 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003081
3082 /* We know from above that data_len > minlen >= 0,
3083 * so the following check in particular implies that
3084 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003085 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003088 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003090 }
3091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003093 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003094 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003095 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003096 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3097 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003098 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003099 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003100 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003101
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 data += transform->ivlen;
3103 rec->data_offset += transform->ivlen;
3104 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003105 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003107
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003108 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3109
Hanno Becker4c6876b2017-12-27 21:28:58 +00003110 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3111 transform->iv_dec, transform->ivlen,
3112 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003115 return( ret );
3116 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003117
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003118 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003119 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003123 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003126 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3127 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003128 {
3129 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003130 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3131 * records is equivalent to CBC decryption of the concatenation
3132 * of the records; in other words, IVs are maintained across
3133 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003134 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003135 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3136 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003137 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
Hanno Becker4c6876b2017-12-27 21:28:58 +00003140 /* Safe since data_len >= minlen + maclen + 1, so after having
3141 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003142 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3143 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003144 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003145
Hanno Becker4c6876b2017-12-27 21:28:58 +00003146 if( auth_done == 1 )
3147 {
3148 correct *= ( rec->data_len >= padlen + 1 );
3149 padlen *= ( rec->data_len >= padlen + 1 );
3150 }
3151 else
Paul Bakker45829992013-01-03 14:52:21 +01003152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003154 if( rec->data_len < transform->maclen + padlen + 1 )
3155 {
3156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3157 rec->data_len,
3158 transform->maclen,
3159 padlen + 1 ) );
3160 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003161#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003162
3163 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3164 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003165 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
Hanno Becker4c6876b2017-12-27 21:28:58 +00003167 padlen++;
3168
3169 /* Regardless of the validity of the padding,
3170 * we have data_len >= padlen here. */
3171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003173 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3174 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003176 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178#if defined(MBEDTLS_SSL_DEBUG_ALL)
3179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003180 "should be no more than %d",
3181 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003182#endif
Paul Bakker45829992013-01-03 14:52:21 +01003183 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 }
3185 }
3186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3188#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3189 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003190 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3191 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003193 /* The padding check involves a series of up to 256
3194 * consecutive memory reads at the end of the record
3195 * plaintext buffer. In order to hide the length and
3196 * validity of the padding, always perform exactly
3197 * `min(256,plaintext_len)` reads (but take into account
3198 * only the last `padlen` bytes for the padding check). */
3199 size_t pad_count = 0;
3200 size_t real_count = 0;
3201 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003202
Hanno Becker4c6876b2017-12-27 21:28:58 +00003203 /* Index of first padding byte; it has been ensured above
3204 * that the subtraction is safe. */
3205 size_t const padding_idx = rec->data_len - padlen;
3206 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3207 size_t const start_idx = rec->data_len - num_checks;
3208 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003209
Hanno Becker4c6876b2017-12-27 21:28:58 +00003210 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003211 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003212 real_count |= ( idx >= padding_idx );
3213 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003214 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003215 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003218 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003220#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003221 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003223 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3225 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3228 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003229 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003230
Hanno Becker4c6876b2017-12-27 21:28:58 +00003231 /* If the padding was found to be invalid, padlen == 0
3232 * and the subtraction is safe. If the padding was found valid,
3233 * padlen hasn't been changed and the previous assertion
3234 * data_len >= padlen still holds. */
3235 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003239 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003244
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003245#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003247 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003248#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003249
3250 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003251 * Authenticate if not done yet.
3252 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003254#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003255 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003256 {
Hanno Becker992b6872017-11-09 18:57:39 +00003257 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003258
Hanno Becker4c6876b2017-12-27 21:28:58 +00003259 /* If the initial value of padlen was such that
3260 * data_len < maclen + padlen + 1, then padlen
3261 * got reset to 1, and the initial check
3262 * data_len >= minlen + maclen + 1
3263 * guarantees that at this point we still
3264 * have at least data_len >= maclen.
3265 *
3266 * If the initial value of padlen was such that
3267 * data_len >= maclen + padlen + 1, then we have
3268 * subtracted either padlen + 1 (if the padding was correct)
3269 * or 0 (if the padding was incorrect) since then,
3270 * hence data_len >= maclen in any case.
3271 */
3272 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003273 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003276 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3277 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003278 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003279 ssl_mac( &transform->md_ctx_dec,
3280 transform->mac_dec,
3281 data, rec->data_len,
3282 rec->ctr, rec->type,
3283 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003284 }
3285 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3287#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3288 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003289 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3290 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003291 {
3292 /*
3293 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003294 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003295 *
3296 * Known timing attacks:
3297 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3298 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003299 * To compensate for different timings for the MAC calculation
3300 * depending on how much padding was removed (which is determined
3301 * by padlen), process extra_run more blocks through the hash
3302 * function.
3303 *
3304 * The formula in the paper is
3305 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3306 * where L1 is the size of the header plus the decrypted message
3307 * plus CBC padding and L2 is the size of the header plus the
3308 * decrypted message. This is for an underlying hash function
3309 * with 64-byte blocks.
3310 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3311 * correctly. We round down instead of up, so -56 is the correct
3312 * value for our calculations instead of -55.
3313 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003314 * Repeat the formula rather than defining a block_size variable.
3315 * This avoids requiring division by a variable at runtime
3316 * (which would be marginally less efficient and would require
3317 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003318 */
3319 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003320 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003321
3322 /*
3323 * The next two sizes are the minimum and maximum values of
3324 * in_msglen over all padlen values.
3325 *
3326 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003327 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003328 *
3329 * Note that max_len + maclen is never more than the buffer
3330 * length, as we previously did in_msglen -= maclen too.
3331 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003332 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003333 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3334
Hanno Becker4c6876b2017-12-27 21:28:58 +00003335 memset( tmp, 0, sizeof( tmp ) );
3336
3337 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003338 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003339#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3340 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003341 case MBEDTLS_MD_MD5:
3342 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003343 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003344 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003345 extra_run =
3346 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3347 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003348 break;
3349#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003350#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003351 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003352 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003353 extra_run =
3354 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3355 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003356 break;
3357#endif
3358 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3361 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003362
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003363 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003364
Hanno Beckere83efe62019-04-29 13:52:53 +01003365 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3366 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003367 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3368 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003369 /* Make sure we access everything even when padlen > 0. This
3370 * makes the synchronisation requirements for just-in-time
3371 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003372 ssl_read_memory( data + rec->data_len, padlen );
3373 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003374
3375 /* Call mbedtls_md_process at least once due to cache attacks
3376 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003377 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003378 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003379
Hanno Becker4c6876b2017-12-27 21:28:58 +00003380 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003381
3382 /* Make sure we access all the memory that could contain the MAC,
3383 * before we check it in the next code block. This makes the
3384 * synchronisation requirements for just-in-time Prime+Probe
3385 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003386 ssl_read_memory( data + min_len,
3387 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003388 }
3389 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3391 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003396
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003397#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003398 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3399 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003400#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
Hanno Becker4c6876b2017-12-27 21:28:58 +00003402 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3403 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405#if defined(MBEDTLS_SSL_DEBUG_ALL)
3406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003407#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003408 correct = 0;
3409 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003410 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003411 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003412
3413 /*
3414 * Finally check the correct flag
3415 */
3416 if( correct == 0 )
3417 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003418#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003419
3420 /* Make extra sure authentication was performed, exactly once */
3421 if( auth_done != 1 )
3422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3424 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003426
Hanno Beckera5a2b082019-05-15 14:03:01 +01003427#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003428 if( rec->cid_len != 0 )
3429 {
3430 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3431 &rec->type );
3432 if( ret != 0 )
3433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3434 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003435#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003438
3439 return( 0 );
3440}
3441
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003442#undef MAC_NONE
3443#undef MAC_PLAINTEXT
3444#undef MAC_CIPHERTEXT
3445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003447/*
3448 * Compression/decompression functions
3449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003451{
3452 int ret;
3453 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003454 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003455 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003456 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003459
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003460 if( len_pre == 0 )
3461 return( 0 );
3462
Paul Bakker2770fbd2012-07-03 13:30:23 +00003463 memcpy( msg_pre, ssl->out_msg, len_pre );
3464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003466 ssl->out_msglen ) );
3467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003469 ssl->out_msg, ssl->out_msglen );
3470
Paul Bakker48916f92012-09-16 19:57:18 +00003471 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3472 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3473 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003474 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003475
Paul Bakker48916f92012-09-16 19:57:18 +00003476 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477 if( ret != Z_OK )
3478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3480 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003481 }
3482
Angus Grattond8213d02016-05-25 20:56:48 +10003483 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003484 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003487 ssl->out_msglen ) );
3488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003490 ssl->out_msg, ssl->out_msglen );
3491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003493
3494 return( 0 );
3495}
3496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003498{
3499 int ret;
3500 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003501 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003503 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003506
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003507 if( len_pre == 0 )
3508 return( 0 );
3509
Paul Bakker2770fbd2012-07-03 13:30:23 +00003510 memcpy( msg_pre, ssl->in_msg, len_pre );
3511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003513 ssl->in_msglen ) );
3514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003516 ssl->in_msg, ssl->in_msglen );
3517
Paul Bakker48916f92012-09-16 19:57:18 +00003518 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3519 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3520 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003521 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003522 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003523
Paul Bakker48916f92012-09-16 19:57:18 +00003524 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003525 if( ret != Z_OK )
3526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3528 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003529 }
3530
Angus Grattond8213d02016-05-25 20:56:48 +10003531 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003532 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003535 ssl->in_msglen ) );
3536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003538 ssl->in_msg, ssl->in_msglen );
3539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003541
3542 return( 0 );
3543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3547static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549#if defined(MBEDTLS_SSL_PROTO_DTLS)
3550static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003551{
3552 /* If renegotiation is not enforced, retransmit until we would reach max
3553 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003554 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003555 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003556 uint32_t ratio =
3557 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3558 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003559 unsigned char doublings = 1;
3560
3561 while( ratio != 0 )
3562 {
3563 ++doublings;
3564 ratio >>= 1;
3565 }
3566
3567 if( ++ssl->renego_records_seen > doublings )
3568 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003570 return( 0 );
3571 }
3572 }
3573
3574 return( ssl_write_hello_request( ssl ) );
3575}
3576#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003578
Paul Bakker5121ce52009-01-03 21:22:43 +00003579/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003580 * Fill the input message buffer by appending data to it.
3581 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003582 *
3583 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3584 * available (from this read and/or a previous one). Otherwise, an error code
3585 * is returned (possibly EOF or WANT_READ).
3586 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003587 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3588 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3589 * since we always read a whole datagram at once.
3590 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003591 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003592 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003595{
Paul Bakker23986e52011-04-24 08:57:21 +00003596 int ret;
3597 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003600
Hanno Beckera58a8962019-06-13 16:11:15 +01003601 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3602 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003605 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003607 }
3608
Angus Grattond8213d02016-05-25 20:56:48 +10003609 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003613 }
3614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003616 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003618 uint32_t timeout;
3619
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003620 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003621 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3622 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003623 {
3624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3625 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3627 }
3628
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003629 /*
3630 * The point is, we need to always read a full datagram at once, so we
3631 * sometimes read more then requested, and handle the additional data.
3632 * It could be the rest of the current record (while fetching the
3633 * header) and/or some other records in the same datagram.
3634 */
3635
3636 /*
3637 * Move to the next record in the already read datagram if applicable
3638 */
3639 if( ssl->next_record_offset != 0 )
3640 {
3641 if( ssl->in_left < ssl->next_record_offset )
3642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3644 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003645 }
3646
3647 ssl->in_left -= ssl->next_record_offset;
3648
3649 if( ssl->in_left != 0 )
3650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003652 ssl->next_record_offset ) );
3653 memmove( ssl->in_hdr,
3654 ssl->in_hdr + ssl->next_record_offset,
3655 ssl->in_left );
3656 }
3657
3658 ssl->next_record_offset = 0;
3659 }
3660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003663
3664 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003665 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003666 */
3667 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003670 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003671 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003672
3673 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003674 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003675 * are not at the beginning of a new record, the caller did something
3676 * wrong.
3677 */
3678 if( ssl->in_left != 0 )
3679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003682 }
3683
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003684 /*
3685 * Don't even try to read if time's out already.
3686 * This avoids by-passing the timer when repeatedly receiving messages
3687 * that will end up being dropped.
3688 */
3689 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003690 {
3691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003692 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003693 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003694 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003695 {
Angus Grattond8213d02016-05-25 20:56:48 +10003696 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003699 timeout = ssl->handshake->retransmit_timeout;
3700 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003701 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003704
Hanno Beckera58a8962019-06-13 16:11:15 +01003705 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3706 {
3707 ret = mbedtls_ssl_get_recv_timeout( ssl )
3708 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3709 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003710 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003711 {
3712 ret = mbedtls_ssl_get_recv( ssl )
3713 ( ssl->p_bio, ssl->in_hdr, len );
3714 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003717
3718 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003720 }
3721
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003722 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003725 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003728 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003729 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003732 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003733 }
3734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003738 return( ret );
3739 }
3740
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003741 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003742 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003744 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3745 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003747 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003748 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003750 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003751 return( ret );
3752 }
3753
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003754 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003755 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003757 }
3758
Paul Bakker5121ce52009-01-03 21:22:43 +00003759 if( ret < 0 )
3760 return( ret );
3761
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003762 ssl->in_left = ret;
3763 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003764 MBEDTLS_SSL_TRANSPORT_ELSE
3765#endif /* MBEDTLS_SSL_PROTO_DTLS */
3766#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003769 ssl->in_left, nb_want ) );
3770
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003771 while( ssl->in_left < nb_want )
3772 {
3773 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003774
3775 if( ssl_check_timer( ssl ) != 0 )
3776 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3777 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003778 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003779 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003780 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003781 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3782 ssl->in_hdr + ssl->in_left, len,
3783 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003784 }
3785 else
3786 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003787 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003788 ssl->in_hdr + ssl->in_left, len );
3789 }
3790 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003793 ssl->in_left, nb_want ) );
3794 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003795
3796 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003798
3799 if( ret < 0 )
3800 return( ret );
3801
mohammad160352aecb92018-03-28 23:41:40 -07003802 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003803 {
Darryl Green11999bb2018-03-13 15:22:58 +00003804 MBEDTLS_SSL_DEBUG_MSG( 1,
3805 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003806 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003807 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3808 }
3809
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003810 ssl->in_left += ret;
3811 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003813#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003816
3817 return( 0 );
3818}
3819
3820/*
3821 * Flush any data not yet written
3822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003824{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003825 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003826 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003829
Hanno Beckera58a8962019-06-13 16:11:15 +01003830 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003833 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003835 }
3836
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003837 /* Avoid incrementing counter if data is flushed */
3838 if( ssl->out_left == 0 )
3839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003841 return( 0 );
3842 }
3843
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 while( ssl->out_left > 0 )
3845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003847 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
Hanno Becker2b1e3542018-08-06 11:19:13 +01003849 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003850 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003853
3854 if( ret <= 0 )
3855 return( ret );
3856
mohammad160352aecb92018-03-28 23:41:40 -07003857 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003858 {
Darryl Green11999bb2018-03-13 15:22:58 +00003859 MBEDTLS_SSL_DEBUG_MSG( 1,
3860 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003861 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003862 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3863 }
3864
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 ssl->out_left -= ret;
3866 }
3867
Hanno Becker2b1e3542018-08-06 11:19:13 +01003868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003869 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003870 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003871 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003872 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003873 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003874#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003875#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003876 {
3877 ssl->out_hdr = ssl->out_buf + 8;
3878 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003879#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003880 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003883
3884 return( 0 );
3885}
3886
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003887/*
3888 * Functions to handle the DTLS retransmission state machine
3889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003891/*
3892 * Append current handshake message to current outgoing flight
3893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003895{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3898 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3899 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003900
3901 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003902 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003903 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003906 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003907 }
3908
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003909 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003910 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003913 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003914 }
3915
3916 /* Copy current handshake message with headers */
3917 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3918 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003919 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003920 msg->next = NULL;
3921
3922 /* Append to the current flight */
3923 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003924 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003925 else
3926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928 while( cur->next != NULL )
3929 cur = cur->next;
3930 cur->next = msg;
3931 }
3932
Hanno Becker3b235902018-08-06 09:54:53 +01003933 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934 return( 0 );
3935}
3936
3937/*
3938 * Free the current flight of handshake messages
3939 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003941{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 mbedtls_ssl_flight_item *cur = flight;
3943 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003944
3945 while( cur != NULL )
3946 {
3947 next = cur->next;
3948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 mbedtls_free( cur->p );
3950 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003951
3952 cur = next;
3953 }
3954}
3955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3957static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003958#endif
3959
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003960/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003961 * Swap transform_out and out_ctr with the alternative ones
3962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003966 unsigned char tmp_out_ctr[8];
3967
3968 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003971 return;
3972 }
3973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003975
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003976 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003977 tmp_transform = ssl->transform_out;
3978 ssl->transform_out = ssl->handshake->alt_transform_out;
3979 ssl->handshake->alt_transform_out = tmp_transform;
3980
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003981 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003982 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3983 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003984 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003985
3986 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003987 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3990 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3995 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003996 }
3997 }
3998#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003999}
4000
4001/*
4002 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004003 */
4004int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4005{
4006 int ret = 0;
4007
4008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4009
4010 ret = mbedtls_ssl_flight_transmit( ssl );
4011
4012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4013
4014 return( ret );
4015}
4016
4017/*
4018 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004019 *
4020 * Need to remember the current message in case flush_output returns
4021 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004022 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004023 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004024int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004025{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004026 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004030 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004032
4033 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004034 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004035 ssl_swap_epochs( ssl );
4036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004038 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004039
4040 while( ssl->handshake->cur_msg != NULL )
4041 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004042 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004043 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004044
Hanno Beckere1dcb032018-08-17 16:47:58 +01004045 int const is_finished =
4046 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4047 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4048
Hanno Becker04da1892018-08-14 13:22:10 +01004049 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4050 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4051
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004052 /* Swap epochs before sending Finished: we can't do it after
4053 * sending ChangeCipherSpec, in case write returns WANT_READ.
4054 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004055 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004056 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004058 ssl_swap_epochs( ssl );
4059 }
4060
Hanno Becker67bc7c32018-08-06 11:33:50 +01004061 ret = ssl_get_remaining_payload_in_datagram( ssl );
4062 if( ret < 0 )
4063 return( ret );
4064 max_frag_len = (size_t) ret;
4065
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004066 /* CCS is copied as is, while HS messages may need fragmentation */
4067 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4068 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004069 if( max_frag_len == 0 )
4070 {
4071 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4072 return( ret );
4073
4074 continue;
4075 }
4076
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004077 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004078 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004079 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004080
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004081 /* Update position inside current message */
4082 ssl->handshake->cur_msg_p += cur->len;
4083 }
4084 else
4085 {
4086 const unsigned char * const p = ssl->handshake->cur_msg_p;
4087 const size_t hs_len = cur->len - 12;
4088 const size_t frag_off = p - ( cur->p + 12 );
4089 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004090 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004091
Hanno Beckere1dcb032018-08-17 16:47:58 +01004092 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004093 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004094 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004095 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004096
Hanno Becker67bc7c32018-08-06 11:33:50 +01004097 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4098 return( ret );
4099
4100 continue;
4101 }
4102 max_hs_frag_len = max_frag_len - 12;
4103
4104 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4105 max_hs_frag_len : rem_len;
4106
4107 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004108 {
4109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004110 (unsigned) cur_hs_frag_len,
4111 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004112 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004113
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004114 /* Messages are stored with handshake headers as if not fragmented,
4115 * copy beginning of headers then fill fragmentation fields.
4116 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4117 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004118
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004119 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4120 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4121 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4122
Hanno Becker67bc7c32018-08-06 11:33:50 +01004123 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4124 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4125 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004126
4127 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4128
Hanno Becker3f7b9732018-08-28 09:53:25 +01004129 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004130 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4131 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004132 ssl->out_msgtype = cur->type;
4133
4134 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004135 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004136 }
4137
4138 /* If done with the current message move to the next one if any */
4139 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4140 {
4141 if( cur->next != NULL )
4142 {
4143 ssl->handshake->cur_msg = cur->next;
4144 ssl->handshake->cur_msg_p = cur->next->p + 12;
4145 }
4146 else
4147 {
4148 ssl->handshake->cur_msg = NULL;
4149 ssl->handshake->cur_msg_p = NULL;
4150 }
4151 }
4152
4153 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004154 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004157 return( ret );
4158 }
4159 }
4160
Hanno Becker67bc7c32018-08-06 11:33:50 +01004161 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4162 return( ret );
4163
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004164 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4166 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004167 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004169 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004170 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4171 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004172
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004174
4175 return( 0 );
4176}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004177
4178/*
4179 * To be called when the last message of an incoming flight is received.
4180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004182{
4183 /* We won't need to resend that one any more */
4184 ssl_flight_free( ssl->handshake->flight );
4185 ssl->handshake->flight = NULL;
4186 ssl->handshake->cur_msg = NULL;
4187
4188 /* The next incoming flight will start with this msg_seq */
4189 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4190
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004191 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004192 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004193
Hanno Becker0271f962018-08-16 13:23:47 +01004194 /* Clear future message buffering structure. */
4195 ssl_buffering_free( ssl );
4196
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004197 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004198 ssl_set_timer( ssl, 0 );
4199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4201 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004203 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004204 }
4205 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004207}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004208
4209/*
4210 * To be called when the last message of an outgoing flight is send.
4211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004213{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004214 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004215 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4218 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004221 }
4222 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004226
Paul Bakker5121ce52009-01-03 21:22:43 +00004227/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004228 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004230
4231/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004232 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004233 *
4234 * - fill in handshake headers
4235 * - update handshake checksum
4236 * - DTLS: save message for resending
4237 * - then pass to the record layer
4238 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004239 * DTLS: except for HelloRequest, messages are only queued, and will only be
4240 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004241 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004242 * Inputs:
4243 * - ssl->out_msglen: 4 + actual handshake message len
4244 * (4 is the size of handshake headers for TLS)
4245 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4246 * - ssl->out_msg + 4: the handshake message body
4247 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004248 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004249 * - ssl->out_msglen: the length of the record contents
4250 * (including handshake headers but excluding record headers)
4251 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004252 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004253int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004254{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004255 int ret;
4256 const size_t hs_len = ssl->out_msglen - 4;
4257 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004258
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4260
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004261 /*
4262 * Sanity checks
4263 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004264 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004265 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4266 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004267 /* In SSLv3, the client might send a NoCertificate alert. */
4268#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004269 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004270 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004271 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4272 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004273#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4274 {
4275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4277 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004279
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004280 /* Whenever we send anything different from a
4281 * HelloRequest we should be in a handshake - double check. */
4282 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4283 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004284 ssl->handshake == NULL )
4285 {
4286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004291 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004292 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004294 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4296 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004297 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004298#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004299
Hanno Beckerb50a2532018-08-06 11:52:54 +01004300 /* Double-check that we did not exceed the bounds
4301 * of the outgoing record buffer.
4302 * This should never fail as the various message
4303 * writing functions must obey the bounds of the
4304 * outgoing record buffer, but better be safe.
4305 *
4306 * Note: We deliberately do not check for the MTU or MFL here.
4307 */
4308 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4309 {
4310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4311 "size %u, maximum %u",
4312 (unsigned) ssl->out_msglen,
4313 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4315 }
4316
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004317 /*
4318 * Fill handshake headers
4319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004321 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004322 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4323 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4324 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004326 /*
4327 * DTLS has additional fields in the Handshake layer,
4328 * between the length field and the actual payload:
4329 * uint16 message_seq;
4330 * uint24 fragment_offset;
4331 * uint24 fragment_length;
4332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004334 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004335 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004336 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004337 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004338 {
4339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4340 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004341 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004342 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4344 }
4345
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004346 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004347 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004348
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004349 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004350 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004351 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004352 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4353 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4354 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004355 }
4356 else
4357 {
4358 ssl->out_msg[4] = 0;
4359 ssl->out_msg[5] = 0;
4360 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004361
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004362 /* Handshake hashes are computed without fragmentation,
4363 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004364 memset( ssl->out_msg + 6, 0x00, 3 );
4365 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004367#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004368
Hanno Becker0207e532018-08-28 10:28:28 +01004369 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004370 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004371 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 }
4373
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004374 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004376 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004377 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4378 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004379 {
4380 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004383 return( ret );
4384 }
4385 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004386 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004387#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004388 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004389 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004390 {
4391 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4392 return( ret );
4393 }
4394 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004395
4396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4397
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004398 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004399}
4400
4401/*
4402 * Record layer functions
4403 */
4404
4405/*
4406 * Write current record.
4407 *
4408 * Uses:
4409 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4410 * - ssl->out_msglen: length of the record content (excl headers)
4411 * - ssl->out_msg: record content
4412 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004413int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004414{
4415 int ret, done = 0;
4416 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004417 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004418
4419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004421#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004422 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004424 {
4425 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004428 return( ret );
4429 }
4430
4431 len = ssl->out_msglen;
4432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004433#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4436 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004440 ret = mbedtls_ssl_hw_record_write( ssl );
4441 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4444 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004445 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004446
4447 if( ret == 0 )
4448 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004449 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004451 if( !done )
4452 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004453 unsigned i;
4454 size_t protected_record_size;
4455
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004456 /* Skip writing the record content type to after the encryption,
4457 * as it may change when using the CID extension. */
4458
Hanno Becker2881d802019-05-22 14:44:53 +01004459 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4460 mbedtls_ssl_get_minor_ver( ssl ),
4461 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004462
Hanno Becker19859472018-08-06 09:40:20 +01004463 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004464 ssl->out_len[0] = (unsigned char)( len >> 8 );
4465 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004466
Paul Bakker48916f92012-09-16 19:57:18 +00004467 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004468 {
Hanno Becker3307b532017-12-27 21:37:21 +00004469 mbedtls_record rec;
4470
4471 rec.buf = ssl->out_iv;
4472 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4473 ( ssl->out_iv - ssl->out_buf );
4474 rec.data_len = ssl->out_msglen;
4475 rec.data_offset = ssl->out_msg - rec.buf;
4476
4477 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004478 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4479 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004480 ssl->conf->transport, rec.ver );
4481 rec.type = ssl->out_msgtype;
4482
Hanno Beckera5a2b082019-05-15 14:03:01 +01004483#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004484 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004485 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004486#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004487
Hanno Becker611a83b2018-01-03 14:27:32 +00004488 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004489 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004490 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004493 return( ret );
4494 }
4495
Hanno Becker3307b532017-12-27 21:37:21 +00004496 if( rec.data_offset != 0 )
4497 {
4498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4499 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4500 }
4501
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004502 /* Update the record content type and CID. */
4503 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004504#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004505 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004506#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004507 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004508 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4509 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004510 }
4511
Hanno Becker43395762019-05-03 14:46:38 +01004512 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004513
4514#if defined(MBEDTLS_SSL_PROTO_DTLS)
4515 /* In case of DTLS, double-check that we don't exceed
4516 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004517 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004518 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004519 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004520 if( ret < 0 )
4521 return( ret );
4522
4523 if( protected_record_size > (size_t) ret )
4524 {
4525 /* Should never happen */
4526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4527 }
4528 }
4529#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004530
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004531 /* Now write the potentially updated record content type. */
4532 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004535 "version = [%d:%d], msglen = %d",
4536 ssl->out_hdr[0], ssl->out_hdr[1],
4537 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004540 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004541
4542 ssl->out_left += protected_record_size;
4543 ssl->out_hdr += protected_record_size;
4544 ssl_update_out_pointers( ssl, ssl->transform_out );
4545
Hanno Becker04484622018-08-06 09:49:38 +01004546 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4547 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4548 break;
4549
4550 /* The loop goes to its end iff the counter is wrapping */
4551 if( i == ssl_ep_len( ssl ) )
4552 {
4553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4554 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004556 }
4557
Hanno Becker67bc7c32018-08-06 11:33:50 +01004558#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004559 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004560 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004561 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004562 size_t remaining;
4563 ret = ssl_get_remaining_payload_in_datagram( ssl );
4564 if( ret < 0 )
4565 {
4566 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4567 ret );
4568 return( ret );
4569 }
4570
4571 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004572 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004573 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004574 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004575 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004576 else
4577 {
Hanno Becker513815a2018-08-20 11:56:09 +01004578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004579 }
4580 }
4581#endif /* MBEDTLS_SSL_PROTO_DTLS */
4582
4583 if( ( flush == SSL_FORCE_FLUSH ) &&
4584 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587 return( ret );
4588 }
4589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004591
4592 return( 0 );
4593}
4594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004596
4597static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4598{
4599 if( ssl->in_msglen < ssl->in_hslen ||
4600 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4601 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4602 {
4603 return( 1 );
4604 }
4605 return( 0 );
4606}
Hanno Becker44650b72018-08-16 12:51:11 +01004607
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004608static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004609{
4610 return( ( ssl->in_msg[9] << 16 ) |
4611 ( ssl->in_msg[10] << 8 ) |
4612 ssl->in_msg[11] );
4613}
4614
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004615static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004616{
4617 return( ( ssl->in_msg[6] << 16 ) |
4618 ( ssl->in_msg[7] << 8 ) |
4619 ssl->in_msg[8] );
4620}
4621
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004622static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004623{
4624 uint32_t msg_len, frag_off, frag_len;
4625
4626 msg_len = ssl_get_hs_total_len( ssl );
4627 frag_off = ssl_get_hs_frag_off( ssl );
4628 frag_len = ssl_get_hs_frag_len( ssl );
4629
4630 if( frag_off > msg_len )
4631 return( -1 );
4632
4633 if( frag_len > msg_len - frag_off )
4634 return( -1 );
4635
4636 if( frag_len + 12 > ssl->in_msglen )
4637 return( -1 );
4638
4639 return( 0 );
4640}
4641
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004642/*
4643 * Mark bits in bitmask (used for DTLS HS reassembly)
4644 */
4645static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4646{
4647 unsigned int start_bits, end_bits;
4648
4649 start_bits = 8 - ( offset % 8 );
4650 if( start_bits != 8 )
4651 {
4652 size_t first_byte_idx = offset / 8;
4653
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004654 /* Special case */
4655 if( len <= start_bits )
4656 {
4657 for( ; len != 0; len-- )
4658 mask[first_byte_idx] |= 1 << ( start_bits - len );
4659
4660 /* Avoid potential issues with offset or len becoming invalid */
4661 return;
4662 }
4663
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004664 offset += start_bits; /* Now offset % 8 == 0 */
4665 len -= start_bits;
4666
4667 for( ; start_bits != 0; start_bits-- )
4668 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4669 }
4670
4671 end_bits = len % 8;
4672 if( end_bits != 0 )
4673 {
4674 size_t last_byte_idx = ( offset + len ) / 8;
4675
4676 len -= end_bits; /* Now len % 8 == 0 */
4677
4678 for( ; end_bits != 0; end_bits-- )
4679 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4680 }
4681
4682 memset( mask + offset / 8, 0xFF, len / 8 );
4683}
4684
4685/*
4686 * Check that bitmask is full
4687 */
4688static int ssl_bitmask_check( unsigned char *mask, size_t len )
4689{
4690 size_t i;
4691
4692 for( i = 0; i < len / 8; i++ )
4693 if( mask[i] != 0xFF )
4694 return( -1 );
4695
4696 for( i = 0; i < len % 8; i++ )
4697 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4698 return( -1 );
4699
4700 return( 0 );
4701}
4702
Hanno Becker56e205e2018-08-16 09:06:12 +01004703/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004704static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004705 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004706{
Hanno Becker56e205e2018-08-16 09:06:12 +01004707 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004708
Hanno Becker56e205e2018-08-16 09:06:12 +01004709 alloc_len = 12; /* Handshake header */
4710 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004711
Hanno Beckerd07df862018-08-16 09:14:58 +01004712 if( add_bitmap )
4713 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004714
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004715 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004716}
Hanno Becker56e205e2018-08-16 09:06:12 +01004717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004719
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004720static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004721{
4722 return( ( ssl->in_msg[1] << 16 ) |
4723 ( ssl->in_msg[2] << 8 ) |
4724 ssl->in_msg[3] );
4725}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004726
Simon Butcher99000142016-10-13 17:21:01 +01004727int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004728{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004732 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004733 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004734 }
4735
Hanno Becker12555c62018-08-16 12:47:53 +01004736 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004739 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004740 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004743 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004744 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004745 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004746 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004747
Hanno Becker44650b72018-08-16 12:51:11 +01004748 if( ssl_check_hs_header( ssl ) != 0 )
4749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4751 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4752 }
4753
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004754 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004755 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4756 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4757 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4758 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004759 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004760 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4761 {
4762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4763 recv_msg_seq,
4764 ssl->handshake->in_msg_seq ) );
4765 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4766 }
4767
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004768 /* Retransmit only on last message from previous flight, to avoid
4769 * too many retransmissions.
4770 * Besides, No sane server ever retransmits HelloVerifyRequest */
4771 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004775 "message_seq = %d, start_of_flight = %d",
4776 recv_msg_seq,
4777 ssl->handshake->in_flight_start_seq ) );
4778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004782 return( ret );
4783 }
4784 }
4785 else
4786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004788 "message_seq = %d, expected = %d",
4789 recv_msg_seq,
4790 ssl->handshake->in_msg_seq ) );
4791 }
4792
Hanno Becker90333da2017-10-10 11:27:13 +01004793 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004794 }
4795 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004796
Hanno Becker6d97ef52018-08-16 13:09:04 +01004797 /* Message reassembly is handled alongside buffering of future
4798 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004799 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004800 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004801 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004804 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004805 }
4806 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004807 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004809#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004810 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004811 /* With TLS we don't handle fragmentation (for now) */
4812 if( ssl->in_msglen < ssl->in_hslen )
4813 {
4814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4815 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4816 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004817 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004818#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004819
Simon Butcher99000142016-10-13 17:21:01 +01004820 return( 0 );
4821}
4822
4823void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4824{
Hanno Becker0271f962018-08-16 13:23:47 +01004825 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004826
Hanno Becker0271f962018-08-16 13:23:47 +01004827 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004828 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004829
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004830 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004832 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004833 ssl->handshake != NULL )
4834 {
Hanno Becker0271f962018-08-16 13:23:47 +01004835 unsigned offset;
4836 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004837
Hanno Becker0271f962018-08-16 13:23:47 +01004838 /* Increment handshake sequence number */
4839 hs->in_msg_seq++;
4840
4841 /*
4842 * Clear up handshake buffering and reassembly structure.
4843 */
4844
4845 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004846 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004847
4848 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004849 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4850 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004851 offset++, hs_buf++ )
4852 {
4853 *hs_buf = *(hs_buf + 1);
4854 }
4855
4856 /* Create a fresh last entry */
4857 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004858 }
4859#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004860}
4861
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004862/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004863 * DTLS anti-replay: RFC 6347 4.1.2.6
4864 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004865 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4866 * Bit n is set iff record number in_window_top - n has been seen.
4867 *
4868 * Usually, in_window_top is the last record number seen and the lsb of
4869 * in_window is set. The only exception is the initial state (record number 0
4870 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4873static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004874{
4875 ssl->in_window_top = 0;
4876 ssl->in_window = 0;
4877}
4878
4879static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4880{
4881 return( ( (uint64_t) buf[0] << 40 ) |
4882 ( (uint64_t) buf[1] << 32 ) |
4883 ( (uint64_t) buf[2] << 24 ) |
4884 ( (uint64_t) buf[3] << 16 ) |
4885 ( (uint64_t) buf[4] << 8 ) |
4886 ( (uint64_t) buf[5] ) );
4887}
4888
4889/*
4890 * Return 0 if sequence number is acceptable, -1 otherwise
4891 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004892int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004893{
4894 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4895 uint64_t bit;
4896
Hanno Becker7f376f42019-06-12 16:20:48 +01004897 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4898 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4899 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004900 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004901 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004902
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004903 if( rec_seqnum > ssl->in_window_top )
4904 return( 0 );
4905
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004906 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004907
4908 if( bit >= 64 )
4909 return( -1 );
4910
4911 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4912 return( -1 );
4913
4914 return( 0 );
4915}
4916
4917/*
4918 * Update replay window on new validated record
4919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004920void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004921{
4922 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4923
Hanno Becker7f376f42019-06-12 16:20:48 +01004924 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4925 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4926 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004927 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004928 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004929
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004930 if( rec_seqnum > ssl->in_window_top )
4931 {
4932 /* Update window_top and the contents of the window */
4933 uint64_t shift = rec_seqnum - ssl->in_window_top;
4934
4935 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004936 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004937 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004938 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004939 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004940 ssl->in_window |= 1;
4941 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004942
4943 ssl->in_window_top = rec_seqnum;
4944 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004945 else
4946 {
4947 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004948 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004949
4950 if( bit < 64 ) /* Always true, but be extra sure */
4951 ssl->in_window |= (uint64_t) 1 << bit;
4952 }
4953}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004955
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004956#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004957/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004958static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4959
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004960/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004961 * Without any SSL context, check if a datagram looks like a ClientHello with
4962 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004963 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004964 *
4965 * - if cookie is valid, return 0
4966 * - if ClientHello looks superficially valid but cookie is not,
4967 * fill obuf and set olen, then
4968 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4969 * - otherwise return a specific error code
4970 */
4971static int ssl_check_dtls_clihlo_cookie(
4972 mbedtls_ssl_cookie_write_t *f_cookie_write,
4973 mbedtls_ssl_cookie_check_t *f_cookie_check,
4974 void *p_cookie,
4975 const unsigned char *cli_id, size_t cli_id_len,
4976 const unsigned char *in, size_t in_len,
4977 unsigned char *obuf, size_t buf_len, size_t *olen )
4978{
4979 size_t sid_len, cookie_len;
4980 unsigned char *p;
4981
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004982 /*
4983 * Structure of ClientHello with record and handshake headers,
4984 * and expected values. We don't need to check a lot, more checks will be
4985 * done when actually parsing the ClientHello - skipping those checks
4986 * avoids code duplication and does not make cookie forging any easier.
4987 *
4988 * 0-0 ContentType type; copied, must be handshake
4989 * 1-2 ProtocolVersion version; copied
4990 * 3-4 uint16 epoch; copied, must be 0
4991 * 5-10 uint48 sequence_number; copied
4992 * 11-12 uint16 length; (ignored)
4993 *
4994 * 13-13 HandshakeType msg_type; (ignored)
4995 * 14-16 uint24 length; (ignored)
4996 * 17-18 uint16 message_seq; copied
4997 * 19-21 uint24 fragment_offset; copied, must be 0
4998 * 22-24 uint24 fragment_length; (ignored)
4999 *
5000 * 25-26 ProtocolVersion client_version; (ignored)
5001 * 27-58 Random random; (ignored)
5002 * 59-xx SessionID session_id; 1 byte len + sid_len content
5003 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5004 * ...
5005 *
5006 * Minimum length is 61 bytes.
5007 */
5008 if( in_len < 61 ||
5009 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5010 in[3] != 0 || in[4] != 0 ||
5011 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5012 {
5013 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5014 }
5015
5016 sid_len = in[59];
5017 if( sid_len > in_len - 61 )
5018 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5019
5020 cookie_len = in[60 + sid_len];
5021 if( cookie_len > in_len - 60 )
5022 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5023
5024 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5025 cli_id, cli_id_len ) == 0 )
5026 {
5027 /* Valid cookie */
5028 return( 0 );
5029 }
5030
5031 /*
5032 * If we get here, we've got an invalid cookie, let's prepare HVR.
5033 *
5034 * 0-0 ContentType type; copied
5035 * 1-2 ProtocolVersion version; copied
5036 * 3-4 uint16 epoch; copied
5037 * 5-10 uint48 sequence_number; copied
5038 * 11-12 uint16 length; olen - 13
5039 *
5040 * 13-13 HandshakeType msg_type; hello_verify_request
5041 * 14-16 uint24 length; olen - 25
5042 * 17-18 uint16 message_seq; copied
5043 * 19-21 uint24 fragment_offset; copied
5044 * 22-24 uint24 fragment_length; olen - 25
5045 *
5046 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5047 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5048 *
5049 * Minimum length is 28.
5050 */
5051 if( buf_len < 28 )
5052 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5053
5054 /* Copy most fields and adapt others */
5055 memcpy( obuf, in, 25 );
5056 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5057 obuf[25] = 0xfe;
5058 obuf[26] = 0xff;
5059
5060 /* Generate and write actual cookie */
5061 p = obuf + 28;
5062 if( f_cookie_write( p_cookie,
5063 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5064 {
5065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5066 }
5067
5068 *olen = p - obuf;
5069
5070 /* Go back and fill length fields */
5071 obuf[27] = (unsigned char)( *olen - 28 );
5072
5073 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5074 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5075 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5076
5077 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5078 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5079
5080 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5081}
5082
5083/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005084 * Handle possible client reconnect with the same UDP quadruplet
5085 * (RFC 6347 Section 4.2.8).
5086 *
5087 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5088 * that looks like a ClientHello.
5089 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005090 * - if the input looks like a ClientHello without cookies,
5091 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005092 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005093 * - if the input looks like a ClientHello with a valid cookie,
5094 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005095 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005096 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005097 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005098 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005099 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5100 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005101 */
5102static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5103{
5104 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005105 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005106
Hanno Becker87b56262019-07-10 14:37:41 +01005107 if( ssl->conf->f_cookie_write == NULL ||
5108 ssl->conf->f_cookie_check == NULL )
5109 {
5110 /* If we can't use cookies to verify reachability of the peer,
5111 * drop the record. */
5112 return( 0 );
5113 }
5114
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005115 ret = ssl_check_dtls_clihlo_cookie(
5116 ssl->conf->f_cookie_write,
5117 ssl->conf->f_cookie_check,
5118 ssl->conf->p_cookie,
5119 ssl->cli_id, ssl->cli_id_len,
5120 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005121 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005122
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005123 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5124
5125 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005126 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005127 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005128 * If the error is permanent we'll catch it later,
5129 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005130 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005131 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005132 }
5133
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005134 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005135 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005136 /* Got a valid cookie, partially reset context */
5137 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5138 {
5139 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5140 return( ret );
5141 }
5142
5143 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005144 }
5145
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005146 return( ret );
5147}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005148#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005149
Hanno Becker46483f12019-05-03 13:25:54 +01005150static int ssl_check_record_type( uint8_t record_type )
5151{
5152 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5153 record_type != MBEDTLS_SSL_MSG_ALERT &&
5154 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5155 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5156 {
5157 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5158 }
5159
5160 return( 0 );
5161}
5162
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005163/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005164 * ContentType type;
5165 * ProtocolVersion version;
5166 * uint16 epoch; // DTLS only
5167 * uint48 sequence_number; // DTLS only
5168 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005169 *
5170 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005171 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005172 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5173 *
5174 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005175 * 1. proceed with the record if this function returns 0
5176 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5177 * 3. return CLIENT_RECONNECT if this function return that value
5178 * 4. drop the whole datagram if this function returns anything else.
5179 * Point 2 is needed when the peer is resending, and we have already received
5180 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005181 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005182static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005183 unsigned char *buf,
5184 size_t len,
5185 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005186{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005187 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005188
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005189 size_t const rec_hdr_type_offset = 0;
5190 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005191
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005192 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5193 rec_hdr_type_len;
5194 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005195
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005196 size_t const rec_hdr_ctr_len = 8;
5197#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005198 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005199 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5200 rec_hdr_version_len;
5201
Hanno Beckera5a2b082019-05-15 14:03:01 +01005202#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005203 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5204 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005205 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005206#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5207#endif /* MBEDTLS_SSL_PROTO_DTLS */
5208
5209 size_t rec_hdr_len_offset; /* To be determined */
5210 size_t const rec_hdr_len_len = 2;
5211
5212 /*
5213 * Check minimum lengths for record header.
5214 */
5215
5216#if defined(MBEDTLS_SSL_PROTO_DTLS)
5217 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5218 {
5219 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5220 }
5221 MBEDTLS_SSL_TRANSPORT_ELSE
5222#endif /* MBEDTLS_SSL_PROTO_DTLS */
5223#if defined(MBEDTLS_SSL_PROTO_TLS)
5224 {
5225 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5226 }
5227#endif /* MBEDTLS_SSL_PROTO_DTLS */
5228
5229 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5230 {
5231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5232 (unsigned) len,
5233 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5234 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5235 }
5236
5237 /*
5238 * Parse and validate record content type
5239 */
5240
5241 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005242
5243 /* Check record content type */
5244#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5245 rec->cid_len = 0;
5246
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005247 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005248 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5249 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005250 {
5251 /* Shift pointers to account for record header including CID
5252 * struct {
5253 * ContentType special_type = tls12_cid;
5254 * ProtocolVersion version;
5255 * uint16 epoch;
5256 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005257 * opaque cid[cid_length]; // Additional field compared to
5258 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005259 * uint16 length;
5260 * opaque enc_content[DTLSCiphertext.length];
5261 * } DTLSCiphertext;
5262 */
5263
5264 /* So far, we only support static CID lengths
5265 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005266 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5267 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005268
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005269 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005270 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5272 (unsigned) len,
5273 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005274 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005275 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005276
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005277 /* configured CID len is guaranteed at most 255, see
5278 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5279 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005280 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005281 }
5282 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005283#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005284 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005285 if( ssl_check_record_type( rec->type ) )
5286 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5288 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005289 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5290 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005291 }
5292
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005293 /*
5294 * Parse and validate record version
5295 */
5296
Hanno Becker8061c6e2019-07-26 08:07:03 +01005297 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5298 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005299 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5300 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005301 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005302
Hanno Becker2881d802019-05-22 14:44:53 +01005303 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5306 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005307 }
5308
Hanno Beckere965bd32019-06-12 14:04:34 +01005309 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5312 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005313 }
5314
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005315 /*
5316 * Parse/Copy record sequence number.
5317 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005318
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005319#if defined(MBEDTLS_SSL_PROTO_DTLS)
5320 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5321 {
5322 /* Copy explicit record sequence number from input buffer. */
5323 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5324 rec_hdr_ctr_len );
5325 }
5326 MBEDTLS_SSL_TRANSPORT_ELSE
5327#endif /* MBEDTLS_SSL_PROTO_DTLS */
5328#if defined(MBEDTLS_SSL_PROTO_TLS)
5329 {
5330 /* Copy implicit record sequence number from SSL context structure. */
5331 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5332 }
5333#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005334
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005335 /*
5336 * Parse record length.
5337 */
5338
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005339 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005340 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5341 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005342 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5343
Hanno Becker8b09b732019-05-08 12:03:28 +01005344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005345 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005346 rec->type,
5347 major_ver, minor_ver, rec->data_len ) );
5348
5349 rec->buf = buf;
5350 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005351
Hanno Beckerec014082019-07-26 08:20:27 +01005352 if( rec->data_len == 0 )
5353 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5354
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005355 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005356 * DTLS-related tests.
5357 * Check epoch before checking length constraint because
5358 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5359 * message gets duplicated before the corresponding Finished message,
5360 * the second ChangeCipherSpec should be discarded because it belongs
5361 * to an old epoch, but not because its length is shorter than
5362 * the minimum record length for packets using the new record transform.
5363 * Note that these two kinds of failures are handled differently,
5364 * as an unexpected record is silently skipped but an invalid
5365 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005366 */
5367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005368 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005369 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005370 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005371
Hanno Beckere0452772019-07-10 17:12:07 +01005372 /* Check that the datagram is large enough to contain a record
5373 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005374 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005375 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5377 (unsigned) len,
5378 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005379 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5380 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005381
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005382 /* Records from other, non-matching epochs are silently discarded.
5383 * (The case of same-port Client reconnects must be considered in
5384 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005385 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005386 {
5387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5388 "expected %d, received %d",
5389 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005390
5391 /* Records from the next epoch are considered for buffering
5392 * (concretely: early Finished messages). */
5393 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5394 {
5395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5396 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5397 }
5398
Hanno Becker87b56262019-07-10 14:37:41 +01005399 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005400 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005401#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005402 /* For records from the correct epoch, check whether their
5403 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005404 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005405 {
5406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5407 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5408 }
5409#endif
5410 }
5411#endif /* MBEDTLS_SSL_PROTO_DTLS */
5412
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005413 return( 0 );
5414}
Paul Bakker5121ce52009-01-03 21:22:43 +00005415
Hanno Becker87b56262019-07-10 14:37:41 +01005416
5417#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5418static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5419{
5420 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5421
5422 /*
5423 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5424 * access the first byte of record content (handshake type), as we
5425 * have an active transform (possibly iv_len != 0), so use the
5426 * fact that the record header len is 13 instead.
5427 */
5428 if( rec_epoch == 0 &&
5429 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5430 MBEDTLS_SSL_IS_SERVER &&
5431 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5432 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5433 ssl->in_left > 13 &&
5434 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5435 {
5436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5437 "from the same port" ) );
5438 return( ssl_handle_possible_reconnect( ssl ) );
5439 }
5440
5441 return( 0 );
5442}
5443#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5444
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445/*
5446 * If applicable, decrypt (and decompress) record content
5447 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005448static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5449 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005450{
5451 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005454 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5457 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 ret = mbedtls_ssl_hw_record_read( ssl );
5462 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5465 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005466 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005467
5468 if( ret == 0 )
5469 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005472 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005474 unsigned char const old_msg_type = rec->type;
5475
Hanno Becker611a83b2018-01-03 14:27:32 +00005476 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005477 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005480
Hanno Beckera5a2b082019-05-15 14:03:01 +01005481#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005482 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005483 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5484 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005485 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005487 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005488 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005489#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005490
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 return( ret );
5492 }
5493
Hanno Becker106f3da2019-07-12 09:35:58 +01005494 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005495 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005496 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005497 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005499
Paul Bakker5121ce52009-01-03 21:22:43 +00005500 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005501 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005502
Hanno Beckera5a2b082019-05-15 14:03:01 +01005503#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005504 /* We have already checked the record content type
5505 * in ssl_parse_record_header(), failing or silently
5506 * dropping the record in the case of an unknown type.
5507 *
5508 * Since with the use of CIDs, the record content type
5509 * might change during decryption, re-check the record
5510 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005511 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005512 {
5513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5514 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5515 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005516#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005517
Hanno Becker106f3da2019-07-12 09:35:58 +01005518 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005519 {
5520#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005521 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005522 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005523 {
5524 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5526 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5527 }
5528#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5529
5530 ssl->nb_zero++;
5531
5532 /*
5533 * Three or more empty messages may be a DoS attack
5534 * (excessive CPU consumption).
5535 */
5536 if( ssl->nb_zero > 3 )
5537 {
5538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005539 "messages, possible DoS attack" ) );
5540 /* Treat the records as if they were not properly authenticated,
5541 * thereby failing the connection if we see more than allowed
5542 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005543 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5544 }
5545 }
5546 else
5547 ssl->nb_zero = 0;
5548
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005549 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5550#if defined(MBEDTLS_SSL_PROTO_TLS)
5551 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005552 {
5553 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005554 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005555 if( ++ssl->in_ctr[i - 1] != 0 )
5556 break;
5557
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005558 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005559 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005560 {
5561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5562 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5563 }
5564 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005565#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005566
Paul Bakker5121ce52009-01-03 21:22:43 +00005567 }
5568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005570 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005571 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005572 {
5573 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005576 return( ret );
5577 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005581#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005582 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005585 }
5586#endif
5587
Hanno Beckerf0242852019-07-09 17:30:02 +01005588 /* Check actual (decrypted) record content length against
5589 * configured maximum. */
5590 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5591 {
5592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5593 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5594 }
5595
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005596 return( 0 );
5597}
5598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005599static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005600
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005601/*
5602 * Read a record.
5603 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005604 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5605 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5606 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005607 */
Hanno Becker1097b342018-08-15 14:09:41 +01005608
5609/* Helper functions for mbedtls_ssl_read_record(). */
5610static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005611static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5612static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005613
Hanno Becker327c93b2018-08-15 13:56:18 +01005614int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005615 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005616{
5617 int ret;
5618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005620
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005621 if( ssl->keep_current_message == 0 )
5622 {
5623 do {
Simon Butcher99000142016-10-13 17:21:01 +01005624
Hanno Becker26994592018-08-15 14:14:59 +01005625 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005626 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005627 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005628
Hanno Beckere74d5562018-08-15 14:26:08 +01005629 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005630 {
Hanno Becker40f50842018-08-15 14:48:01 +01005631#if defined(MBEDTLS_SSL_PROTO_DTLS)
5632 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005633
Hanno Becker40f50842018-08-15 14:48:01 +01005634 /* We only check for buffered messages if the
5635 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005636 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005637 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005638 {
Hanno Becker40f50842018-08-15 14:48:01 +01005639 if( ssl_load_buffered_message( ssl ) == 0 )
5640 have_buffered = 1;
5641 }
5642
5643 if( have_buffered == 0 )
5644#endif /* MBEDTLS_SSL_PROTO_DTLS */
5645 {
5646 ret = ssl_get_next_record( ssl );
5647 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5648 continue;
5649
5650 if( ret != 0 )
5651 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005652 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005653 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005654 return( ret );
5655 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005656 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005657 }
5658
5659 ret = mbedtls_ssl_handle_message_type( ssl );
5660
Hanno Becker40f50842018-08-15 14:48:01 +01005661#if defined(MBEDTLS_SSL_PROTO_DTLS)
5662 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5663 {
5664 /* Buffer future message */
5665 ret = ssl_buffer_message( ssl );
5666 if( ret != 0 )
5667 return( ret );
5668
5669 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5670 }
5671#endif /* MBEDTLS_SSL_PROTO_DTLS */
5672
Hanno Becker90333da2017-10-10 11:27:13 +01005673 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5674 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005675
5676 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005677 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005678 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005679 return( ret );
5680 }
5681
Hanno Becker327c93b2018-08-15 13:56:18 +01005682 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005683 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005684 {
5685 mbedtls_ssl_update_handshake_status( ssl );
5686 }
Simon Butcher99000142016-10-13 17:21:01 +01005687 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005688 else
Simon Butcher99000142016-10-13 17:21:01 +01005689 {
Hanno Becker02f59072018-08-15 14:00:24 +01005690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005691 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005692 }
5693
5694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5695
5696 return( 0 );
5697}
5698
Hanno Becker40f50842018-08-15 14:48:01 +01005699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005700static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005701{
Hanno Becker40f50842018-08-15 14:48:01 +01005702 if( ssl->in_left > ssl->next_record_offset )
5703 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005704
Hanno Becker40f50842018-08-15 14:48:01 +01005705 return( 0 );
5706}
5707
5708static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5709{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005710 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005711 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005712 int ret = 0;
5713
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005714 if( hs == NULL )
5715 return( -1 );
5716
Hanno Beckere00ae372018-08-20 09:39:42 +01005717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5718
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005719 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5720 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5721 {
5722 /* Check if we have seen a ChangeCipherSpec before.
5723 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005724 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005725 {
5726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5727 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005728 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005729 }
5730
Hanno Becker39b8bc92018-08-28 17:17:13 +01005731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005732 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5733 ssl->in_msglen = 1;
5734 ssl->in_msg[0] = 1;
5735
5736 /* As long as they are equal, the exact value doesn't matter. */
5737 ssl->in_left = 0;
5738 ssl->next_record_offset = 0;
5739
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005740 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005741 goto exit;
5742 }
Hanno Becker37f95322018-08-16 13:55:32 +01005743
Hanno Beckerb8f50142018-08-28 10:01:34 +01005744#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005745 /* Debug only */
5746 {
5747 unsigned offset;
5748 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5749 {
5750 hs_buf = &hs->buffering.hs[offset];
5751 if( hs_buf->is_valid == 1 )
5752 {
5753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5754 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005755 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005756 }
5757 }
5758 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005759#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005760
5761 /* Check if we have buffered and/or fully reassembled the
5762 * next handshake message. */
5763 hs_buf = &hs->buffering.hs[0];
5764 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5765 {
5766 /* Synthesize a record containing the buffered HS message. */
5767 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5768 ( hs_buf->data[2] << 8 ) |
5769 hs_buf->data[3];
5770
5771 /* Double-check that we haven't accidentally buffered
5772 * a message that doesn't fit into the input buffer. */
5773 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5774 {
5775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5776 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5777 }
5778
5779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5780 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5781 hs_buf->data, msg_len + 12 );
5782
5783 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5784 ssl->in_hslen = msg_len + 12;
5785 ssl->in_msglen = msg_len + 12;
5786 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5787
5788 ret = 0;
5789 goto exit;
5790 }
5791 else
5792 {
5793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5794 hs->in_msg_seq ) );
5795 }
5796
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005797 ret = -1;
5798
5799exit:
5800
5801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5802 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005803}
5804
Hanno Beckera02b0b42018-08-21 17:20:27 +01005805static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5806 size_t desired )
5807{
5808 int offset;
5809 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5811 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005812
Hanno Becker01315ea2018-08-21 17:22:17 +01005813 /* Get rid of future records epoch first, if such exist. */
5814 ssl_free_buffered_record( ssl );
5815
5816 /* Check if we have enough space available now. */
5817 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5818 hs->buffering.total_bytes_buffered ) )
5819 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005821 return( 0 );
5822 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005823
Hanno Becker4f432ad2018-08-28 10:02:32 +01005824 /* We don't have enough space to buffer the next expected handshake
5825 * message. Remove buffers used for future messages to gain space,
5826 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005827 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5828 offset >= 0; offset-- )
5829 {
5830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5831 offset ) );
5832
Hanno Beckerb309b922018-08-23 13:18:05 +01005833 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005834
5835 /* Check if we have enough space available now. */
5836 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5837 hs->buffering.total_bytes_buffered ) )
5838 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005840 return( 0 );
5841 }
5842 }
5843
5844 return( -1 );
5845}
5846
Hanno Becker40f50842018-08-15 14:48:01 +01005847static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5848{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005849 int ret = 0;
5850 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5851
5852 if( hs == NULL )
5853 return( 0 );
5854
5855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5856
5857 switch( ssl->in_msgtype )
5858 {
5859 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005861
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005862 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863 break;
5864
5865 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005866 {
5867 unsigned recv_msg_seq_offset;
5868 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5869 mbedtls_ssl_hs_buffer *hs_buf;
5870 size_t msg_len = ssl->in_hslen - 12;
5871
5872 /* We should never receive an old handshake
5873 * message - double-check nonetheless. */
5874 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5875 {
5876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5878 }
5879
5880 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5881 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5882 {
5883 /* Silently ignore -- message too far in the future */
5884 MBEDTLS_SSL_DEBUG_MSG( 2,
5885 ( "Ignore future HS message with sequence number %u, "
5886 "buffering window %u - %u",
5887 recv_msg_seq, ssl->handshake->in_msg_seq,
5888 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5889
5890 goto exit;
5891 }
5892
5893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5894 recv_msg_seq, recv_msg_seq_offset ) );
5895
5896 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5897
5898 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005899 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005900 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005901 size_t reassembly_buf_sz;
5902
Hanno Becker37f95322018-08-16 13:55:32 +01005903 hs_buf->is_fragmented =
5904 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5905
5906 /* We copy the message back into the input buffer
5907 * after reassembly, so check that it's not too large.
5908 * This is an implementation-specific limitation
5909 * and not one from the standard, hence it is not
5910 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005911 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005912 {
5913 /* Ignore message */
5914 goto exit;
5915 }
5916
Hanno Beckere0b150f2018-08-21 15:51:03 +01005917 /* Check if we have enough space to buffer the message. */
5918 if( hs->buffering.total_bytes_buffered >
5919 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5920 {
5921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5923 }
5924
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005925 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5926 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005927
5928 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5929 hs->buffering.total_bytes_buffered ) )
5930 {
5931 if( recv_msg_seq_offset > 0 )
5932 {
5933 /* If we can't buffer a future message because
5934 * of space limitations -- ignore. */
5935 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",
5936 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5937 (unsigned) hs->buffering.total_bytes_buffered ) );
5938 goto exit;
5939 }
Hanno Beckere1801392018-08-21 16:51:05 +01005940 else
5941 {
5942 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",
5943 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5944 (unsigned) hs->buffering.total_bytes_buffered ) );
5945 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005946
Hanno Beckera02b0b42018-08-21 17:20:27 +01005947 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005948 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005949 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",
5950 (unsigned) msg_len,
5951 (unsigned) reassembly_buf_sz,
5952 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005953 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005954 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5955 goto exit;
5956 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005957 }
5958
5959 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5960 msg_len ) );
5961
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005962 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5963 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005964 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005965 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005966 goto exit;
5967 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005968 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005969
5970 /* Prepare final header: copy msg_type, length and message_seq,
5971 * then add standardised fragment_offset and fragment_length */
5972 memcpy( hs_buf->data, ssl->in_msg, 6 );
5973 memset( hs_buf->data + 6, 0, 3 );
5974 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5975
5976 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005977
5978 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005979 }
5980 else
5981 {
5982 /* Make sure msg_type and length are consistent */
5983 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5984 {
5985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5986 /* Ignore */
5987 goto exit;
5988 }
5989 }
5990
Hanno Becker4422bbb2018-08-20 09:40:19 +01005991 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005992 {
5993 size_t frag_len, frag_off;
5994 unsigned char * const msg = hs_buf->data + 12;
5995
5996 /*
5997 * Check and copy current fragment
5998 */
5999
6000 /* Validation of header fields already done in
6001 * mbedtls_ssl_prepare_handshake_record(). */
6002 frag_off = ssl_get_hs_frag_off( ssl );
6003 frag_len = ssl_get_hs_frag_len( ssl );
6004
6005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6006 frag_off, frag_len ) );
6007 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6008
6009 if( hs_buf->is_fragmented )
6010 {
6011 unsigned char * const bitmask = msg + msg_len;
6012 ssl_bitmask_set( bitmask, frag_off, frag_len );
6013 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6014 msg_len ) == 0 );
6015 }
6016 else
6017 {
6018 hs_buf->is_complete = 1;
6019 }
6020
6021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6022 hs_buf->is_complete ? "" : "not yet " ) );
6023 }
6024
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006025 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006026 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006027
6028 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006029 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006030 break;
6031 }
6032
6033exit:
6034
6035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6036 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006037}
6038#endif /* MBEDTLS_SSL_PROTO_DTLS */
6039
Hanno Becker1097b342018-08-15 14:09:41 +01006040static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006041{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006042 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006043 * Consume last content-layer message and potentially
6044 * update in_msglen which keeps track of the contents'
6045 * consumption state.
6046 *
6047 * (1) Handshake messages:
6048 * Remove last handshake message, move content
6049 * and adapt in_msglen.
6050 *
6051 * (2) Alert messages:
6052 * Consume whole record content, in_msglen = 0.
6053 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006054 * (3) Change cipher spec:
6055 * Consume whole record content, in_msglen = 0.
6056 *
6057 * (4) Application data:
6058 * Don't do anything - the record layer provides
6059 * the application data as a stream transport
6060 * and consumes through mbedtls_ssl_read only.
6061 *
6062 */
6063
6064 /* Case (1): Handshake messages */
6065 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006066 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006067 /* Hard assertion to be sure that no application data
6068 * is in flight, as corrupting ssl->in_msglen during
6069 * ssl->in_offt != NULL is fatal. */
6070 if( ssl->in_offt != NULL )
6071 {
6072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6074 }
6075
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006076 /*
6077 * Get next Handshake message in the current record
6078 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006079
Hanno Becker4a810fb2017-05-24 16:27:30 +01006080 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006081 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006082 * current handshake content: If DTLS handshake
6083 * fragmentation is used, that's the fragment
6084 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006085 * size here is faulty and should be changed at
6086 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006087 * (2) While it doesn't seem to cause problems, one
6088 * has to be very careful not to assume that in_hslen
6089 * is always <= in_msglen in a sensible communication.
6090 * Again, it's wrong for DTLS handshake fragmentation.
6091 * The following check is therefore mandatory, and
6092 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006093 * Additionally, ssl->in_hslen might be arbitrarily out of
6094 * bounds after handling a DTLS message with an unexpected
6095 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006096 */
6097 if( ssl->in_hslen < ssl->in_msglen )
6098 {
6099 ssl->in_msglen -= ssl->in_hslen;
6100 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6101 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006102
Hanno Becker4a810fb2017-05-24 16:27:30 +01006103 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6104 ssl->in_msg, ssl->in_msglen );
6105 }
6106 else
6107 {
6108 ssl->in_msglen = 0;
6109 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006110
Hanno Becker4a810fb2017-05-24 16:27:30 +01006111 ssl->in_hslen = 0;
6112 }
6113 /* Case (4): Application data */
6114 else if( ssl->in_offt != NULL )
6115 {
6116 return( 0 );
6117 }
6118 /* Everything else (CCS & Alerts) */
6119 else
6120 {
6121 ssl->in_msglen = 0;
6122 }
6123
Hanno Becker1097b342018-08-15 14:09:41 +01006124 return( 0 );
6125}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006126
Hanno Beckere74d5562018-08-15 14:26:08 +01006127static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6128{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006129 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006130 return( 1 );
6131
6132 return( 0 );
6133}
6134
Hanno Becker5f066e72018-08-16 14:56:31 +01006135#if defined(MBEDTLS_SSL_PROTO_DTLS)
6136
6137static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6138{
6139 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6140 if( hs == NULL )
6141 return;
6142
Hanno Becker01315ea2018-08-21 17:22:17 +01006143 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006144 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006145 hs->buffering.total_bytes_buffered -=
6146 hs->buffering.future_record.len;
6147
6148 mbedtls_free( hs->buffering.future_record.data );
6149 hs->buffering.future_record.data = NULL;
6150 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006151}
6152
6153static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6154{
6155 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6156 unsigned char * rec;
6157 size_t rec_len;
6158 unsigned rec_epoch;
6159
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006160 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006161 return( 0 );
6162
6163 if( hs == NULL )
6164 return( 0 );
6165
Hanno Becker5f066e72018-08-16 14:56:31 +01006166 rec = hs->buffering.future_record.data;
6167 rec_len = hs->buffering.future_record.len;
6168 rec_epoch = hs->buffering.future_record.epoch;
6169
6170 if( rec == NULL )
6171 return( 0 );
6172
Hanno Becker4cb782d2018-08-20 11:19:05 +01006173 /* Only consider loading future records if the
6174 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006175 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006176 return( 0 );
6177
Hanno Becker5f066e72018-08-16 14:56:31 +01006178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6179
6180 if( rec_epoch != ssl->in_epoch )
6181 {
6182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6183 goto exit;
6184 }
6185
6186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6187
6188 /* Double-check that the record is not too large */
6189 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6190 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6191 {
6192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6194 }
6195
6196 memcpy( ssl->in_hdr, rec, rec_len );
6197 ssl->in_left = rec_len;
6198 ssl->next_record_offset = 0;
6199
6200 ssl_free_buffered_record( ssl );
6201
6202exit:
6203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6204 return( 0 );
6205}
6206
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006207static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6208 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006209{
6210 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006211
6212 /* Don't buffer future records outside handshakes. */
6213 if( hs == NULL )
6214 return( 0 );
6215
6216 /* Only buffer handshake records (we are only interested
6217 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006218 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006219 return( 0 );
6220
6221 /* Don't buffer more than one future epoch record. */
6222 if( hs->buffering.future_record.data != NULL )
6223 return( 0 );
6224
Hanno Becker01315ea2018-08-21 17:22:17 +01006225 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006226 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006227 hs->buffering.total_bytes_buffered ) )
6228 {
6229 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 +01006230 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006231 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006232 return( 0 );
6233 }
6234
Hanno Becker5f066e72018-08-16 14:56:31 +01006235 /* Buffer record */
6236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6237 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006238 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006239
6240 /* ssl_parse_record_header() only considers records
6241 * of the next epoch as candidates for buffering. */
6242 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006243 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006244
6245 hs->buffering.future_record.data =
6246 mbedtls_calloc( 1, hs->buffering.future_record.len );
6247 if( hs->buffering.future_record.data == NULL )
6248 {
6249 /* If we run out of RAM trying to buffer a
6250 * record from the next epoch, just ignore. */
6251 return( 0 );
6252 }
6253
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006254 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006255
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006256 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006257 return( 0 );
6258}
6259
6260#endif /* MBEDTLS_SSL_PROTO_DTLS */
6261
Hanno Beckere74d5562018-08-15 14:26:08 +01006262static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006263{
6264 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006265 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006266
Hanno Becker5f066e72018-08-16 14:56:31 +01006267#if defined(MBEDTLS_SSL_PROTO_DTLS)
6268 /* We might have buffered a future record; if so,
6269 * and if the epoch matches now, load it.
6270 * On success, this call will set ssl->in_left to
6271 * the length of the buffered record, so that
6272 * the calls to ssl_fetch_input() below will
6273 * essentially be no-ops. */
6274 ret = ssl_load_buffered_record( ssl );
6275 if( ret != 0 )
6276 return( ret );
6277#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006278
Hanno Becker8b09b732019-05-08 12:03:28 +01006279 /* Ensure that we have enough space available for the default form
6280 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6281 * with no space for CIDs counted in). */
6282 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6283 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006286 return( ret );
6287 }
6288
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006289 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6290 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006293 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006294 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006295 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6296 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006297 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006298 if( ret != 0 )
6299 return( ret );
6300
6301 /* Fall through to handling of unexpected records */
6302 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6303 }
6304
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006305 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6306 {
Hanno Becker87b56262019-07-10 14:37:41 +01006307#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006308 /* Reset in pointers to default state for TLS/DTLS records,
6309 * assuming no CID and no offset between record content and
6310 * record plaintext. */
6311 ssl_update_in_pointers( ssl );
6312
Hanno Becker69412452019-07-12 08:33:49 +01006313 /* Setup internal message pointers from record structure. */
6314 ssl->in_msgtype = rec.type;
6315#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6316 ssl->in_len = ssl->in_cid + rec.cid_len;
6317#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006318 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006319 ssl->in_msglen = rec.data_len;
6320
Hanno Becker87b56262019-07-10 14:37:41 +01006321 ret = ssl_check_client_reconnect( ssl );
6322 if( ret != 0 )
6323 return( ret );
6324#endif
6325
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006326 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006327 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006328
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6330 "(header)" ) );
6331 }
6332 else
6333 {
6334 /* Skip invalid record and the rest of the datagram */
6335 ssl->next_record_offset = 0;
6336 ssl->in_left = 0;
6337
6338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6339 "(header)" ) );
6340 }
6341
6342 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006343 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006344 }
Hanno Becker87b56262019-07-10 14:37:41 +01006345 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006346#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006347 {
6348 return( ret );
6349 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006350 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006353 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006354 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006355 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006356 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006357 if( ssl->next_record_offset < ssl->in_left )
6358 {
6359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6360 }
6361 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006362 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006363#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006364#if defined(MBEDTLS_SSL_PROTO_TLS)
6365 {
Hanno Beckere0452772019-07-10 17:12:07 +01006366 /*
6367 * Fetch record contents from underlying transport.
6368 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006369 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006370 if( ret != 0 )
6371 {
6372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6373 return( ret );
6374 }
6375
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006376 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006377 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006378#endif /* MBEDTLS_SSL_PROTO_TLS */
6379
6380 /*
6381 * Decrypt record contents.
6382 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006383
Hanno Beckera89610a2019-07-11 13:07:45 +01006384 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006387 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006388 {
6389 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006390 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006391 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006392 /* Except when waiting for Finished as a bad mac here
6393 * probably means something went wrong in the handshake
6394 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6395 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6396 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6397 {
6398#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6399 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6400 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006401 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006402 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6403 }
6404#endif
6405 return( ret );
6406 }
6407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006408#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006409 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6410 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6413 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006414 }
6415#endif
6416
Hanno Becker4a810fb2017-05-24 16:27:30 +01006417 /* As above, invalid records cause
6418 * dismissal of the whole datagram. */
6419
6420 ssl->next_record_offset = 0;
6421 ssl->in_left = 0;
6422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006424 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006425 }
6426
6427 return( ret );
6428 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006429 MBEDTLS_SSL_TRANSPORT_ELSE
6430#endif /* MBEDTLS_SSL_PROTO_DTLS */
6431#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006432 {
6433 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006434#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6435 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006436 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006437 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006439 }
6440#endif
6441 return( ret );
6442 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006443#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006444 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006445
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006446
6447 /* Reset in pointers to default state for TLS/DTLS records,
6448 * assuming no CID and no offset between record content and
6449 * record plaintext. */
6450 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006451#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6452 ssl->in_len = ssl->in_cid + rec.cid_len;
6453#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006454 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006455
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006456 /* The record content type may change during decryption,
6457 * so re-read it. */
6458 ssl->in_msgtype = rec.type;
6459 /* Also update the input buffer, because unfortunately
6460 * the server-side ssl_parse_client_hello() reparses the
6461 * record header when receiving a ClientHello initiating
6462 * a renegotiation. */
6463 ssl->in_hdr[0] = rec.type;
6464 ssl->in_msg = rec.buf + rec.data_offset;
6465 ssl->in_msglen = rec.data_len;
6466 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6467 ssl->in_len[1] = (unsigned char)( rec.data_len );
6468
Simon Butcher99000142016-10-13 17:21:01 +01006469 return( 0 );
6470}
6471
6472int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6473{
6474 int ret;
6475
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006476 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006477 * Handle particular types of records
6478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006480 {
Simon Butcher99000142016-10-13 17:21:01 +01006481 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6482 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006483 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006485 }
6486
Hanno Beckere678eaa2018-08-21 14:57:46 +01006487 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006488 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006489 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006490 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6492 ssl->in_msglen ) );
6493 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006494 }
6495
Hanno Beckere678eaa2018-08-21 14:57:46 +01006496 if( ssl->in_msg[0] != 1 )
6497 {
6498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6499 ssl->in_msg[0] ) );
6500 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6501 }
6502
6503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006504 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006505 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6506 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6507 {
6508 if( ssl->handshake == NULL )
6509 {
6510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6511 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6512 }
6513
6514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6515 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6516 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006517#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006518 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006521 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006522 if( ssl->in_msglen != 2 )
6523 {
6524 /* Note: Standard allows for more than one 2 byte alert
6525 to be packed in a single message, but Mbed TLS doesn't
6526 currently support this. */
6527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6528 ssl->in_msglen ) );
6529 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6530 }
6531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006533 ssl->in_msg[0], ssl->in_msg[1] ) );
6534
6535 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006536 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006541 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006542 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006543 }
6544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6546 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6549 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006550 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006551
6552#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6553 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6554 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6555 {
Hanno Becker90333da2017-10-10 11:27:13 +01006556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006557 /* Will be handled when trying to parse ServerHello */
6558 return( 0 );
6559 }
6560#endif
6561
6562#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006563 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006564 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6565 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006566 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6567 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6568 {
6569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6570 /* Will be handled in mbedtls_ssl_parse_certificate() */
6571 return( 0 );
6572 }
6573#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6574
6575 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006576 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006577 }
6578
Hanno Beckerc76c6192017-06-06 10:03:17 +01006579#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006580 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006581 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006582 /* Drop unexpected ApplicationData records,
6583 * except at the beginning of renegotiations */
6584 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6585 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6586#if defined(MBEDTLS_SSL_RENEGOTIATION)
6587 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6588 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006589#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006590 )
6591 {
6592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6593 return( MBEDTLS_ERR_SSL_NON_FATAL );
6594 }
6595
6596 if( ssl->handshake != NULL &&
6597 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6598 {
6599 ssl_handshake_wrapup_free_hs_transform( ssl );
6600 }
6601 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006602#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006603
Paul Bakker5121ce52009-01-03 21:22:43 +00006604 return( 0 );
6605}
6606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006608{
6609 int ret;
6610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006611 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6613 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006614 {
6615 return( ret );
6616 }
6617
6618 return( 0 );
6619}
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006622 unsigned char level,
6623 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006624{
6625 int ret;
6626
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006627 if( ssl == NULL || ssl->conf == NULL )
6628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006634 ssl->out_msglen = 2;
6635 ssl->out_msg[0] = level;
6636 ssl->out_msg[1] = message;
6637
Hanno Becker67bc7c32018-08-06 11:33:50 +01006638 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006641 return( ret );
6642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006644
6645 return( 0 );
6646}
6647
Hanno Becker17572472019-02-08 07:19:04 +00006648#if defined(MBEDTLS_X509_CRT_PARSE_C)
6649static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6650{
6651#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6652 if( session->peer_cert != NULL )
6653 {
6654 mbedtls_x509_crt_free( session->peer_cert );
6655 mbedtls_free( session->peer_cert );
6656 session->peer_cert = NULL;
6657 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006658#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006659 if( session->peer_cert_digest != NULL )
6660 {
6661 /* Zeroization is not necessary. */
6662 mbedtls_free( session->peer_cert_digest );
6663 session->peer_cert_digest = NULL;
6664 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6665 session->peer_cert_digest_len = 0;
6666 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006667#else
6668 ((void) session);
6669#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006670}
6671#endif /* MBEDTLS_X509_CRT_PARSE_C */
6672
Paul Bakker5121ce52009-01-03 21:22:43 +00006673/*
6674 * Handshake functions
6675 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006676#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006677/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006679{
Hanno Beckerdf645962019-06-26 13:02:22 +01006680 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6681 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006684
Hanno Becker5097cba2019-02-05 13:36:46 +00006685 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006688 ssl->state++;
6689 return( 0 );
6690 }
6691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006694}
6695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006697{
Hanno Beckerdf645962019-06-26 13:02:22 +01006698 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6699 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006702
Hanno Becker5097cba2019-02-05 13:36:46 +00006703 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006706 ssl->state++;
6707 return( 0 );
6708 }
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6711 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006712}
Gilles Peskinef9828522017-05-03 12:28:43 +02006713
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006714#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006715/* Some certificate support -> implement write and parse */
6716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006718{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006720 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006721 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006722 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6723 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006726
Hanno Becker5097cba2019-02-05 13:36:46 +00006727 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006730 ssl->state++;
6731 return( 0 );
6732 }
6733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006735 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6736 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006737 {
6738 if( ssl->client_auth == 0 )
6739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006741 ssl->state++;
6742 return( 0 );
6743 }
6744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 /*
6747 * If using SSLv3 and got no cert, send an Alert message
6748 * (otherwise an empty Certificate message will be sent).
6749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006751 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006752 {
6753 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6755 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6756 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006759 goto write_msg;
6760 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006762 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763#endif /* MBEDTLS_SSL_CLI_C */
6764#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006765 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006767 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6770 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006771 }
6772 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006773#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776
6777 /*
6778 * 0 . 0 handshake type
6779 * 1 . 3 handshake length
6780 * 4 . 6 length of all certs
6781 * 7 . 9 length of cert. 1
6782 * 10 . n-1 peer certificate
6783 * n . n+2 length of cert. 2
6784 * n+3 . ... upper level cert, etc.
6785 */
6786 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006787 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006788
Paul Bakker29087132010-03-21 21:03:34 +00006789 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006790 {
6791 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006792 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006795 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006797 }
6798
6799 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6800 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6801 ssl->out_msg[i + 2] = (unsigned char)( n );
6802
6803 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6804 i += n; crt = crt->next;
6805 }
6806
6807 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6808 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6809 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6810
6811 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6813 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006815#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006816write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006817#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
6819 ssl->state++;
6820
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006821 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006824 return( ret );
6825 }
6826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006828
Paul Bakkered27a042013-04-18 22:46:23 +02006829 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830}
6831
Hanno Becker285ff0c2019-01-31 07:44:03 +00006832#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006833
6834#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006835static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6836 unsigned char *crt_buf,
6837 size_t crt_buf_len )
6838{
6839 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6840
6841 if( peer_crt == NULL )
6842 return( -1 );
6843
6844 if( peer_crt->raw.len != crt_buf_len )
6845 return( -1 );
6846
Hanno Becker68b856d2019-02-08 14:00:04 +00006847 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006848}
Hanno Becker5882dd02019-06-06 16:25:57 +01006849#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006850static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6851 unsigned char *crt_buf,
6852 size_t crt_buf_len )
6853{
6854 int ret;
6855 unsigned char const * const peer_cert_digest =
6856 ssl->session->peer_cert_digest;
6857 mbedtls_md_type_t const peer_cert_digest_type =
6858 ssl->session->peer_cert_digest_type;
6859 mbedtls_md_info_t const * const digest_info =
6860 mbedtls_md_info_from_type( peer_cert_digest_type );
6861 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6862 size_t digest_len;
6863
6864 if( peer_cert_digest == NULL || digest_info == NULL )
6865 return( -1 );
6866
6867 digest_len = mbedtls_md_get_size( digest_info );
6868 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6869 return( -1 );
6870
6871 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6872 if( ret != 0 )
6873 return( -1 );
6874
6875 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6876}
Hanno Becker5882dd02019-06-06 16:25:57 +01006877#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006878#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006879
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006880/*
6881 * Once the certificate message is read, parse it into a cert chain and
6882 * perform basic checks, but leave actual verification to the caller
6883 */
Hanno Becker35e41772019-02-05 15:37:23 +00006884static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6885 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006886{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006887 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006888#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6889 int crt_cnt=0;
6890#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006891 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006892 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006897 mbedtls_ssl_pend_fatal_alert( ssl,
6898 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006900 }
6901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6903 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006906 mbedtls_ssl_pend_fatal_alert( ssl,
6907 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006909 }
6910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006912
Paul Bakker5121ce52009-01-03 21:22:43 +00006913 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006915 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006916 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006917
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006918 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006922 mbedtls_ssl_pend_fatal_alert( ssl,
6923 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006925 }
6926
Hanno Becker33c3dc82019-01-30 14:46:46 +00006927 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6928 i += 3;
6929
Hanno Becker33c3dc82019-01-30 14:46:46 +00006930 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006931 while( i < ssl->in_hslen )
6932 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006933 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006934 if ( i + 3 > ssl->in_hslen ) {
6935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006936 mbedtls_ssl_pend_fatal_alert( ssl,
6937 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006938 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6939 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006940 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6941 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006942 if( ssl->in_msg[i] != 0 )
6943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006945 mbedtls_ssl_pend_fatal_alert( ssl,
6946 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006947 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006948 }
6949
Hanno Becker33c3dc82019-01-30 14:46:46 +00006950 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006951 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6952 | (unsigned int) ssl->in_msg[i + 2];
6953 i += 3;
6954
6955 if( n < 128 || i + n > ssl->in_hslen )
6956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006958 mbedtls_ssl_pend_fatal_alert( ssl,
6959 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006961 }
6962
Hanno Becker33c3dc82019-01-30 14:46:46 +00006963 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006964#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6965 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006966 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6967 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006968 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006969 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006970 /* During client-side renegotiation, check that the server's
6971 * end-CRTs hasn't changed compared to the initial handshake,
6972 * mitigating the triple handshake attack. On success, reuse
6973 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6975 if( ssl_check_peer_crt_unchanged( ssl,
6976 &ssl->in_msg[i],
6977 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006978 {
Hanno Becker35e41772019-02-05 15:37:23 +00006979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006980 mbedtls_ssl_pend_fatal_alert( ssl,
6981 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00006982 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006983 }
Hanno Becker35e41772019-02-05 15:37:23 +00006984
6985 /* Now we can safely free the original chain. */
6986 ssl_clear_peer_cert( ssl->session );
6987 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006988#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6989
Hanno Becker33c3dc82019-01-30 14:46:46 +00006990 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006991#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006992 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006993#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006994 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006995 * it in-place from the input buffer instead of making a copy. */
6996 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6997#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006998 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006999 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007000 case 0: /*ok*/
7001 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7002 /* Ignore certificate with an unknown algorithm: maybe a
7003 prior certificate was already trusted. */
7004 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007005
Hanno Becker33c3dc82019-01-30 14:46:46 +00007006 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7007 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7008 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007009
Hanno Becker33c3dc82019-01-30 14:46:46 +00007010 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7011 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7012 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007013
Hanno Becker33c3dc82019-01-30 14:46:46 +00007014 default:
7015 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7016 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007017 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007018 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7019 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007020 }
7021
7022 i += n;
7023 }
7024
Hanno Becker35e41772019-02-05 15:37:23 +00007025 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007026 return( 0 );
7027}
7028
Hanno Beckerb8a08572019-02-05 12:49:06 +00007029#if defined(MBEDTLS_SSL_SRV_C)
7030static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7031{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007032 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007033 return( -1 );
7034
7035#if defined(MBEDTLS_SSL_PROTO_SSL3)
7036 /*
7037 * Check if the client sent an empty certificate
7038 */
Hanno Becker2881d802019-05-22 14:44:53 +01007039 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007040 {
7041 if( ssl->in_msglen == 2 &&
7042 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7043 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7044 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7045 {
7046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7047 return( 0 );
7048 }
7049
7050 return( -1 );
7051 }
7052#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7053
7054#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7055 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7056 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7057 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7058 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7059 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7060 {
7061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7062 return( 0 );
7063 }
7064
Hanno Beckerb8a08572019-02-05 12:49:06 +00007065#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7066 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007067
7068 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007069}
7070#endif /* MBEDTLS_SSL_SRV_C */
7071
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007072/* Check if a certificate message is expected.
7073 * Return either
7074 * - SSL_CERTIFICATE_EXPECTED, or
7075 * - SSL_CERTIFICATE_SKIP
7076 * indicating whether a Certificate message is expected or not.
7077 */
7078#define SSL_CERTIFICATE_EXPECTED 0
7079#define SSL_CERTIFICATE_SKIP 1
7080static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7081 int authmode )
7082{
Hanno Becker473f98f2019-06-26 10:27:32 +01007083 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007084 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007085
7086 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7087 return( SSL_CERTIFICATE_SKIP );
7088
7089#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007090 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007091 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007092 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7093 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7094 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007095 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007096 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007097
7098 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7099 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007100 ssl->session_negotiate->verify_result =
7101 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7102 return( SSL_CERTIFICATE_SKIP );
7103 }
7104 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007105#else
7106 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007107#endif /* MBEDTLS_SSL_SRV_C */
7108
7109 return( SSL_CERTIFICATE_EXPECTED );
7110}
7111
Hanno Becker3cf50612019-02-05 14:36:34 +00007112static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7113 int authmode,
7114 mbedtls_x509_crt *chain,
7115 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007116{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007117 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007118 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007119 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007120 mbedtls_x509_crt *ca_chain;
7121 mbedtls_x509_crl *ca_crl;
7122
7123 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7124 return( 0 );
7125
7126#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7127 if( ssl->handshake->sni_ca_chain != NULL )
7128 {
7129 ca_chain = ssl->handshake->sni_ca_chain;
7130 ca_crl = ssl->handshake->sni_ca_crl;
7131 }
7132 else
7133#endif
7134 {
7135 ca_chain = ssl->conf->ca_chain;
7136 ca_crl = ssl->conf->ca_crl;
7137 }
7138
7139 /*
7140 * Main check: verify certificate
7141 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007142 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007143 chain,
7144 ca_chain, ca_crl,
7145 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007146#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007147 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007148#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007149 &ssl->session_negotiate->verify_result,
7150 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7151
Hanno Becker8c13ee62019-02-26 16:48:17 +00007152 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007153 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007154 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007155 }
7156
7157#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007158 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007159 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7160#endif
7161
7162 /*
7163 * Secondary checks: always done, but change 'ret' only if it was 0
7164 */
7165
7166#if defined(MBEDTLS_ECP_C)
7167 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007168 int ret;
Hanno Becker8c13ee62019-02-26 16:48:17 +00007169 mbedtls_pk_context *pk;
7170 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7171 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007172 {
7173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007174 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007175 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007176
7177 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007178 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
7179 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
7180
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007181 mbedtls_x509_crt_pk_release( chain );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007182
7183 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007184 {
7185 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7186
7187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007188 if( verify_ret == 0 )
7189 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007190 }
7191 }
7192#endif /* MBEDTLS_ECP_C */
7193
7194 if( mbedtls_ssl_check_cert_usage( chain,
7195 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007196 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7197 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007198 &ssl->session_negotiate->verify_result ) != 0 )
7199 {
7200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007201 if( verify_ret == 0 )
7202 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007203 }
7204
7205 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7206 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7207 * with details encoded in the verification flags. All other kinds
7208 * of error codes, including those from the user provided f_vrfy
7209 * functions, are treated as fatal and lead to a failure of
7210 * ssl_parse_certificate even if verification was optional. */
7211 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007212 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7213 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007214 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007215 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007216 }
7217
7218 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7219 {
7220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007221 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007222 }
7223
Hanno Becker8c13ee62019-02-26 16:48:17 +00007224 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007225 {
7226 uint8_t alert;
7227
7228 /* The certificate may have been rejected for several reasons.
7229 Pick one and send the corresponding alert. Which alert to send
7230 may be a subject of debate in some cases. */
7231 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7232 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7233 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7234 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7235 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7236 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7237 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7238 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7239 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7240 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7241 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7242 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7243 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7244 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7245 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7246 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7247 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7248 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7249 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7250 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7251 else
7252 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007253 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007254 }
7255
7256#if defined(MBEDTLS_DEBUG_C)
7257 if( ssl->session_negotiate->verify_result != 0 )
7258 {
7259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7260 ssl->session_negotiate->verify_result ) );
7261 }
7262 else
7263 {
7264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7265 }
7266#endif /* MBEDTLS_DEBUG_C */
7267
Hanno Becker8c13ee62019-02-26 16:48:17 +00007268 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007269}
7270
Hanno Becker34106f62019-02-08 14:59:05 +00007271#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007272
7273#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007274static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7275 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007276{
7277 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007278 /* Remember digest of the peer's end-CRT. */
7279 ssl->session_negotiate->peer_cert_digest =
7280 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7281 if( ssl->session_negotiate->peer_cert_digest == NULL )
7282 {
7283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7284 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007285 mbedtls_ssl_pend_fatal_alert( ssl,
7286 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007287
7288 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7289 }
7290
7291 ret = mbedtls_md( mbedtls_md_info_from_type(
7292 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7293 start, len,
7294 ssl->session_negotiate->peer_cert_digest );
7295
7296 ssl->session_negotiate->peer_cert_digest_type =
7297 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7298 ssl->session_negotiate->peer_cert_digest_len =
7299 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7300
7301 return( ret );
7302}
Hanno Becker5882dd02019-06-06 16:25:57 +01007303#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007304
7305static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7306 unsigned char *start, size_t len )
7307{
7308 unsigned char *end = start + len;
7309 int ret;
7310
7311 /* Make a copy of the peer's raw public key. */
7312 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7313 ret = mbedtls_pk_parse_subpubkey( &start, end,
7314 &ssl->handshake->peer_pubkey );
7315 if( ret != 0 )
7316 {
7317 /* We should have parsed the public key before. */
7318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7319 }
7320
7321 return( 0 );
7322}
7323#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7324
Hanno Becker3cf50612019-02-05 14:36:34 +00007325int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7326{
7327 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007328 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007329#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7330 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7331 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007332 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007333#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007334 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007335#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007336 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007337 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007338
7339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7340
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007341 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7342 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007343 {
7344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007345 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007346 }
7347
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007348#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7349 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007350 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007351 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007352 chain = ssl->handshake->ecrs_peer_cert;
7353 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007354 goto crt_verify;
7355 }
7356#endif
7357
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007358 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007359 {
7360 /* mbedtls_ssl_read_record may have sent an alert already. We
7361 let it decide whether to alert. */
7362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007363 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007364 }
7365
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007366#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007367 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7368 {
7369 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007370
Hanno Beckerb8a08572019-02-05 12:49:06 +00007371 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007372 ret = 0;
7373 else
7374 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007375
Hanno Becker613d4902019-02-05 13:11:17 +00007376 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007377 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007378#endif /* MBEDTLS_SSL_SRV_C */
7379
Hanno Becker35e41772019-02-05 15:37:23 +00007380 /* Clear existing peer CRT structure in case we tried to
7381 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007382 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007383
7384 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7385 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007386 {
7387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7388 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007389 mbedtls_ssl_pend_fatal_alert( ssl,
7390 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007391
Hanno Beckere4aeb762019-02-05 17:19:52 +00007392 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7393 goto exit;
7394 }
7395 mbedtls_x509_crt_init( chain );
7396
7397 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007398 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007399 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007400
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007401#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7402 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007403 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007404
7405crt_verify:
7406 if( ssl->handshake->ecrs_enabled)
7407 rs_ctx = &ssl->handshake->ecrs_ctx;
7408#endif
7409
Hanno Becker3cf50612019-02-05 14:36:34 +00007410 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007411 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007412 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007413 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007414
Hanno Becker3008d282019-02-05 17:02:28 +00007415#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007416 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007417 size_t pk_len;
7418 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007419
Hanno Becker34106f62019-02-08 14:59:05 +00007420 /* We parse the CRT chain without copying, so
7421 * these pointers point into the input buffer,
7422 * and are hence still valid after freeing the
7423 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007424
Hanno Becker5882dd02019-06-06 16:25:57 +01007425#if defined(MBEDTLS_SSL_RENEGOTIATION)
7426 unsigned char *crt_start;
7427 size_t crt_len;
7428
Hanno Becker34106f62019-02-08 14:59:05 +00007429 crt_start = chain->raw.p;
7430 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007431#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007432
Hanno Becker8c13ee62019-02-26 16:48:17 +00007433 pk_start = chain->cache->pk_raw.p;
7434 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007435
7436 /* Free the CRT structures before computing
7437 * digest and copying the peer's public key. */
7438 mbedtls_x509_crt_free( chain );
7439 mbedtls_free( chain );
7440 chain = NULL;
7441
Hanno Becker5882dd02019-06-06 16:25:57 +01007442#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007443 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007444 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007445 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007446#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007447
Hanno Becker34106f62019-02-08 14:59:05 +00007448 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007449 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007450 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007451 }
Hanno Becker34106f62019-02-08 14:59:05 +00007452#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7453 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007454 ssl->session_negotiate->peer_cert = chain;
7455 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007456#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007459
Hanno Becker613d4902019-02-05 13:11:17 +00007460exit:
7461
Hanno Beckere4aeb762019-02-05 17:19:52 +00007462 if( ret == 0 )
7463 ssl->state++;
7464
7465#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7466 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7467 {
7468 ssl->handshake->ecrs_peer_cert = chain;
7469 chain = NULL;
7470 }
7471#endif
7472
7473 if( chain != NULL )
7474 {
7475 mbedtls_x509_crt_free( chain );
7476 mbedtls_free( chain );
7477 }
7478
Paul Bakker5121ce52009-01-03 21:22:43 +00007479 return( ret );
7480}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007481#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007484{
7485 int ret;
7486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007490 ssl->out_msglen = 1;
7491 ssl->out_msg[0] = 1;
7492
Paul Bakker5121ce52009-01-03 21:22:43 +00007493 ssl->state++;
7494
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007495 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007496 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007498 return( ret );
7499 }
7500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502
7503 return( 0 );
7504}
7505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007507{
7508 int ret;
7509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007511
Hanno Becker327c93b2018-08-15 13:56:18 +01007512 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007515 return( ret );
7516 }
7517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007521 mbedtls_ssl_pend_fatal_alert( ssl,
7522 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007524 }
7525
Hanno Beckere678eaa2018-08-21 14:57:46 +01007526 /* CCS records are only accepted if they have length 1 and content '1',
7527 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007528
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007529 /*
7530 * Switch to our negotiated transform and session parameters for inbound
7531 * data.
7532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007534 ssl->transform_in = ssl->transform_negotiate;
7535 ssl->session_in = ssl->session_negotiate;
7536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007538 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007541 ssl_dtls_replay_reset( ssl );
7542#endif
7543
7544 /* Increment epoch */
7545 if( ++ssl->in_epoch == 0 )
7546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007548 /* This is highly unlikely to happen for legitimate reasons, so
7549 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007551 }
7552 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007553 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007555#if defined(MBEDTLS_SSL_PROTO_TLS)
7556 {
7557 memset( ssl->in_ctr, 0, 8 );
7558 }
7559#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007560
Hanno Beckerf5970a02019-05-08 09:38:41 +01007561 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7564 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007569 mbedtls_ssl_pend_fatal_alert( ssl,
7570 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007572 }
7573 }
7574#endif
7575
Paul Bakker5121ce52009-01-03 21:22:43 +00007576 ssl->state++;
7577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007579
7580 return( 0 );
7581}
7582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007584{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7586 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007587 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007588 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7591#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007592 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007595 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007598}
7599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007603
7604 /*
7605 * Free our handshake params
7606 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007607 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007609 ssl->handshake = NULL;
7610
7611 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007612 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007613 */
7614 if( ssl->transform )
7615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 mbedtls_ssl_transform_free( ssl->transform );
7617 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007618 }
7619 ssl->transform = ssl->transform_negotiate;
7620 ssl->transform_negotiate = NULL;
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007623}
7624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#if defined(MBEDTLS_SSL_RENEGOTIATION)
7630 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007633 ssl->renego_records_seen = 0;
7634 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007635#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007636
7637 /*
7638 * Free the previous session and switch in the current one
7639 */
Paul Bakker0a597072012-09-25 21:55:46 +00007640 if( ssl->session )
7641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007643 /* RFC 7366 3.1: keep the EtM state */
7644 ssl->session_negotiate->encrypt_then_mac =
7645 ssl->session->encrypt_then_mac;
7646#endif
7647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_ssl_session_free( ssl->session );
7649 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007650 }
7651 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007652 ssl->session_negotiate = NULL;
7653
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007654#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007655 /*
7656 * Add cache entry
7657 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007658 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007659 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007660 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007661 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007662 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007664 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007665#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007668 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007669 ssl->handshake->flight != NULL )
7670 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007671 /* Cancel handshake timer */
7672 ssl_set_timer( ssl, 0 );
7673
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007674 /* Keep last flight around in case we need to resend it:
7675 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007677 }
7678 else
7679#endif
7680 ssl_handshake_wrapup_free_hs_transform( ssl );
7681
Paul Bakker48916f92012-09-16 19:57:18 +00007682 ssl->state++;
7683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007685}
7686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007689 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007692
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007693 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007694
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007695 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7696 mbedtls_ssl_suite_get_mac(
7697 mbedtls_ssl_ciphersuite_from_id(
7698 mbedtls_ssl_session_get_ciphersuite(
7699 ssl->session_negotiate ) ) ),
7700 ssl, ssl->out_msg + 4,
7701 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007702
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007703 /*
7704 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7705 * may define some other value. Currently (early 2016), no defined
7706 * ciphersuite does this (and this is unlikely to change as activity has
7707 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7708 */
Hanno Becker2881d802019-05-22 14:44:53 +01007709 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007712 ssl->verify_data_len = hash_len;
7713 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007714#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007715
Paul Bakker5121ce52009-01-03 21:22:43 +00007716 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7718 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007719
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007720#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007721 /*
7722 * In case of session resuming, invert the client and server
7723 * ChangeCipherSpec messages order.
7724 */
Paul Bakker0a597072012-09-25 21:55:46 +00007725 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007728 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7729 MBEDTLS_SSL_IS_CLIENT )
7730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007732 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007735 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7736 MBEDTLS_SSL_IS_SERVER )
7737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007739 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007740#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007741 }
7742 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007743#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007744 ssl->state++;
7745
Paul Bakker48916f92012-09-16 19:57:18 +00007746 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007747 * Switch to our negotiated transform and session parameters for outbound
7748 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007753 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007754 {
7755 unsigned char i;
7756
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007757 /* Remember current epoch settings for resending */
7758 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007759 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007760
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007761 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007762 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007763
7764 /* Increment epoch */
7765 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007766 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007767 break;
7768
7769 /* The loop goes to its end iff the counter is wrapping */
7770 if( i == 0 )
7771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7773 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007774 }
7775 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007776 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007778#if defined(MBEDTLS_SSL_PROTO_TLS)
7779 {
7780 memset( ssl->cur_out_ctr, 0, 8 );
7781 }
7782#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007783
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007784 ssl->transform_out = ssl->transform_negotiate;
7785 ssl->session_out = ssl->session_negotiate;
7786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7788 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007790 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7793 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007794 }
7795 }
7796#endif
7797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007799 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007801#endif
7802
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007803 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007804 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007806 return( ret );
7807 }
7808
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007810 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007811 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7812 {
7813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7814 return( ret );
7815 }
7816#endif
7817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007819
7820 return( 0 );
7821}
7822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007824#define SSL_MAX_HASH_LEN 36
7825#else
7826#define SSL_MAX_HASH_LEN 12
7827#endif
7828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007830{
Paul Bakker23986e52011-04-24 08:57:21 +00007831 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007832 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007833 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007836
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007837 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7838 mbedtls_ssl_suite_get_mac(
7839 mbedtls_ssl_ciphersuite_from_id(
7840 mbedtls_ssl_session_get_ciphersuite(
7841 ssl->session_negotiate ) ) ),
7842 ssl, buf,
7843 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007844
Hanno Becker327c93b2018-08-15 13:56:18 +01007845 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007848 return( ret );
7849 }
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007854 mbedtls_ssl_pend_fatal_alert( ssl,
7855 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007857 }
7858
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007859 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007861 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007862 hash_len = 36;
7863 else
7864#endif
7865 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7868 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007871 mbedtls_ssl_pend_fatal_alert( ssl,
7872 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007874 }
7875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007877 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007880 mbedtls_ssl_pend_fatal_alert( ssl,
7881 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007883 }
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007886 ssl->verify_data_len = hash_len;
7887 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007888#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007889
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007890#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007891 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007894 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007896#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007898 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007900#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007901 }
7902 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007903#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007904 ssl->state++;
7905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007907 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007909#endif
7910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007912
7913 return( 0 );
7914}
7915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7921 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7922 mbedtls_md5_init( &handshake->fin_md5 );
7923 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007924 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7925 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007926#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7928#if defined(MBEDTLS_SHA256_C)
7929 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007930 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#if defined(MBEDTLS_SHA512_C)
7933 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007934 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007937
Hanno Becker7e5437a2017-04-28 17:15:26 +01007938#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7939 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7940 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7941#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_DHM_C)
7944 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_ECDH_C)
7947 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007948#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007949#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007950 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007951#if defined(MBEDTLS_SSL_CLI_C)
7952 handshake->ecjpake_cache = NULL;
7953 handshake->ecjpake_cache_len = 0;
7954#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007955#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007956
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007957#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007958 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007959#endif
7960
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007961#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7962 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7963#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007964
7965#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7966 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7967 mbedtls_pk_init( &handshake->peer_pubkey );
7968#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007969}
7970
Hanno Becker611a83b2018-01-03 14:27:32 +00007971void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7976 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007977
Hanno Becker92231322018-01-03 15:32:51 +00007978#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 mbedtls_md_init( &transform->md_ctx_enc );
7980 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007981#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982}
7983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007987}
7988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007990{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007991 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007992 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007994 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007997 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007998
7999 /*
8000 * Either the pointers are now NULL or cleared properly and can be freed.
8001 * Now allocate missing structures.
8002 */
8003 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008004 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008005 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008006 }
Paul Bakker48916f92012-09-16 19:57:18 +00008007
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008008 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008009 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008010 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008011 }
Paul Bakker48916f92012-09-16 19:57:18 +00008012
Paul Bakker82788fb2014-10-20 13:59:19 +02008013 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008014 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008015 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008016 }
Paul Bakker48916f92012-09-16 19:57:18 +00008017
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008018 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008019 if( ssl->handshake == NULL ||
8020 ssl->transform_negotiate == NULL ||
8021 ssl->session_negotiate == NULL )
8022 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 mbedtls_free( ssl->handshake );
8026 mbedtls_free( ssl->transform_negotiate );
8027 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008028
8029 ssl->handshake = NULL;
8030 ssl->transform_negotiate = NULL;
8031 ssl->session_negotiate = NULL;
8032
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008033 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008034 }
8035
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008036 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008038 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008039 ssl_handshake_params_init( ssl->handshake );
8040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008042 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008043 {
8044 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008045
Hanno Becker2d9623f2019-06-13 12:07:05 +01008046 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008047 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8048 else
8049 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8050 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008051#endif
8052
Paul Bakker48916f92012-09-16 19:57:18 +00008053 return( 0 );
8054}
8055
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008056#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008057/* Dummy cookie callbacks for defaults */
8058static int ssl_cookie_write_dummy( void *ctx,
8059 unsigned char **p, unsigned char *end,
8060 const unsigned char *cli_id, size_t cli_id_len )
8061{
8062 ((void) ctx);
8063 ((void) p);
8064 ((void) end);
8065 ((void) cli_id);
8066 ((void) cli_id_len);
8067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008069}
8070
8071static int ssl_cookie_check_dummy( void *ctx,
8072 const unsigned char *cookie, size_t cookie_len,
8073 const unsigned char *cli_id, size_t cli_id_len )
8074{
8075 ((void) ctx);
8076 ((void) cookie);
8077 ((void) cookie_len);
8078 ((void) cli_id);
8079 ((void) cli_id_len);
8080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008082}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008083#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008084
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008085/* Once ssl->out_hdr as the address of the beginning of the
8086 * next outgoing record is set, deduce the other pointers.
8087 *
8088 * Note: For TLS, we save the implicit record sequence number
8089 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8090 * and the caller has to make sure there's space for this.
8091 */
8092
8093static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8094 mbedtls_ssl_transform *transform )
8095{
8096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008097 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008098 {
8099 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008100#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008101 ssl->out_cid = ssl->out_ctr + 8;
8102 ssl->out_len = ssl->out_cid;
8103 if( transform != NULL )
8104 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008105#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008106 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008107#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008108 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008109 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008110 MBEDTLS_SSL_TRANSPORT_ELSE
8111#endif /* MBEDTLS_SSL_PROTO_DTLS */
8112#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008113 {
8114 ssl->out_ctr = ssl->out_hdr - 8;
8115 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008116#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008117 ssl->out_cid = ssl->out_len;
8118#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008119 ssl->out_iv = ssl->out_hdr + 5;
8120 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008121#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008122
8123 /* Adjust out_msg to make space for explicit IV, if used. */
8124 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008125 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008126 {
8127 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8128 }
8129 else
8130 ssl->out_msg = ssl->out_iv;
8131}
8132
8133/* Once ssl->in_hdr as the address of the beginning of the
8134 * next incoming record is set, deduce the other pointers.
8135 *
8136 * Note: For TLS, we save the implicit record sequence number
8137 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8138 * and the caller has to make sure there's space for this.
8139 */
8140
Hanno Beckerf5970a02019-05-08 09:38:41 +01008141static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008142{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008143 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008144 * of unprotected TLS/DTLS records, with ssl->in_msg
8145 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008146 *
8147 * When decrypting a protected record, ssl->in_msg
8148 * will be shifted to point to the beginning of the
8149 * record plaintext.
8150 */
8151
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008153 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008154 {
Hanno Becker70e79282019-05-03 14:34:53 +01008155 /* This sets the header pointers to match records
8156 * without CID. When we receive a record containing
8157 * a CID, the fields are shifted accordingly in
8158 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008159 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008160#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008161 ssl->in_cid = ssl->in_ctr + 8;
8162 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008163#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008164 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008165#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008166 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008167 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008168 MBEDTLS_SSL_TRANSPORT_ELSE
8169#endif /* MBEDTLS_SSL_PROTO_DTLS */
8170#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008171 {
8172 ssl->in_ctr = ssl->in_hdr - 8;
8173 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008174#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008175 ssl->in_cid = ssl->in_len;
8176#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008177 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008178 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008179#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008180}
8181
Paul Bakker5121ce52009-01-03 21:22:43 +00008182/*
8183 * Initialize an SSL context
8184 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008185void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8186{
8187 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8188}
8189
8190/*
8191 * Setup an SSL context
8192 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008193
8194static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8195{
8196 /* Set the incoming and outgoing record pointers. */
8197#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008198 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008199 {
8200 ssl->out_hdr = ssl->out_buf;
8201 ssl->in_hdr = ssl->in_buf;
8202 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008203 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008204#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008205#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008206 {
8207 ssl->out_hdr = ssl->out_buf + 8;
8208 ssl->in_hdr = ssl->in_buf + 8;
8209 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008210#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008211
8212 /* Derive other internal pointers. */
8213 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008214 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008215}
8216
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008217int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008218 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008219{
Paul Bakker48916f92012-09-16 19:57:18 +00008220 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008221
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008222 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008223
Hanno Beckeref982d52019-07-23 15:56:18 +01008224#if defined(MBEDTLS_USE_TINYCRYPT)
8225 uECC_set_rng( &uecc_rng_wrapper );
8226#endif
8227
Paul Bakker62f2dee2012-09-28 07:31:51 +00008228 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008229 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008230 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008231
8232 /* Set to NULL in case of an error condition */
8233 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008234
Angus Grattond8213d02016-05-25 20:56:48 +10008235 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8236 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008237 {
Angus Grattond8213d02016-05-25 20:56:48 +10008238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008239 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008240 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008241 }
8242
8243 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8244 if( ssl->out_buf == NULL )
8245 {
8246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008247 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008248 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008249 }
8250
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008251 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008252
Paul Bakker48916f92012-09-16 19:57:18 +00008253 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008254 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008255
Hanno Beckerc8f52992019-07-25 11:15:08 +01008256 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008257
Paul Bakker5121ce52009-01-03 21:22:43 +00008258 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008259
8260error:
8261 mbedtls_free( ssl->in_buf );
8262 mbedtls_free( ssl->out_buf );
8263
8264 ssl->conf = NULL;
8265
8266 ssl->in_buf = NULL;
8267 ssl->out_buf = NULL;
8268
8269 ssl->in_hdr = NULL;
8270 ssl->in_ctr = NULL;
8271 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008272 ssl->in_msg = NULL;
8273
8274 ssl->out_hdr = NULL;
8275 ssl->out_ctr = NULL;
8276 ssl->out_len = NULL;
8277 ssl->out_iv = NULL;
8278 ssl->out_msg = NULL;
8279
k-stachowiak9f7798e2018-07-31 16:52:32 +02008280 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008281}
8282
8283/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008284 * Reset an initialized and used SSL context for re-use while retaining
8285 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008286 *
8287 * If partial is non-zero, keep data in the input buffer and client ID.
8288 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008289 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008290static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008291{
Paul Bakker48916f92012-09-16 19:57:18 +00008292 int ret;
8293
Hanno Becker7e772132018-08-10 12:38:21 +01008294#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8295 !defined(MBEDTLS_SSL_SRV_C)
8296 ((void) partial);
8297#endif
8298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008300
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008301 /* Cancel any possibly running timer */
8302 ssl_set_timer( ssl, 0 );
8303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304#if defined(MBEDTLS_SSL_RENEGOTIATION)
8305 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008306 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008307
8308 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8310 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008313
Paul Bakker7eb013f2011-10-06 12:37:39 +00008314 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008315 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008316
8317 ssl->in_msgtype = 0;
8318 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008319#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008320 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008321 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008324 ssl_dtls_replay_reset( ssl );
8325#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008326
8327 ssl->in_hslen = 0;
8328 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008329
8330 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008331
8332 ssl->out_msgtype = 0;
8333 ssl->out_msglen = 0;
8334 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008335#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8336 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008337 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008338#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008339
Hanno Becker19859472018-08-06 09:40:20 +01008340 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8341
Paul Bakker48916f92012-09-16 19:57:18 +00008342 ssl->transform_in = NULL;
8343 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008344
Hanno Becker78640902018-08-13 16:35:15 +01008345 ssl->session_in = NULL;
8346 ssl->session_out = NULL;
8347
Angus Grattond8213d02016-05-25 20:56:48 +10008348 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008349
8350#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008351 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008352#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8353 {
8354 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008355 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008356 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8359 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8362 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8365 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008366 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008367 }
8368#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008369
Paul Bakker48916f92012-09-16 19:57:18 +00008370 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 mbedtls_ssl_transform_free( ssl->transform );
8373 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008374 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008375 }
Paul Bakker48916f92012-09-16 19:57:18 +00008376
Paul Bakkerc0463502013-02-14 11:19:38 +01008377 if( ssl->session )
8378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379 mbedtls_ssl_session_free( ssl->session );
8380 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008381 ssl->session = NULL;
8382 }
8383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008385 ssl->alpn_chosen = NULL;
8386#endif
8387
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008388#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008389#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008390 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008391#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008392 {
8393 mbedtls_free( ssl->cli_id );
8394 ssl->cli_id = NULL;
8395 ssl->cli_id_len = 0;
8396 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008397#endif
8398
Paul Bakker48916f92012-09-16 19:57:18 +00008399 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8400 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008401
8402 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008403}
8404
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008405/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008406 * Reset an initialized and used SSL context for re-use while retaining
8407 * all application-set variables, function pointers and data.
8408 */
8409int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8410{
8411 return( ssl_session_reset_int( ssl, 0 ) );
8412}
8413
8414/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008415 * SSL set accessors
8416 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008417#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008418void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008419{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008420 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008421}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008422#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008423
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008424void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008425{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008426 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008427}
8428
Hanno Becker7f376f42019-06-12 16:20:48 +01008429#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8430 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008431void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008432{
Hanno Becker7f376f42019-06-12 16:20:48 +01008433 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008434}
Hanno Becker7f376f42019-06-12 16:20:48 +01008435#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008436
Hanno Beckerde671542019-06-12 16:30:46 +01008437#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8438 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8439void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8440 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008442 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008443}
Hanno Beckerde671542019-06-12 16:30:46 +01008444#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008447
Hanno Becker1841b0a2018-08-24 11:13:57 +01008448void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8449 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008450{
8451 ssl->disable_datagram_packing = !allow_packing;
8452}
8453
Hanno Becker1f835fa2019-06-13 10:14:59 +01008454#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8455 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008456void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8457 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008458{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008459 conf->hs_timeout_min = min;
8460 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008461}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008462#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8463 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8464void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8465 uint32_t min, uint32_t max )
8466{
8467 ((void) conf);
8468 ((void) min);
8469 ((void) max);
8470}
8471#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8472 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8473
8474#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008475
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008476void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008477{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008478#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8479 conf->authmode = authmode;
8480#else
8481 ((void) conf);
8482 ((void) authmode);
8483#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008484}
8485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008487void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008488 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008489 void *p_vrfy )
8490{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008491 conf->f_vrfy = f_vrfy;
8492 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008493}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008495
Hanno Beckerece325c2019-06-13 15:39:27 +01008496#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008497void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008498 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008499 void *p_rng )
8500{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008501 conf->f_rng = f_rng;
8502 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008503}
Hanno Beckerece325c2019-06-13 15:39:27 +01008504#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008505
Hanno Becker14a4a442019-07-02 17:00:34 +01008506#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008507void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008508 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008509 void *p_dbg )
8510{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008511 conf->f_dbg = f_dbg;
8512 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008513}
Hanno Becker14a4a442019-07-02 17:00:34 +01008514#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008515
Hanno Beckera58a8962019-06-13 16:11:15 +01008516#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8517 !defined(MBEDTLS_SSL_CONF_SEND) && \
8518 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008519void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008520 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008521 mbedtls_ssl_send_t *f_send,
8522 mbedtls_ssl_recv_t *f_recv,
8523 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008524{
Hanno Beckera58a8962019-06-13 16:11:15 +01008525 ssl->p_bio = p_bio;
8526 ssl->f_send = f_send;
8527 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008528 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008529}
Hanno Beckera58a8962019-06-13 16:11:15 +01008530#else
8531void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8532 void *p_bio )
8533{
8534 ssl->p_bio = p_bio;
8535}
8536#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008537
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008538#if defined(MBEDTLS_SSL_PROTO_DTLS)
8539void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8540{
8541 ssl->mtu = mtu;
8542}
8543#endif
8544
Hanno Becker1f835fa2019-06-13 10:14:59 +01008545#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008546void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008547{
8548 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008549}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008550#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008551
Hanno Becker0ae6b242019-06-13 16:45:36 +01008552#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8553 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008554void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8555 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008556 mbedtls_ssl_set_timer_t *f_set_timer,
8557 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008558{
8559 ssl->p_timer = p_timer;
8560 ssl->f_set_timer = f_set_timer;
8561 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008562 /* Make sure we start with no timer running */
8563 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008564}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008565#else
8566void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8567 void *p_timer )
8568{
8569 ssl->p_timer = p_timer;
8570 /* Make sure we start with no timer running */
8571 ssl_set_timer( ssl, 0 );
8572}
8573#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008574
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008575#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008576void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008577 void *p_cache,
8578 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8579 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008580{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008581 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008582 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008583 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008584}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008585#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008586
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008587#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008588int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008589{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008590 int ret;
8591
8592 if( ssl == NULL ||
8593 session == NULL ||
8594 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008595 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008598 }
8599
Hanno Becker58fccf22019-02-06 14:30:46 +00008600 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8601 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008602 return( ret );
8603
Paul Bakker0a597072012-09-25 21:55:46 +00008604 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008605
8606 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008607}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008608#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008609
Hanno Becker73f4cb12019-06-27 13:51:07 +01008610#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008611void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008612 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008613{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008614 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8615 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8616 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8617 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008618}
8619
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008621 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008622 int major, int minor )
8623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008625 return;
8626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008628 return;
8629
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008630 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008631}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008632#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008635void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008636 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008637{
8638 conf->cert_profile = profile;
8639}
8640
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008641/* Append a new keycert entry to a (possibly empty) list */
8642static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8643 mbedtls_x509_crt *cert,
8644 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008645{
niisato8ee24222018-06-25 19:05:48 +09008646 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008647
niisato8ee24222018-06-25 19:05:48 +09008648 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8649 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008650 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008651
niisato8ee24222018-06-25 19:05:48 +09008652 new_cert->cert = cert;
8653 new_cert->key = key;
8654 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008655
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008656 /* Update head is the list was null, else add to the end */
8657 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008658 {
niisato8ee24222018-06-25 19:05:48 +09008659 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008660 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008661 else
8662 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008663 mbedtls_ssl_key_cert *cur = *head;
8664 while( cur->next != NULL )
8665 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008666 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008667 }
8668
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008669 return( 0 );
8670}
8671
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008672int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008673 mbedtls_x509_crt *own_cert,
8674 mbedtls_pk_context *pk_key )
8675{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008676 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008677}
8678
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008679void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008680 mbedtls_x509_crt *ca_chain,
8681 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008682{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008683 conf->ca_chain = ca_chain;
8684 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008687
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008688#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8689int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8690 mbedtls_x509_crt *own_cert,
8691 mbedtls_pk_context *pk_key )
8692{
8693 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8694 own_cert, pk_key ) );
8695}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008696
8697void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8698 mbedtls_x509_crt *ca_chain,
8699 mbedtls_x509_crl *ca_crl )
8700{
8701 ssl->handshake->sni_ca_chain = ca_chain;
8702 ssl->handshake->sni_ca_crl = ca_crl;
8703}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008704
8705void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8706 int authmode )
8707{
8708 ssl->handshake->sni_authmode = authmode;
8709}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008710#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8711
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008712#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008713/*
8714 * Set EC J-PAKE password for current handshake
8715 */
8716int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8717 const unsigned char *pw,
8718 size_t pw_len )
8719{
8720 mbedtls_ecjpake_role role;
8721
Janos Follath8eb64132016-06-03 15:40:57 +01008722 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008723 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8724
Hanno Becker2d9623f2019-06-13 12:07:05 +01008725 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008726 role = MBEDTLS_ECJPAKE_SERVER;
8727 else
8728 role = MBEDTLS_ECJPAKE_CLIENT;
8729
8730 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8731 role,
8732 MBEDTLS_MD_SHA256,
8733 MBEDTLS_ECP_DP_SECP256R1,
8734 pw, pw_len ) );
8735}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008736#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008738#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008739int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008740 const unsigned char *psk, size_t psk_len,
8741 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008742{
Paul Bakker6db455e2013-09-18 17:29:31 +02008743 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008748
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008749 /* Identity len will be encoded on two bytes */
8750 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008751 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008752 {
8753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8754 }
8755
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008756 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008757 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008758 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008759
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008760 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008761 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008762 conf->psk_len = 0;
8763 }
8764 if( conf->psk_identity != NULL )
8765 {
8766 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008767 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008768 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008769 }
8770
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008771 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8772 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008773 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008774 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008775 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008776 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008777 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008779 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008780
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008781 conf->psk_len = psk_len;
8782 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008783
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008784 memcpy( conf->psk, psk, conf->psk_len );
8785 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008786
8787 return( 0 );
8788}
8789
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008790int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8791 const unsigned char *psk, size_t psk_len )
8792{
8793 if( psk == NULL || ssl->handshake == NULL )
8794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8795
8796 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8797 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8798
8799 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008800 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008801 mbedtls_platform_zeroize( ssl->handshake->psk,
8802 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008803 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008804 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008805 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008806
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008807 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008808 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008809
8810 ssl->handshake->psk_len = psk_len;
8811 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8812
8813 return( 0 );
8814}
8815
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008816void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008818 size_t),
8819 void *p_psk )
8820{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008821 conf->f_psk = f_psk;
8822 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008823}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008824#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008825
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008826#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008827
8828#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008829int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008830{
8831 int ret;
8832
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008833 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8834 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8835 {
8836 mbedtls_mpi_free( &conf->dhm_P );
8837 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008838 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008840
8841 return( 0 );
8842}
Hanno Becker470a8c42017-10-04 15:28:46 +01008843#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008844
Hanno Beckera90658f2017-10-04 15:29:08 +01008845int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8846 const unsigned char *dhm_P, size_t P_len,
8847 const unsigned char *dhm_G, size_t G_len )
8848{
8849 int ret;
8850
8851 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8852 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8853 {
8854 mbedtls_mpi_free( &conf->dhm_P );
8855 mbedtls_mpi_free( &conf->dhm_G );
8856 return( ret );
8857 }
8858
8859 return( 0 );
8860}
Paul Bakker5121ce52009-01-03 21:22:43 +00008861
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008862int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008863{
8864 int ret;
8865
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008866 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8867 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8868 {
8869 mbedtls_mpi_free( &conf->dhm_P );
8870 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008871 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008872 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008873
8874 return( 0 );
8875}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008876#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008877
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008878#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8879/*
8880 * Set the minimum length for Diffie-Hellman parameters
8881 */
8882void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8883 unsigned int bitlen )
8884{
8885 conf->dhm_min_bitlen = bitlen;
8886}
8887#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8888
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008889#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008890/*
8891 * Set allowed/preferred hashes for handshake signatures
8892 */
8893void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8894 const int *hashes )
8895{
Hanno Becker56595f42019-06-19 16:31:38 +01008896#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008897 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008898#else
8899 ((void) conf);
8900 ((void) hashes);
8901#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008902}
Hanno Becker947194e2017-04-07 13:25:49 +01008903#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008904
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008905#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008906#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008907/*
8908 * Set the allowed elliptic curves
8909 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008910void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008911 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008912{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008913 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008914}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008915#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008916#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008917
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008918#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008919int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008920{
Hanno Becker947194e2017-04-07 13:25:49 +01008921 /* Initialize to suppress unnecessary compiler warning */
8922 size_t hostname_len = 0;
8923
8924 /* Check if new hostname is valid before
8925 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008926 if( hostname != NULL )
8927 {
8928 hostname_len = strlen( hostname );
8929
8930 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8932 }
8933
8934 /* Now it's clear that we will overwrite the old hostname,
8935 * so we can free it safely */
8936
8937 if( ssl->hostname != NULL )
8938 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008939 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008940 mbedtls_free( ssl->hostname );
8941 }
8942
8943 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008944
Paul Bakker5121ce52009-01-03 21:22:43 +00008945 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008946 {
8947 ssl->hostname = NULL;
8948 }
8949 else
8950 {
8951 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008952 if( ssl->hostname == NULL )
8953 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008954
Hanno Becker947194e2017-04-07 13:25:49 +01008955 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008956
Hanno Becker947194e2017-04-07 13:25:49 +01008957 ssl->hostname[hostname_len] = '\0';
8958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008959
8960 return( 0 );
8961}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008962#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008963
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008964#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008965void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008966 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008967 const unsigned char *, size_t),
8968 void *p_sni )
8969{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008970 conf->f_sni = f_sni;
8971 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008972}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008975#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008976int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008977{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008978 size_t cur_len, tot_len;
8979 const char **p;
8980
8981 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008982 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8983 * MUST NOT be truncated."
8984 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008985 */
8986 tot_len = 0;
8987 for( p = protos; *p != NULL; p++ )
8988 {
8989 cur_len = strlen( *p );
8990 tot_len += cur_len;
8991
8992 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008993 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008994 }
8995
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008996 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008997
8998 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008999}
9000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009002{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009003 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009005#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009006
Hanno Becker33b9b252019-07-05 11:23:25 +01009007#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9008 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9009void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9010 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009011{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009012 conf->max_major_ver = major;
9013 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009014}
Hanno Becker33b9b252019-07-05 11:23:25 +01009015#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9016 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009017
Hanno Becker33b9b252019-07-05 11:23:25 +01009018#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9019 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9020void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9021 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009022{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009023 conf->min_major_ver = major;
9024 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009025}
Hanno Becker33b9b252019-07-05 11:23:25 +01009026#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9027 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009030void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009031{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009032 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009033}
9034#endif
9035
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009036#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009037void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9038 char cert_req_ca_list )
9039{
9040 conf->cert_req_ca_list = cert_req_ca_list;
9041}
9042#endif
9043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009045void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009046{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009047 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009048}
9049#endif
9050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009052#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009053void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009055 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009056}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009057#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9058#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009059void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009060 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009061{
9062 conf->enforce_extended_master_secret = ems_enf;
9063}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009064#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009065#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009066
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009067#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009068void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009069{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009070 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009071}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009072#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009074#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009075int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009078 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009081 }
9082
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009083 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009084
9085 return( 0 );
9086}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009087#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009090void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009092 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009097void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009098{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009099 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009100}
9101#endif
9102
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009103#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009104void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009106 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009107}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009108#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009111void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009112{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009113 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009114}
9115
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009116void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009117{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009118 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009119}
9120
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009122 const unsigned char period[8] )
9123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009124 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009128#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009129#if defined(MBEDTLS_SSL_CLI_C)
9130void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009131{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009132 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009133}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009134#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009135
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009136#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009137void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9138 mbedtls_ssl_ticket_write_t *f_ticket_write,
9139 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9140 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009141{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009142 conf->f_ticket_write = f_ticket_write;
9143 conf->f_ticket_parse = f_ticket_parse;
9144 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009145}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009146#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009147#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009148
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009149#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9150void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9151 mbedtls_ssl_export_keys_t *f_export_keys,
9152 void *p_export_keys )
9153{
9154 conf->f_export_keys = f_export_keys;
9155 conf->p_export_keys = p_export_keys;
9156}
9157#endif
9158
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009159#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009160void mbedtls_ssl_conf_async_private_cb(
9161 mbedtls_ssl_config *conf,
9162 mbedtls_ssl_async_sign_t *f_async_sign,
9163 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9164 mbedtls_ssl_async_resume_t *f_async_resume,
9165 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009166 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009167{
9168 conf->f_async_sign_start = f_async_sign;
9169 conf->f_async_decrypt_start = f_async_decrypt;
9170 conf->f_async_resume = f_async_resume;
9171 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009172 conf->p_async_config_data = async_config_data;
9173}
9174
Gilles Peskine8f97af72018-04-26 11:46:10 +02009175void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9176{
9177 return( conf->p_async_config_data );
9178}
9179
Gilles Peskine1febfef2018-04-30 11:54:39 +02009180void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009181{
9182 if( ssl->handshake == NULL )
9183 return( NULL );
9184 else
9185 return( ssl->handshake->user_async_ctx );
9186}
9187
Gilles Peskine1febfef2018-04-30 11:54:39 +02009188void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009189 void *ctx )
9190{
9191 if( ssl->handshake != NULL )
9192 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009193}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009194#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009195
Paul Bakker5121ce52009-01-03 21:22:43 +00009196/*
9197 * SSL get accessors
9198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009200{
9201 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9202}
9203
Hanno Becker8b170a02017-10-10 11:51:19 +01009204int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9205{
9206 /*
9207 * Case A: We're currently holding back
9208 * a message for further processing.
9209 */
9210
9211 if( ssl->keep_current_message == 1 )
9212 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009214 return( 1 );
9215 }
9216
9217 /*
9218 * Case B: Further records are pending in the current datagram.
9219 */
9220
9221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009222 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009223 ssl->in_left > ssl->next_record_offset )
9224 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009226 return( 1 );
9227 }
9228#endif /* MBEDTLS_SSL_PROTO_DTLS */
9229
9230 /*
9231 * Case C: A handshake message is being processed.
9232 */
9233
Hanno Becker8b170a02017-10-10 11:51:19 +01009234 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9235 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009237 return( 1 );
9238 }
9239
9240 /*
9241 * Case D: An application data message is being processed
9242 */
9243 if( ssl->in_offt != NULL )
9244 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009246 return( 1 );
9247 }
9248
9249 /*
9250 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009251 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009252 * we implement support for multiple alerts in single records.
9253 */
9254
9255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9256 return( 0 );
9257}
9258
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009259uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009260{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009261 if( ssl->session != NULL )
9262 return( ssl->session->verify_result );
9263
9264 if( ssl->session_negotiate != NULL )
9265 return( ssl->session_negotiate->verify_result );
9266
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009267 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009268}
9269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009271{
Hanno Beckere02758c2019-06-26 15:31:31 +01009272 int suite;
9273
Paul Bakker926c8e42013-03-06 10:23:34 +01009274 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009275 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009276
Hanno Beckere02758c2019-06-26 15:31:31 +01009277 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9278 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009279}
9280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009281const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009284 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009285 {
Hanno Becker2881d802019-05-22 14:44:53 +01009286 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009289 return( "DTLSv1.0" );
9290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009292 return( "DTLSv1.2" );
9293
9294 default:
9295 return( "unknown (DTLS)" );
9296 }
9297 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009298 MBEDTLS_SSL_TRANSPORT_ELSE
9299#endif /* MBEDTLS_SSL_PROTO_DTLS */
9300#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009301 {
Hanno Becker2881d802019-05-22 14:44:53 +01009302 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009303 {
9304 case MBEDTLS_SSL_MINOR_VERSION_0:
9305 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009306
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009307 case MBEDTLS_SSL_MINOR_VERSION_1:
9308 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009309
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009310 case MBEDTLS_SSL_MINOR_VERSION_2:
9311 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009312
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009313 case MBEDTLS_SSL_MINOR_VERSION_3:
9314 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009315
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009316 default:
9317 return( "unknown" );
9318 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009319 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009320#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321}
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009324{
Hanno Becker3136ede2018-08-17 15:28:19 +01009325 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009327
Hanno Becker43395762019-05-03 14:46:38 +01009328 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9329
Hanno Becker78640902018-08-13 16:35:15 +01009330 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009331 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333#if defined(MBEDTLS_ZLIB_SUPPORT)
9334 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9335 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009336#endif
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009339 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009340#if defined(MBEDTLS_GCM_C) || \
9341 defined(MBEDTLS_CCM_C) || \
9342 defined(MBEDTLS_CHACHAPOLY_C)
9343#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009345#endif
9346#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009348#endif
9349#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009350 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009351#endif
9352 transform_expansion =
9353 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009354 break;
9355
Hanno Beckera9d5c452019-07-25 16:47:12 +01009356#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9357 MBEDTLS_CHACHAPOLY_C */
9358
9359#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9360 case MBEDTLS_MODE_STREAM:
9361 transform_expansion = transform->maclen;
9362 break;
9363#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9364
9365#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009367 {
9368 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009369
9370 block_size = mbedtls_cipher_get_block_size(
9371 &transform->cipher_ctx_enc );
9372
Hanno Becker3136ede2018-08-17 15:28:19 +01009373 /* Expansion due to the addition of the MAC. */
9374 transform_expansion += transform->maclen;
9375
9376 /* Expansion due to the addition of CBC padding;
9377 * Theoretically up to 256 bytes, but we never use
9378 * more than the block size of the underlying cipher. */
9379 transform_expansion += block_size;
9380
9381 /* For TLS 1.1 or higher, an explicit IV is added
9382 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009383#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009384 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009385 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009386#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009387
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009388 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009389 }
9390#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009391
9392 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009394 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009395 }
9396
Hanno Beckera5a2b082019-05-15 14:03:01 +01009397#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009398 if( transform->out_cid_len != 0 )
9399 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009400#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009401
Hanno Becker43395762019-05-03 14:46:38 +01009402 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009403}
9404
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009405#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9406size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9407{
9408 size_t max_len;
9409
9410 /*
9411 * Assume mfl_code is correct since it was checked when set
9412 */
Angus Grattond8213d02016-05-25 20:56:48 +10009413 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009414
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009415 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009416 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009417 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009418 {
Angus Grattond8213d02016-05-25 20:56:48 +10009419 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009420 }
9421
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009422 /* During a handshake, use the value being negotiated */
9423 if( ssl->session_negotiate != NULL &&
9424 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9425 {
9426 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9427 }
9428
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009429 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009430}
9431#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9432
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009433#if defined(MBEDTLS_SSL_PROTO_DTLS)
9434static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9435{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009436 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009437 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009438 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9439 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9440 return ( 0 );
9441
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009442 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9443 return( ssl->mtu );
9444
9445 if( ssl->mtu == 0 )
9446 return( ssl->handshake->mtu );
9447
9448 return( ssl->mtu < ssl->handshake->mtu ?
9449 ssl->mtu : ssl->handshake->mtu );
9450}
9451#endif /* MBEDTLS_SSL_PROTO_DTLS */
9452
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009453int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9454{
9455 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9456
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009457#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9458 !defined(MBEDTLS_SSL_PROTO_DTLS)
9459 (void) ssl;
9460#endif
9461
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009462#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9463 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9464
9465 if( max_len > mfl )
9466 max_len = mfl;
9467#endif
9468
9469#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009470 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009471 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009472 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009473 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9474 const size_t overhead = (size_t) ret;
9475
9476 if( ret < 0 )
9477 return( ret );
9478
9479 if( mtu <= overhead )
9480 {
9481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9482 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9483 }
9484
9485 if( max_len > mtu - overhead )
9486 max_len = mtu - overhead;
9487 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009489
Hanno Becker0defedb2018-08-10 12:35:02 +01009490#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9491 !defined(MBEDTLS_SSL_PROTO_DTLS)
9492 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009493#endif
9494
9495 return( (int) max_len );
9496}
9497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498#if defined(MBEDTLS_X509_CRT_PARSE_C)
9499const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009500{
9501 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009502 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009503
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009504#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009505 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009506#else
9507 return( NULL );
9508#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009513int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9514 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009515{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009516 if( ssl == NULL ||
9517 dst == NULL ||
9518 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009519 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009522 }
9523
Hanno Becker58fccf22019-02-06 14:30:46 +00009524 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009527
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009528const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9529{
9530 if( ssl == NULL )
9531 return( NULL );
9532
9533 return( ssl->session );
9534}
9535
Paul Bakker5121ce52009-01-03 21:22:43 +00009536/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009537 * Define ticket header determining Mbed TLS version
9538 * and structure of the ticket.
9539 */
9540
Hanno Becker41527622019-05-16 12:50:45 +01009541/*
Hanno Becker26829e92019-05-28 14:30:45 +01009542 * Define bitflag determining compile-time settings influencing
9543 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009544 */
9545
Hanno Becker26829e92019-05-28 14:30:45 +01009546#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009547#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009548#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009549#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009550#endif /* MBEDTLS_HAVE_TIME */
9551
9552#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009553#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009554#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009555#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009556#endif /* MBEDTLS_X509_CRT_PARSE_C */
9557
9558#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009559#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009560#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009561#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009562#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9563
9564#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009565#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009566#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009567#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009568#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9569
9570#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009571#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009572#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009573#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009574#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9575
9576#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009577#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009578#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009579#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009580#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9581
Hanno Becker41527622019-05-16 12:50:45 +01009582#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9583#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9584#else
9585#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9586#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9587
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009588#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9589#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9590#else
9591#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9592#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9593
Hanno Becker88440552019-07-03 14:16:13 +01009594#if defined(MBEDTLS_ZLIB_SUPPORT)
9595#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9596#else
9597#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9598#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9599
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009600#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9601#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9602#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9603#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9604#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9605#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9606#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009607#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009608#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009609
Hanno Becker26829e92019-05-28 14:30:45 +01009610#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009611 ( (uint16_t) ( \
9612 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9613 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9614 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9615 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9616 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9617 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009618 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009619 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009620 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009621
Hanno Becker557fe9f2019-05-16 12:41:07 +01009622static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009623 MBEDTLS_VERSION_MAJOR,
9624 MBEDTLS_VERSION_MINOR,
9625 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009626 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9627 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009628};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009629
9630/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009631 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009632 * (in the presentation language of TLS, RFC 8446 section 3)
9633 *
Hanno Becker26829e92019-05-28 14:30:45 +01009634 * opaque mbedtls_version[3]; // major, minor, patch
9635 * opaque session_format[2]; // version-specific 16-bit field determining
9636 * // the format of the remaining
9637 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009638 *
9639 * Note: When updating the format, remember to keep
9640 * these version+format bytes.
9641 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009642 * // In this version, `session_format` determines
9643 * // the setting of those compile-time
9644 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009645 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009646 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009647 * uint8 ciphersuite[2]; // defined by the standard
9648 * uint8 compression; // 0 or 1
9649 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009650 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009651 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009652 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009653 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9654 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9655 * case disabled: uint8_t peer_cert_digest_type;
9656 * opaque peer_cert_digest<0..2^8-1>;
9657 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009658 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009659 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009660 * uint8 mfl_code; // up to 255 according to standard
9661 * uint8 trunc_hmac; // 0 or 1
9662 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009663 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009664 * The order is the same as in the definition of the structure, except
9665 * verify_result is put before peer_cert so that all mandatory fields come
9666 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009667 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009668static int ssl_session_save( const mbedtls_ssl_session *session,
9669 unsigned char omit_header,
9670 unsigned char *buf,
9671 size_t buf_len,
9672 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009673{
9674 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009675 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009676#if defined(MBEDTLS_HAVE_TIME)
9677 uint64_t start;
9678#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009679#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009680#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009681 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009682#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009683#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009684
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009685 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009686 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009687 /*
9688 * Add version identifier
9689 */
9690
9691 used += sizeof( ssl_serialized_session_header );
9692
9693 if( used <= buf_len )
9694 {
9695 memcpy( p, ssl_serialized_session_header,
9696 sizeof( ssl_serialized_session_header ) );
9697 p += sizeof( ssl_serialized_session_header );
9698 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009699 }
9700
9701 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009702 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009703 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009704#if defined(MBEDTLS_HAVE_TIME)
9705 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009706
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009707 if( used <= buf_len )
9708 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009709 start = (uint64_t) session->start;
9710
9711 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9712 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9713 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9714 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9715 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9716 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9717 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9718 *p++ = (unsigned char)( ( start ) & 0xFF );
9719 }
9720#endif /* MBEDTLS_HAVE_TIME */
9721
9722 /*
9723 * Basic mandatory fields
9724 */
Hanno Becker88440552019-07-03 14:16:13 +01009725 {
9726 size_t const ciphersuite_len = 2;
9727#if defined(MBEDTLS_ZLIB_SUPPORT)
9728 size_t const compression_len = 1;
9729#else
9730 size_t const compression_len = 0;
9731#endif
9732 size_t const id_len_len = 1;
9733 size_t const id_len = 32;
9734 size_t const master_len = 48;
9735 size_t const verif_result_len = 4;
9736
9737 size_t const basic_len =
9738 ciphersuite_len +
9739 compression_len +
9740 id_len_len +
9741 id_len +
9742 master_len +
9743 verif_result_len;
9744
9745 used += basic_len;
9746 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009747
9748 if( used <= buf_len )
9749 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009750 const int ciphersuite =
9751 mbedtls_ssl_session_get_ciphersuite( session );
9752 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9753 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009754
Hanno Becker88440552019-07-03 14:16:13 +01009755#if defined(MBEDTLS_ZLIB_SUPPORT)
9756 *p++ = (unsigned char)(
9757 mbedtls_ssl_session_get_compression( session ) );
9758#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009759
9760 *p++ = (unsigned char)( session->id_len & 0xFF );
9761 memcpy( p, session->id, 32 );
9762 p += 32;
9763
9764 memcpy( p, session->master, 48 );
9765 p += 48;
9766
9767 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9768 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9769 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9770 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009771 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009772
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009773 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009774 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009775 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009776#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009777#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009778 if( session->peer_cert == NULL )
9779 cert_len = 0;
9780 else
9781 cert_len = session->peer_cert->raw.len;
9782
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009783 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009784
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009785 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009786 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009787 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9788 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9789 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9790
9791 if( session->peer_cert != NULL )
9792 {
9793 memcpy( p, session->peer_cert->raw.p, cert_len );
9794 p += cert_len;
9795 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009796 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009797
Hanno Becker5882dd02019-06-06 16:25:57 +01009798#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009799 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009800 if( session->peer_cert_digest != NULL )
9801 {
9802 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9803 if( used <= buf_len )
9804 {
9805 *p++ = (unsigned char) session->peer_cert_digest_type;
9806 *p++ = (unsigned char) session->peer_cert_digest_len;
9807 memcpy( p, session->peer_cert_digest,
9808 session->peer_cert_digest_len );
9809 p += session->peer_cert_digest_len;
9810 }
9811 }
9812 else
9813 {
9814 used += 2;
9815 if( used <= buf_len )
9816 {
9817 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9818 *p++ = 0;
9819 }
9820 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009821#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009822#endif /* MBEDTLS_X509_CRT_PARSE_C */
9823
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009824 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009825 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009826 */
9827#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009828 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009829
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009830 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009831 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009832 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9833 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9834 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9835
9836 if( session->ticket != NULL )
9837 {
9838 memcpy( p, session->ticket, session->ticket_len );
9839 p += session->ticket_len;
9840 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009841
9842 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9843 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9844 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9845 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009846 }
9847#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9848
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009849 /*
9850 * Misc extension-related info
9851 */
9852#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9853 used += 1;
9854
9855 if( used <= buf_len )
9856 *p++ = session->mfl_code;
9857#endif
9858
9859#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9860 used += 1;
9861
9862 if( used <= buf_len )
9863 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9864#endif
9865
9866#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9867 used += 1;
9868
9869 if( used <= buf_len )
9870 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9871#endif
9872
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009873 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009874 *olen = used;
9875
9876 if( used > buf_len )
9877 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009878
9879 return( 0 );
9880}
9881
9882/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009883 * Public wrapper for ssl_session_save()
9884 */
9885int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9886 unsigned char *buf,
9887 size_t buf_len,
9888 size_t *olen )
9889{
9890 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9891}
9892
9893/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009894 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009895 *
9896 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009897 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009898 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009899static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009900 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009901 const unsigned char *buf,
9902 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009903{
9904 const unsigned char *p = buf;
9905 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009906 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009907#if defined(MBEDTLS_HAVE_TIME)
9908 uint64_t start;
9909#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009910#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009911#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009912 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009913#endif
9914#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009915
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009916 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009917 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009918 /*
9919 * Check version identifier
9920 */
9921
9922 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9923 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9924
9925 if( memcmp( p, ssl_serialized_session_header,
9926 sizeof( ssl_serialized_session_header ) ) != 0 )
9927 {
9928 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9929 }
9930 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009931 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009932
9933 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009934 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009935 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009936#if defined(MBEDTLS_HAVE_TIME)
9937 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9939
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009940 start = ( (uint64_t) p[0] << 56 ) |
9941 ( (uint64_t) p[1] << 48 ) |
9942 ( (uint64_t) p[2] << 40 ) |
9943 ( (uint64_t) p[3] << 32 ) |
9944 ( (uint64_t) p[4] << 24 ) |
9945 ( (uint64_t) p[5] << 16 ) |
9946 ( (uint64_t) p[6] << 8 ) |
9947 ( (uint64_t) p[7] );
9948 p += 8;
9949
9950 session->start = (time_t) start;
9951#endif /* MBEDTLS_HAVE_TIME */
9952
9953 /*
9954 * Basic mandatory fields
9955 */
Hanno Becker88440552019-07-03 14:16:13 +01009956 {
9957 size_t const ciphersuite_len = 2;
9958#if defined(MBEDTLS_ZLIB_SUPPORT)
9959 size_t const compression_len = 1;
9960#else
9961 size_t const compression_len = 0;
9962#endif
9963 size_t const id_len_len = 1;
9964 size_t const id_len = 32;
9965 size_t const master_len = 48;
9966 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009967
Hanno Becker88440552019-07-03 14:16:13 +01009968 size_t const basic_len =
9969 ciphersuite_len +
9970 compression_len +
9971 id_len_len +
9972 id_len +
9973 master_len +
9974 verif_result_len;
9975
9976 if( basic_len > (size_t)( end - p ) )
9977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9978 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009979
Hanno Beckere02758c2019-06-26 15:31:31 +01009980 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009981 p += 2;
9982
Hanno Becker73f4cb12019-06-27 13:51:07 +01009983#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +01009984 session->ciphersuite = ciphersuite;
9985#else
9986 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +01009987 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +01009988 {
9989 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9990 }
9991#endif
9992
Hanno Becker88440552019-07-03 14:16:13 +01009993#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009994 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +01009995#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009996
9997 session->id_len = *p++;
9998 memcpy( session->id, p, 32 );
9999 p += 32;
10000
10001 memcpy( session->master, p, 48 );
10002 p += 48;
10003
10004 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10005 ( (uint32_t) p[1] << 16 ) |
10006 ( (uint32_t) p[2] << 8 ) |
10007 ( (uint32_t) p[3] );
10008 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010009
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010010 /* Immediately clear invalid pointer values that have been read, in case
10011 * we exit early before we replaced them with valid ones. */
10012#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010013#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010014 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010015#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010016 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010017#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010018#endif
10019#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10020 session->ticket = NULL;
10021#endif
10022
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010023 /*
10024 * Peer certificate
10025 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010026#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010027#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010028 if( 3 > (size_t)( end - p ) )
10029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10030
10031 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10032 p += 3;
10033
10034 if( cert_len == 0 )
10035 {
10036 session->peer_cert = NULL;
10037 }
10038 else
10039 {
10040 int ret;
10041
10042 if( cert_len > (size_t)( end - p ) )
10043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10044
10045 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10046
10047 if( session->peer_cert == NULL )
10048 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10049
10050 mbedtls_x509_crt_init( session->peer_cert );
10051
10052 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10053 p, cert_len ) ) != 0 )
10054 {
10055 mbedtls_x509_crt_free( session->peer_cert );
10056 mbedtls_free( session->peer_cert );
10057 session->peer_cert = NULL;
10058 return( ret );
10059 }
10060
10061 p += cert_len;
10062 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010063#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010064 /* Deserialize CRT digest from the end of the ticket. */
10065 if( 2 > (size_t)( end - p ) )
10066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10067
10068 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10069 session->peer_cert_digest_len = (size_t) *p++;
10070
10071 if( session->peer_cert_digest_len != 0 )
10072 {
Hanno Becker2326d202019-06-06 14:54:55 +010010073 const mbedtls_md_info_t *md_info =
10074 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10075 if( md_info == NULL )
10076 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10077 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10078 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10079
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010080 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10081 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10082
10083 session->peer_cert_digest =
10084 mbedtls_calloc( 1, session->peer_cert_digest_len );
10085 if( session->peer_cert_digest == NULL )
10086 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10087
10088 memcpy( session->peer_cert_digest, p,
10089 session->peer_cert_digest_len );
10090 p += session->peer_cert_digest_len;
10091 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010092#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010093#endif /* MBEDTLS_X509_CRT_PARSE_C */
10094
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010095 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010096 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010097 */
10098#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10099 if( 3 > (size_t)( end - p ) )
10100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10101
10102 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10103 p += 3;
10104
10105 if( session->ticket_len != 0 )
10106 {
10107 if( session->ticket_len > (size_t)( end - p ) )
10108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10109
10110 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10111 if( session->ticket == NULL )
10112 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10113
10114 memcpy( session->ticket, p, session->ticket_len );
10115 p += session->ticket_len;
10116 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010117
10118 if( 4 > (size_t)( end - p ) )
10119 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10120
10121 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10122 ( (uint32_t) p[1] << 16 ) |
10123 ( (uint32_t) p[2] << 8 ) |
10124 ( (uint32_t) p[3] );
10125 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010126#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10127
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010128 /*
10129 * Misc extension-related info
10130 */
10131#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10132 if( 1 > (size_t)( end - p ) )
10133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10134
10135 session->mfl_code = *p++;
10136#endif
10137
10138#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10139 if( 1 > (size_t)( end - p ) )
10140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10141
10142 session->trunc_hmac = *p++;
10143#endif
10144
10145#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10146 if( 1 > (size_t)( end - p ) )
10147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10148
10149 session->encrypt_then_mac = *p++;
10150#endif
10151
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010152 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010153 if( p != end )
10154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10155
10156 return( 0 );
10157}
10158
10159/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010160 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010161 */
10162int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10163 const unsigned char *buf,
10164 size_t len )
10165{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010166 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010167
10168 if( ret != 0 )
10169 mbedtls_ssl_session_free( session );
10170
10171 return( ret );
10172}
10173
10174/*
Paul Bakker1961b702013-01-25 14:49:24 +010010175 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010180
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010181 if( ssl == NULL || ssl->conf == NULL )
10182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010185 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010189 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010191#endif
10192
Hanno Beckerb82350b2019-07-26 07:24:05 +010010193 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010194 return( ret );
10195}
10196
10197/*
10198 * Perform the SSL handshake
10199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010201{
10202 int ret = 0;
10203
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010204 if( ssl == NULL || ssl->conf == NULL )
10205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010212
10213 if( ret != 0 )
10214 break;
10215 }
10216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010218
10219 return( ret );
10220}
10221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222#if defined(MBEDTLS_SSL_RENEGOTIATION)
10223#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010224/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010225 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010228{
10229 int ret;
10230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010232
10233 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10235 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010236
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010237 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010238 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010239 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010240 return( ret );
10241 }
10242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010244
10245 return( 0 );
10246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010248
10249/*
10250 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 * - any side: calling mbedtls_ssl_renegotiate(),
10252 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10253 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010254 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010255 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010258static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010259{
10260 int ret;
10261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010263
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010264 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10265 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010266
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010267 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10268 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010270 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010272 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010273 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10274 MBEDTLS_SSL_IS_SERVER )
10275 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010276 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010277 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010278 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010279 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010280 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010281 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010282 }
10283#endif
10284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10286 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010288 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010291 return( ret );
10292 }
10293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010295
10296 return( 0 );
10297}
10298
10299/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010300 * Renegotiate current connection on client,
10301 * or request renegotiation on server
10302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010305 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010306
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010307 if( ssl == NULL || ssl->conf == NULL )
10308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010311 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010312 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010317 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010318
10319 /* Did we already try/start sending HelloRequest? */
10320 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010322
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010323 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010324 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010328 /*
10329 * On client, either start the renegotiation process or,
10330 * if already in progress, continue the handshake
10331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010336
10337 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010340 return( ret );
10341 }
10342 }
10343 else
10344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010348 return( ret );
10349 }
10350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010352
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010353 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010354}
10355
10356/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010357 * Check record counters and renegotiate if they're above the limit.
10358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010360{
Andres AG2196c7f2016-12-15 17:01:16 +000010361 size_t ep_len = ssl_ep_len( ssl );
10362 int in_ctr_cmp;
10363 int out_ctr_cmp;
10364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10366 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010367 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010368 {
10369 return( 0 );
10370 }
10371
Andres AG2196c7f2016-12-15 17:01:16 +000010372 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10373 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010374 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010375 ssl->conf->renego_period + ep_len, 8 - ep_len );
10376
10377 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010378 {
10379 return( 0 );
10380 }
10381
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010383 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010386
10387/*
10388 * Receive application data decrypted from the SSL layer
10389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010391{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010392 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010393 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010394
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010395 if( ssl == NULL || ssl->conf == NULL )
10396 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010401 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010404 return( ret );
10405
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010406 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010408 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010409 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010410 return( ret );
10411 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010412 }
10413#endif
10414
Hanno Becker4a810fb2017-05-24 16:27:30 +010010415 /*
10416 * Check if renegotiation is necessary and/or handshake is
10417 * in process. If yes, perform/continue, and fall through
10418 * if an unexpected packet is received while the client
10419 * is waiting for the ServerHello.
10420 *
10421 * (There is no equivalent to the last condition on
10422 * the server-side as it is not treated as within
10423 * a handshake while waiting for the ClientHello
10424 * after a renegotiation request.)
10425 */
10426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010428 ret = ssl_check_ctr_renegotiate( ssl );
10429 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10430 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010433 return( ret );
10434 }
10435#endif
10436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010440 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10441 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010444 return( ret );
10445 }
10446 }
10447
Hanno Beckere41158b2017-10-23 13:30:32 +010010448 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010449 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010450 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010451 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010452 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10453 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010454 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010455 ssl_set_timer( ssl,
10456 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010457 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010458
Hanno Becker327c93b2018-08-15 13:56:18 +010010459 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010460 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010461 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10462 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010463
Hanno Becker4a810fb2017-05-24 16:27:30 +010010464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10465 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010466 }
10467
10468 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010470 {
10471 /*
10472 * OpenSSL sends empty messages to randomize the IV
10473 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010474 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010477 return( 0 );
10478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010480 return( ret );
10481 }
10482 }
10483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010487
Hanno Becker4a810fb2017-05-24 16:27:30 +010010488 /*
10489 * - For client-side, expect SERVER_HELLO_REQUEST.
10490 * - For server-side, expect CLIENT_HELLO.
10491 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10492 */
10493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010495 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10496 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010498 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010501
10502 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010504 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010505 {
10506 continue;
10507 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010508 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010509#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010510#if defined(MBEDTLS_SSL_PROTO_TLS)
10511 {
10512 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10513 }
10514#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010515 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010516#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010517
Hanno Becker4a810fb2017-05-24 16:27:30 +010010518#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010519 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10520 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010521 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010524
10525 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010526#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010527 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010528 {
10529 continue;
10530 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010531 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010532#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010533#if defined(MBEDTLS_SSL_PROTO_TLS)
10534 {
10535 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10536 }
10537#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010538 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010539#endif /* MBEDTLS_SSL_SRV_C */
10540
Hanno Becker21df7f92017-10-17 11:03:26 +010010541#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010542 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010543 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10544 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010545 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010546 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10547 {
10548 /*
10549 * Accept renegotiation request
10550 */
Paul Bakker48916f92012-09-16 19:57:18 +000010551
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010552 /* DTLS clients need to know renego is server-initiated */
10553#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010554 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010555 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10556 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010557 {
10558 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10559 }
10560#endif
10561 ret = ssl_start_renegotiation( ssl );
10562 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10563 ret != 0 )
10564 {
10565 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10566 return( ret );
10567 }
10568 }
10569 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010570#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010571 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010572 /*
10573 * Refuse renegotiation
10574 */
10575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010576 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010578#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010579 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010580 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010581 /* SSLv3 does not have a "no_renegotiation" warning, so
10582 we send a fatal alert and abort the connection. */
10583 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10584 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10585 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010586 }
10587 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10589#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10590 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010591 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010592 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010593 ret = mbedtls_ssl_send_alert_message( ssl,
10594 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10595 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10596 if( ret != 0 )
10597 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010598 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010599 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010600#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10601 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010605 }
Paul Bakker48916f92012-09-16 19:57:18 +000010606 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010607
Hanno Becker90333da2017-10-10 11:27:13 +010010608 /* At this point, we don't know whether the renegotiation has been
10609 * completed or not. The cases to consider are the following:
10610 * 1) The renegotiation is complete. In this case, no new record
10611 * has been read yet.
10612 * 2) The renegotiation is incomplete because the client received
10613 * an application data record while awaiting the ServerHello.
10614 * 3) The renegotiation is incomplete because the client received
10615 * a non-handshake, non-application data message while awaiting
10616 * the ServerHello.
10617 * In each of these case, looping will be the proper action:
10618 * - For 1), the next iteration will read a new record and check
10619 * if it's application data.
10620 * - For 2), the loop condition isn't satisfied as application data
10621 * is present, hence continue is the same as break
10622 * - For 3), the loop condition is satisfied and read_record
10623 * will re-deliver the message that was held back by the client
10624 * when expecting the ServerHello.
10625 */
10626 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010627 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010628#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010630 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010631 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010632 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010633 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010636 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010637 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010638 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010639 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010640 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010641#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10644 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010647 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010648 }
10649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010650 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10653 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010654 }
10655
10656 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010657
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010658 /* We're going to return something now, cancel timer,
10659 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010661 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010662
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010663#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010664 /* If we requested renego but received AppData, resend HelloRequest.
10665 * Do it now, after setting in_offt, to avoid taking this branch
10666 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010667#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010668 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10669 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010671 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010672 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010675 return( ret );
10676 }
10677 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010679#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010680 }
10681
10682 n = ( len < ssl->in_msglen )
10683 ? len : ssl->in_msglen;
10684
10685 memcpy( buf, ssl->in_offt, n );
10686 ssl->in_msglen -= n;
10687
10688 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010689 {
10690 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010691 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010692 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010693 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010694 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010695 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010696 /* more data available */
10697 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010698 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010701
Paul Bakker23986e52011-04-24 08:57:21 +000010702 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010703}
10704
10705/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010706 * Send application data to be encrypted by the SSL layer, taking care of max
10707 * fragment length and buffer size.
10708 *
10709 * According to RFC 5246 Section 6.2.1:
10710 *
10711 * Zero-length fragments of Application data MAY be sent as they are
10712 * potentially useful as a traffic analysis countermeasure.
10713 *
10714 * Therefore, it is possible that the input message length is 0 and the
10715 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010716 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010717static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010718 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010719{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010720 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10721 const size_t max_len = (size_t) ret;
10722
10723 if( ret < 0 )
10724 {
10725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10726 return( ret );
10727 }
10728
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010729 if( len > max_len )
10730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010732 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010735 "maximum fragment length: %d > %d",
10736 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010738 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010739 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010740#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010741#if defined(MBEDTLS_SSL_PROTO_TLS)
10742 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010743 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010744 }
10745#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010746 }
Paul Bakker887bd502011-06-08 13:10:54 +000010747
Paul Bakker5121ce52009-01-03 21:22:43 +000010748 if( ssl->out_left != 0 )
10749 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010750 /*
10751 * The user has previously tried to send the data and
10752 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10753 * written. In this case, we expect the high-level write function
10754 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010759 return( ret );
10760 }
10761 }
Paul Bakker887bd502011-06-08 13:10:54 +000010762 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010763 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010764 /*
10765 * The user is trying to send a message the first time, so we need to
10766 * copy the data into the internal buffers and setup the data structure
10767 * to keep track of partial writes
10768 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010769 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010771 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010772
Hanno Becker67bc7c32018-08-06 11:33:50 +010010773 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010776 return( ret );
10777 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010778 }
10779
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010780 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010781}
10782
10783/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010784 * Write application data, doing 1/n-1 splitting if necessary.
10785 *
10786 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010787 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010788 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010791static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010792 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010793{
10794 int ret;
10795
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010796 if( ssl->conf->cbc_record_splitting ==
10797 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010798 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010799 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010800 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10801 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010802 {
10803 return( ssl_write_real( ssl, buf, len ) );
10804 }
10805
10806 if( ssl->split_done == 0 )
10807 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010808 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010809 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010810 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010811 }
10812
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010813 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10814 return( ret );
10815 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010816
10817 return( ret + 1 );
10818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010819#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010820
10821/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010822 * Write application data (public-facing wrapper)
10823 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010824int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010825{
10826 int ret;
10827
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010829
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010830 if( ssl == NULL || ssl->conf == NULL )
10831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10832
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010833#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010834 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10835 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010836 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010837 return( ret );
10838 }
10839#endif
10840
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010841 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010842 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010843 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010844 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010845 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010846 return( ret );
10847 }
10848 }
10849
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010850#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010851 ret = ssl_write_split( ssl, buf, len );
10852#else
10853 ret = ssl_write_real( ssl, buf, len );
10854#endif
10855
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010857
10858 return( ret );
10859}
10860
10861/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010862 * Notify the peer that the connection is being closed
10863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010864int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010865{
10866 int ret;
10867
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010868 if( ssl == NULL || ssl->conf == NULL )
10869 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010872
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010873 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010876 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010878 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10879 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10880 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010882 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010883 return( ret );
10884 }
10885 }
10886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010888
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010889 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010890}
10891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010892void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010893{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010894 if( transform == NULL )
10895 return;
10896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010898 deflateEnd( &transform->ctx_deflate );
10899 inflateEnd( &transform->ctx_inflate );
10900#endif
10901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10903 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010904
Hanno Becker92231322018-01-03 15:32:51 +000010905#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906 mbedtls_md_free( &transform->md_ctx_enc );
10907 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010908#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010909
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010910 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010911}
10912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913#if defined(MBEDTLS_X509_CRT_PARSE_C)
10914static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010915{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010917
10918 while( cur != NULL )
10919 {
10920 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010922 cur = next;
10923 }
10924}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010926
Hanno Becker0271f962018-08-16 13:23:47 +010010927#if defined(MBEDTLS_SSL_PROTO_DTLS)
10928
10929static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10930{
10931 unsigned offset;
10932 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10933
10934 if( hs == NULL )
10935 return;
10936
Hanno Becker283f5ef2018-08-24 09:34:47 +010010937 ssl_free_buffered_record( ssl );
10938
Hanno Becker0271f962018-08-16 13:23:47 +010010939 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010940 ssl_buffering_free_slot( ssl, offset );
10941}
10942
10943static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10944 uint8_t slot )
10945{
10946 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10947 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010948
10949 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10950 return;
10951
Hanno Beckere605b192018-08-21 15:59:07 +010010952 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010953 {
Hanno Beckere605b192018-08-21 15:59:07 +010010954 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010955 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010956 mbedtls_free( hs_buf->data );
10957 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010958 }
10959}
10960
10961#endif /* MBEDTLS_SSL_PROTO_DTLS */
10962
Gilles Peskine9b562d52018-04-25 20:32:43 +020010963void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010964{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010965 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10966
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010967 if( handshake == NULL )
10968 return;
10969
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010970#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10971 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10972 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010973 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010974 handshake->async_in_progress = 0;
10975 }
10976#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10977
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010978#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10979 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10980 mbedtls_md5_free( &handshake->fin_md5 );
10981 mbedtls_sha1_free( &handshake->fin_sha1 );
10982#endif
10983#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10984#if defined(MBEDTLS_SHA256_C)
10985 mbedtls_sha256_free( &handshake->fin_sha256 );
10986#endif
10987#if defined(MBEDTLS_SHA512_C)
10988 mbedtls_sha512_free( &handshake->fin_sha512 );
10989#endif
10990#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992#if defined(MBEDTLS_DHM_C)
10993 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995#if defined(MBEDTLS_ECDH_C)
10996 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010997#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010998#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010999 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011000#if defined(MBEDTLS_SSL_CLI_C)
11001 mbedtls_free( handshake->ecjpake_cache );
11002 handshake->ecjpake_cache = NULL;
11003 handshake->ecjpake_cache_len = 0;
11004#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011005#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011006
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011007#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11008 if( handshake->psk != NULL )
11009 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011010 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011011 mbedtls_free( handshake->psk );
11012 }
11013#endif
11014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11016 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011017 /*
11018 * Free only the linked list wrapper, not the keys themselves
11019 * since the belong to the SNI callback
11020 */
11021 if( handshake->sni_key_cert != NULL )
11022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011023 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011024
11025 while( cur != NULL )
11026 {
11027 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011028 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011029 cur = next;
11030 }
11031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011033
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011034#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011035 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011036 if( handshake->ecrs_peer_cert != NULL )
11037 {
11038 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11039 mbedtls_free( handshake->ecrs_peer_cert );
11040 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011041#endif
11042
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011043#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11044 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11045 mbedtls_pk_free( &handshake->peer_pubkey );
11046#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048#if defined(MBEDTLS_SSL_PROTO_DTLS)
11049 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011050 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011051 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011052#endif
11053
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011054 mbedtls_platform_zeroize( handshake,
11055 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011056}
11057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011058void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011059{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011060 if( session == NULL )
11061 return;
11062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011063#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011064 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011065#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011066
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011067#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011069#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011070
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011071 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011072}
11073
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011074#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011075
11076#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11077#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11078#else
11079#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11080#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11081
11082#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11083#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11084#else
11085#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11086#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11087
11088#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11089#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11090#else
11091#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11092#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11093
11094#if defined(MBEDTLS_SSL_ALPN)
11095#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11096#else
11097#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11098#endif /* MBEDTLS_SSL_ALPN */
11099
11100#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11101#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11102#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11103#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11104
11105#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11106 ( (uint32_t) ( \
11107 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11108 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11109 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11110 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11111 0u ) )
11112
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011113static unsigned char ssl_serialized_context_header[] = {
11114 MBEDTLS_VERSION_MAJOR,
11115 MBEDTLS_VERSION_MINOR,
11116 MBEDTLS_VERSION_PATCH,
11117 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11118 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011119 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11120 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11121 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011122};
11123
Paul Bakker5121ce52009-01-03 21:22:43 +000011124/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011125 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011126 *
11127 * The format of the serialized data is:
11128 * (in the presentation language of TLS, RFC 8446 section 3)
11129 *
11130 * // header
11131 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011132 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011133 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011134 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011135 * Note: When updating the format, remember to keep these
11136 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011137 *
11138 * // session sub-structure
11139 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11140 * // transform sub-structure
11141 * uint8 random[64]; // ServerHello.random+ClientHello.random
11142 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11143 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11144 * // fields from ssl_context
11145 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11146 * uint64 in_window_top; // DTLS: last validated record seq_num
11147 * uint64 in_window; // DTLS: bitmask for replay protection
11148 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11149 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11150 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11151 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11152 *
11153 * Note that many fields of the ssl_context or sub-structures are not
11154 * serialized, as they fall in one of the following categories:
11155 *
11156 * 1. forced value (eg in_left must be 0)
11157 * 2. pointer to dynamically-allocated memory (eg session, transform)
11158 * 3. value can be re-derived from other data (eg session keys from MS)
11159 * 4. value was temporary (eg content of input buffer)
11160 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011161 */
11162int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11163 unsigned char *buf,
11164 size_t buf_len,
11165 size_t *olen )
11166{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011167 unsigned char *p = buf;
11168 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011169 size_t session_len;
11170 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011171
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011172 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011173 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11174 * this function's documentation.
11175 *
11176 * These are due to assumptions/limitations in the implementation. Some of
11177 * them are likely to stay (no handshake in progress) some might go away
11178 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011179 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011180 /* The initial handshake must be over */
11181 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011183 if( ssl->handshake != NULL )
11184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11185 /* Double-check that sub-structures are indeed ready */
11186 if( ssl->transform == NULL || ssl->session == NULL )
11187 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11188 /* There must be no pending incoming or outgoing data */
11189 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11191 if( ssl->out_left != 0 )
11192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11193 /* Protocol must be DLTS, not TLS */
11194 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11196 /* Version must be 1.2 */
11197 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11199 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11201 /* We must be using an AEAD ciphersuite */
11202 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11204 /* Renegotiation must not be enabled */
11205 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011207
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011208 /*
11209 * Version and format identifier
11210 */
11211 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011212
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011213 if( used <= buf_len )
11214 {
11215 memcpy( p, ssl_serialized_context_header,
11216 sizeof( ssl_serialized_context_header ) );
11217 p += sizeof( ssl_serialized_context_header );
11218 }
11219
11220 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011221 * Session (length + data)
11222 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011223 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011224 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11225 return( ret );
11226
11227 used += 4 + session_len;
11228 if( used <= buf_len )
11229 {
11230 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11231 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11232 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11233 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11234
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011235 ret = ssl_session_save( ssl->session, 1,
11236 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011237 if( ret != 0 )
11238 return( ret );
11239
11240 p += session_len;
11241 }
11242
11243 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011244 * Transform
11245 */
11246 used += sizeof( ssl->transform->randbytes );
11247 if( used <= buf_len )
11248 {
11249 memcpy( p, ssl->transform->randbytes,
11250 sizeof( ssl->transform->randbytes ) );
11251 p += sizeof( ssl->transform->randbytes );
11252 }
11253
11254#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11255 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11256 if( used <= buf_len )
11257 {
11258 *p++ = ssl->transform->in_cid_len;
11259 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11260 p += ssl->transform->in_cid_len;
11261
11262 *p++ = ssl->transform->out_cid_len;
11263 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11264 p += ssl->transform->out_cid_len;
11265 }
11266#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11267
11268 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011269 * Saved fields from top-level ssl_context structure
11270 */
11271#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11272 used += 4;
11273 if( used <= buf_len )
11274 {
11275 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11276 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11277 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11278 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11279 }
11280#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11281
11282#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11283 used += 16;
11284 if( used <= buf_len )
11285 {
11286 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11287 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11288 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11289 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11290 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11291 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11292 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11293 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11294
11295 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11296 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11297 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11298 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11299 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11300 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11301 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11302 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11303 }
11304#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11305
11306#if defined(MBEDTLS_SSL_PROTO_DTLS)
11307 used += 1;
11308 if( used <= buf_len )
11309 {
11310 *p++ = ssl->disable_datagram_packing;
11311 }
11312#endif /* MBEDTLS_SSL_PROTO_DTLS */
11313
11314 used += 8;
11315 if( used <= buf_len )
11316 {
11317 memcpy( p, ssl->cur_out_ctr, 8 );
11318 p += 8;
11319 }
11320
11321#if defined(MBEDTLS_SSL_PROTO_DTLS)
11322 used += 2;
11323 if( used <= buf_len )
11324 {
11325 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11326 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11327 }
11328#endif /* MBEDTLS_SSL_PROTO_DTLS */
11329
11330#if defined(MBEDTLS_SSL_ALPN)
11331 {
11332 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011333 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011334 : 0;
11335
11336 used += 1 + alpn_len;
11337 if( used <= buf_len )
11338 {
11339 *p++ = alpn_len;
11340
11341 if( ssl->alpn_chosen != NULL )
11342 {
11343 memcpy( p, ssl->alpn_chosen, alpn_len );
11344 p += alpn_len;
11345 }
11346 }
11347 }
11348#endif /* MBEDTLS_SSL_ALPN */
11349
11350 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011351 * Done
11352 */
11353 *olen = used;
11354
11355 if( used > buf_len )
11356 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011357
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011358 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11359
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011360 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011361}
11362
11363/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011364 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011365 *
11366 * This internal version is wrapped by a public function that cleans up in
11367 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011368 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011369static int ssl_context_load( mbedtls_ssl_context *ssl,
11370 const unsigned char *buf,
11371 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011372{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011373 const unsigned char *p = buf;
11374 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011375 size_t session_len;
11376 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011377
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011378 /*
11379 * The context should have been freshly setup or reset.
11380 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011381 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011382 * renegotiating, or if the user mistakenly loaded a session first.)
11383 */
11384 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11385 ssl->session != NULL )
11386 {
11387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11388 }
11389
11390 /*
11391 * We can't check that the config matches the initial one, but we can at
11392 * least check it matches the requirements for serializing.
11393 */
11394 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011395 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11396 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11397 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11398 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11399 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11400 MBEDTLS_SSL_MINOR_VERSION_3 ||
11401 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11402 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011403 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011404 {
11405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11406 }
11407
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011408 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11409
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011410 /*
11411 * Check version identifier
11412 */
11413 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11414 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11415
11416 if( memcmp( p, ssl_serialized_context_header,
11417 sizeof( ssl_serialized_context_header ) ) != 0 )
11418 {
11419 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11420 }
11421 p += sizeof( ssl_serialized_context_header );
11422
11423 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011424 * Session
11425 */
11426 if( (size_t)( end - p ) < 4 )
11427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11428
11429 session_len = ( (size_t) p[0] << 24 ) |
11430 ( (size_t) p[1] << 16 ) |
11431 ( (size_t) p[2] << 8 ) |
11432 ( (size_t) p[3] );
11433 p += 4;
11434
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011435 /* This has been allocated by ssl_handshake_init(), called by
11436 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11437 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011438 ssl->session_in = ssl->session;
11439 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011440 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011441
11442 if( (size_t)( end - p ) < session_len )
11443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11444
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011445 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011446 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011447 {
11448 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011449 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011450 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011451
11452 p += session_len;
11453
11454 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011455 * Transform
11456 */
11457
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011458 /* This has been allocated by ssl_handshake_init(), called by
11459 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11460 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011461 ssl->transform_in = ssl->transform;
11462 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011463 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011464
11465 /* Read random bytes and populate structure */
11466 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11467 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11468
11469 ret = ssl_populate_transform( ssl->transform,
11470 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11471 ssl->session->master,
11472#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11473#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11474 ssl->session->encrypt_then_mac,
11475#endif
11476#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11477 ssl->session->trunc_hmac,
11478#endif
11479#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11480#if defined(MBEDTLS_ZLIB_SUPPORT)
11481 ssl->session->compression,
11482#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011483 p, /* currently pointing to randbytes */
11484 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11485 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11486 ssl );
11487 if( ret != 0 )
11488 return( ret );
11489
11490 p += sizeof( ssl->transform->randbytes );
11491
11492#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11493 /* Read connection IDs and store them */
11494 if( (size_t)( end - p ) < 1 )
11495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11496
11497 ssl->transform->in_cid_len = *p++;
11498
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011499 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11501
11502 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11503 p += ssl->transform->in_cid_len;
11504
11505 ssl->transform->out_cid_len = *p++;
11506
11507 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11509
11510 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11511 p += ssl->transform->out_cid_len;
11512#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11513
11514 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011515 * Saved fields from top-level ssl_context structure
11516 */
11517#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11518 if( (size_t)( end - p ) < 4 )
11519 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11520
11521 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11522 ( (uint32_t) p[1] << 16 ) |
11523 ( (uint32_t) p[2] << 8 ) |
11524 ( (uint32_t) p[3] );
11525 p += 4;
11526#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11527
11528#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11529 if( (size_t)( end - p ) < 16 )
11530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11531
11532 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11533 ( (uint64_t) p[1] << 48 ) |
11534 ( (uint64_t) p[2] << 40 ) |
11535 ( (uint64_t) p[3] << 32 ) |
11536 ( (uint64_t) p[4] << 24 ) |
11537 ( (uint64_t) p[5] << 16 ) |
11538 ( (uint64_t) p[6] << 8 ) |
11539 ( (uint64_t) p[7] );
11540 p += 8;
11541
11542 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11543 ( (uint64_t) p[1] << 48 ) |
11544 ( (uint64_t) p[2] << 40 ) |
11545 ( (uint64_t) p[3] << 32 ) |
11546 ( (uint64_t) p[4] << 24 ) |
11547 ( (uint64_t) p[5] << 16 ) |
11548 ( (uint64_t) p[6] << 8 ) |
11549 ( (uint64_t) p[7] );
11550 p += 8;
11551#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11552
11553#if defined(MBEDTLS_SSL_PROTO_DTLS)
11554 if( (size_t)( end - p ) < 1 )
11555 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11556
11557 ssl->disable_datagram_packing = *p++;
11558#endif /* MBEDTLS_SSL_PROTO_DTLS */
11559
11560 if( (size_t)( end - p ) < 8 )
11561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11562
11563 memcpy( ssl->cur_out_ctr, p, 8 );
11564 p += 8;
11565
11566#if defined(MBEDTLS_SSL_PROTO_DTLS)
11567 if( (size_t)( end - p ) < 2 )
11568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11569
11570 ssl->mtu = ( p[0] << 8 ) | p[1];
11571 p += 2;
11572#endif /* MBEDTLS_SSL_PROTO_DTLS */
11573
11574#if defined(MBEDTLS_SSL_ALPN)
11575 {
11576 uint8_t alpn_len;
11577 const char **cur;
11578
11579 if( (size_t)( end - p ) < 1 )
11580 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11581
11582 alpn_len = *p++;
11583
11584 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11585 {
11586 /* alpn_chosen should point to an item in the configured list */
11587 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11588 {
11589 if( strlen( *cur ) == alpn_len &&
11590 memcmp( p, cur, alpn_len ) == 0 )
11591 {
11592 ssl->alpn_chosen = *cur;
11593 break;
11594 }
11595 }
11596 }
11597
11598 /* can only happen on conf mismatch */
11599 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11601
11602 p += alpn_len;
11603 }
11604#endif /* MBEDTLS_SSL_ALPN */
11605
11606 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011607 * Forced fields from top-level ssl_context structure
11608 *
11609 * Most of them already set to the correct value by mbedtls_ssl_init() and
11610 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11611 */
11612 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11613
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011614#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011615 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011616#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11617#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011618 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011619#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011620
11621#if defined(MBEDTLS_SSL_PROTO_DTLS)
11622 ssl->in_epoch = 1;
11623#endif
11624
11625 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11626 * which we don't want - otherwise we'd end up freeing the wrong transform
11627 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11628 if( ssl->handshake != NULL )
11629 {
11630 mbedtls_ssl_handshake_free( ssl );
11631 mbedtls_free( ssl->handshake );
11632 ssl->handshake = NULL;
11633 }
11634
11635 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011636 * Done - should have consumed entire buffer
11637 */
11638 if( p != end )
11639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011640
11641 return( 0 );
11642}
11643
11644/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011645 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011646 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011647int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011648 const unsigned char *buf,
11649 size_t len )
11650{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011651 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011652
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011653 if( ret != 0 )
11654 mbedtls_ssl_free( context );
11655
11656 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011657}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011658#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011659
11660/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011661 * Free an SSL context
11662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011663void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011664{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011665 if( ssl == NULL )
11666 return;
11667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011669
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011670 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011671 {
Angus Grattond8213d02016-05-25 20:56:48 +100011672 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011673 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011674 }
11675
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011676 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011677 {
Angus Grattond8213d02016-05-25 20:56:48 +100011678 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011679 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011680 }
11681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011682#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011683 if( ssl->compress_buf != NULL )
11684 {
Angus Grattond8213d02016-05-25 20:56:48 +100011685 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011686 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011687 }
11688#endif
11689
Paul Bakker48916f92012-09-16 19:57:18 +000011690 if( ssl->transform )
11691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011692 mbedtls_ssl_transform_free( ssl->transform );
11693 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011694 }
11695
11696 if( ssl->handshake )
11697 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011698 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011699 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11700 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011702 mbedtls_free( ssl->handshake );
11703 mbedtls_free( ssl->transform_negotiate );
11704 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011705 }
11706
Paul Bakkerc0463502013-02-14 11:19:38 +010011707 if( ssl->session )
11708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011709 mbedtls_ssl_session_free( ssl->session );
11710 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011711 }
11712
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011713#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011714 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011715 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011716 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011717 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011718 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011721#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11722 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11725 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011726 }
11727#endif
11728
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011729#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011730 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011731#endif
11732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011734
Paul Bakker86f04f42013-02-14 11:20:09 +010011735 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011736 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011737}
11738
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011739/*
11740 * Initialze mbedtls_ssl_config
11741 */
11742void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11743{
11744 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011745
11746#if !defined(MBEDTLS_SSL_PROTO_TLS)
11747 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11748#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011749}
11750
Simon Butcherc97b6972015-12-27 23:48:17 +000011751#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011752#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011753static int ssl_preset_default_hashes[] = {
11754#if defined(MBEDTLS_SHA512_C)
11755 MBEDTLS_MD_SHA512,
11756 MBEDTLS_MD_SHA384,
11757#endif
11758#if defined(MBEDTLS_SHA256_C)
11759 MBEDTLS_MD_SHA256,
11760 MBEDTLS_MD_SHA224,
11761#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011762#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011763 MBEDTLS_MD_SHA1,
11764#endif
11765 MBEDTLS_MD_NONE
11766};
Simon Butcherc97b6972015-12-27 23:48:17 +000011767#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011768#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011769
Hanno Becker73f4cb12019-06-27 13:51:07 +010011770#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011771static int ssl_preset_suiteb_ciphersuites[] = {
11772 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11773 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11774 0
11775};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011776#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011777
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011778#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011779#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011780static int ssl_preset_suiteb_hashes[] = {
11781 MBEDTLS_MD_SHA256,
11782 MBEDTLS_MD_SHA384,
11783 MBEDTLS_MD_NONE
11784};
11785#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011786#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011787
Hanno Beckerc1096e72019-06-19 12:30:41 +010011788#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011789static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011790#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011791 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011792#endif
11793#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011794 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011795#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011796 MBEDTLS_ECP_DP_NONE
11797};
11798#endif
11799
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011800/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011801 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011802 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011803int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011804 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011805{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011806#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011807 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011808#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011809
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011810 /* Use the functions here so that they are covered in tests,
11811 * but otherwise access member directly for efficiency */
11812 mbedtls_ssl_conf_endpoint( conf, endpoint );
11813 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011814
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011815 /*
11816 * Things that are common to all presets
11817 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011818#if defined(MBEDTLS_SSL_CLI_C)
11819 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11820 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011821#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011822 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011823#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011824#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11825 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11826#endif
11827 }
11828#endif
11829
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011830#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011831 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011832#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011833
11834#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11835 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11836#endif
11837
11838#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011839#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011840 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011841#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11842#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011843 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011844 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011845#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011846#endif
11847
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011848#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11849 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11850#endif
11851
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011852#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011853 conf->f_cookie_write = ssl_cookie_write_dummy;
11854 conf->f_cookie_check = ssl_cookie_check_dummy;
11855#endif
11856
Hanno Becker7f376f42019-06-12 16:20:48 +010011857#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11858 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011859 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11860#endif
11861
Janos Follath088ce432017-04-10 12:42:31 +010011862#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011863#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011864 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011865#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11866#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011867
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011869#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011870 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011871#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11872#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011873 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011874#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11875#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011876
11877#if defined(MBEDTLS_SSL_RENEGOTIATION)
11878 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011879 memset( conf->renego_period, 0x00, 2 );
11880 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011881#endif
11882
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011883#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11884 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11885 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011886 const unsigned char dhm_p[] =
11887 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11888 const unsigned char dhm_g[] =
11889 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11890
Hanno Beckera90658f2017-10-04 15:29:08 +010011891 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11892 dhm_p, sizeof( dhm_p ),
11893 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011894 {
11895 return( ret );
11896 }
11897 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011898#endif
11899
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011900 /*
11901 * Preset-specific defaults
11902 */
11903 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011904 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011905 /*
11906 * NSA Suite B
11907 */
11908 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011909#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011910 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011911#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11912#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011913 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011914#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11915#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011916 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011917#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11918#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011919 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011920#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011921
Hanno Becker73f4cb12019-06-27 13:51:07 +010011922#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011923 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11924 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11925 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11926 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11927 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011928#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011929
11930#if defined(MBEDTLS_X509_CRT_PARSE_C)
11931 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011932#endif
11933
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011934#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011935#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011936 conf->sig_hashes = ssl_preset_suiteb_hashes;
11937#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011938#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011939
11940#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011941#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011942 conf->curve_list = ssl_preset_suiteb_curves;
11943#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011944#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011945 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011946
11947 /*
11948 * Default
11949 */
11950 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011951#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011952 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11953 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11954 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11955 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011956#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11957#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011958 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11959 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11960 MBEDTLS_SSL_MIN_MINOR_VERSION :
11961 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011963 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011964 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11965#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011966#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11967#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11968 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11969#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11970#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11971 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11972#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011973
Hanno Becker73f4cb12019-06-27 13:51:07 +010011974#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011975 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11976 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11977 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11978 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11979 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010011980#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011981
11982#if defined(MBEDTLS_X509_CRT_PARSE_C)
11983 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11984#endif
11985
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011986#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011987#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011988 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011989#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011990#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011991
11992#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011993#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011994 conf->curve_list = mbedtls_ecp_grp_id_list();
11995#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011996#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011997
11998#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
11999 conf->dhm_min_bitlen = 1024;
12000#endif
12001 }
12002
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012003 return( 0 );
12004}
12005
12006/*
12007 * Free mbedtls_ssl_config
12008 */
12009void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12010{
12011#if defined(MBEDTLS_DHM_C)
12012 mbedtls_mpi_free( &conf->dhm_P );
12013 mbedtls_mpi_free( &conf->dhm_G );
12014#endif
12015
12016#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12017 if( conf->psk != NULL )
12018 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012019 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012020 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012021 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012022 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012023 }
12024
12025 if( conf->psk_identity != NULL )
12026 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012027 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012028 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012029 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012030 conf->psk_identity_len = 0;
12031 }
12032#endif
12033
12034#if defined(MBEDTLS_X509_CRT_PARSE_C)
12035 ssl_key_cert_free( conf->key_cert );
12036#endif
12037
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012038 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012039}
12040
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012041#if defined(MBEDTLS_PK_C) && \
12042 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012043/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012044 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012046unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012048#if defined(MBEDTLS_RSA_C)
12049 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12050 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012052#if defined(MBEDTLS_ECDSA_C)
12053 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12054 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012056 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012057}
12058
Hanno Becker7e5437a2017-04-28 17:15:26 +010012059unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12060{
12061 switch( type ) {
12062 case MBEDTLS_PK_RSA:
12063 return( MBEDTLS_SSL_SIG_RSA );
12064 case MBEDTLS_PK_ECDSA:
12065 case MBEDTLS_PK_ECKEY:
12066 return( MBEDTLS_SSL_SIG_ECDSA );
12067 default:
12068 return( MBEDTLS_SSL_SIG_ANON );
12069 }
12070}
12071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012072mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012073{
12074 switch( sig )
12075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012076#if defined(MBEDTLS_RSA_C)
12077 case MBEDTLS_SSL_SIG_RSA:
12078 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012080#if defined(MBEDTLS_ECDSA_C)
12081 case MBEDTLS_SSL_SIG_ECDSA:
12082 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012083#endif
12084 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012085 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012086 }
12087}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012088#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012089
Hanno Becker7e5437a2017-04-28 17:15:26 +010012090#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12091 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12092
12093/* Find an entry in a signature-hash set matching a given hash algorithm. */
12094mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12095 mbedtls_pk_type_t sig_alg )
12096{
12097 switch( sig_alg )
12098 {
12099 case MBEDTLS_PK_RSA:
12100 return( set->rsa );
12101 case MBEDTLS_PK_ECDSA:
12102 return( set->ecdsa );
12103 default:
12104 return( MBEDTLS_MD_NONE );
12105 }
12106}
12107
12108/* Add a signature-hash-pair to a signature-hash set */
12109void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12110 mbedtls_pk_type_t sig_alg,
12111 mbedtls_md_type_t md_alg )
12112{
12113 switch( sig_alg )
12114 {
12115 case MBEDTLS_PK_RSA:
12116 if( set->rsa == MBEDTLS_MD_NONE )
12117 set->rsa = md_alg;
12118 break;
12119
12120 case MBEDTLS_PK_ECDSA:
12121 if( set->ecdsa == MBEDTLS_MD_NONE )
12122 set->ecdsa = md_alg;
12123 break;
12124
12125 default:
12126 break;
12127 }
12128}
12129
12130/* Allow exactly one hash algorithm for each signature. */
12131void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12132 mbedtls_md_type_t md_alg )
12133{
12134 set->rsa = md_alg;
12135 set->ecdsa = md_alg;
12136}
12137
12138#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12139 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12140
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012141/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012142 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012144mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012145{
12146 switch( hash )
12147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012148#if defined(MBEDTLS_MD5_C)
12149 case MBEDTLS_SSL_HASH_MD5:
12150 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012151#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012152#if defined(MBEDTLS_SHA1_C)
12153 case MBEDTLS_SSL_HASH_SHA1:
12154 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012155#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012156#if defined(MBEDTLS_SHA256_C)
12157 case MBEDTLS_SSL_HASH_SHA224:
12158 return( MBEDTLS_MD_SHA224 );
12159 case MBEDTLS_SSL_HASH_SHA256:
12160 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012162#if defined(MBEDTLS_SHA512_C)
12163 case MBEDTLS_SSL_HASH_SHA384:
12164 return( MBEDTLS_MD_SHA384 );
12165 case MBEDTLS_SSL_HASH_SHA512:
12166 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012167#endif
12168 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012169 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012170 }
12171}
12172
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012173/*
12174 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12175 */
12176unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12177{
12178 switch( md )
12179 {
12180#if defined(MBEDTLS_MD5_C)
12181 case MBEDTLS_MD_MD5:
12182 return( MBEDTLS_SSL_HASH_MD5 );
12183#endif
12184#if defined(MBEDTLS_SHA1_C)
12185 case MBEDTLS_MD_SHA1:
12186 return( MBEDTLS_SSL_HASH_SHA1 );
12187#endif
12188#if defined(MBEDTLS_SHA256_C)
12189 case MBEDTLS_MD_SHA224:
12190 return( MBEDTLS_SSL_HASH_SHA224 );
12191 case MBEDTLS_MD_SHA256:
12192 return( MBEDTLS_SSL_HASH_SHA256 );
12193#endif
12194#if defined(MBEDTLS_SHA512_C)
12195 case MBEDTLS_MD_SHA384:
12196 return( MBEDTLS_SSL_HASH_SHA384 );
12197 case MBEDTLS_MD_SHA512:
12198 return( MBEDTLS_SSL_HASH_SHA512 );
12199#endif
12200 default:
12201 return( MBEDTLS_SSL_HASH_NONE );
12202 }
12203}
12204
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012205#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012206/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012207 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012208 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012209 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012210int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012211{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012212 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12213 if( own_ec_id == grp_id )
12214 return( 0 );
12215 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012216
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012217 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012218}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012219#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012220
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012221#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012222/*
12223 * Check if a hash proposed by the peer is in our list.
12224 * Return 0 if we're willing to use it, -1 otherwise.
12225 */
12226int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12227 mbedtls_md_type_t md )
12228{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012229 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12230 if( md_alg == md )
12231 return( 0 );
12232 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012233
12234 return( -1 );
12235}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012236#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012238#if defined(MBEDTLS_X509_CRT_PARSE_C)
12239int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012240 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012241 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012242 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012243{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012244 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012245#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012246 int usage = 0;
12247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012248#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012249 const char *ext_oid;
12250 size_t ext_len;
12251#endif
12252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012253#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12254 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012255 ((void) cert);
12256 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012257 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012258#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012260#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12261 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012262 {
12263 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012264 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012266 case MBEDTLS_KEY_EXCHANGE_RSA:
12267 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012268 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012269 break;
12270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012271 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12272 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12273 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12274 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012275 break;
12276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012277 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12278 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012279 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012280 break;
12281
12282 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012283 case MBEDTLS_KEY_EXCHANGE_NONE:
12284 case MBEDTLS_KEY_EXCHANGE_PSK:
12285 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12286 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012287 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012288 usage = 0;
12289 }
12290 }
12291 else
12292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012293 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12294 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012295 }
12296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012297 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012298 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012299 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012300 ret = -1;
12301 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012302#else
12303 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012304#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012306#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12307 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012309 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12310 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012311 }
12312 else
12313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012314 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12315 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012316 }
12317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012318 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012319 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012320 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012321 ret = -1;
12322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012323#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012324
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012325 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012327#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012328
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012329#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12330 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12331int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12332 unsigned char *output,
12333 unsigned char *data, size_t data_len )
12334{
12335 int ret = 0;
12336 mbedtls_md5_context mbedtls_md5;
12337 mbedtls_sha1_context mbedtls_sha1;
12338
12339 mbedtls_md5_init( &mbedtls_md5 );
12340 mbedtls_sha1_init( &mbedtls_sha1 );
12341
12342 /*
12343 * digitally-signed struct {
12344 * opaque md5_hash[16];
12345 * opaque sha_hash[20];
12346 * };
12347 *
12348 * md5_hash
12349 * MD5(ClientHello.random + ServerHello.random
12350 * + ServerParams);
12351 * sha_hash
12352 * SHA(ClientHello.random + ServerHello.random
12353 * + ServerParams);
12354 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012355 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012356 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012358 goto exit;
12359 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012360 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012361 ssl->handshake->randbytes, 64 ) ) != 0 )
12362 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012364 goto exit;
12365 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012366 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012367 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012369 goto exit;
12370 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012371 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012372 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012374 goto exit;
12375 }
12376
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012377 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012378 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012379 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012380 goto exit;
12381 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012382 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012383 ssl->handshake->randbytes, 64 ) ) != 0 )
12384 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012386 goto exit;
12387 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012388 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012389 data_len ) ) != 0 )
12390 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012392 goto exit;
12393 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012394 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012395 output + 16 ) ) != 0 )
12396 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012398 goto exit;
12399 }
12400
12401exit:
12402 mbedtls_md5_free( &mbedtls_md5 );
12403 mbedtls_sha1_free( &mbedtls_sha1 );
12404
12405 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012406 mbedtls_ssl_pend_fatal_alert( ssl,
12407 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012408
12409 return( ret );
12410
12411}
12412#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12413 MBEDTLS_SSL_PROTO_TLS1_1 */
12414
12415#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12416 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12417int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012418 unsigned char *hash, size_t *hashlen,
12419 unsigned char *data, size_t data_len,
12420 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012421{
12422 int ret = 0;
12423 mbedtls_md_context_t ctx;
12424 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012425 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012426
12427 mbedtls_md_init( &ctx );
12428
12429 /*
12430 * digitally-signed struct {
12431 * opaque client_random[32];
12432 * opaque server_random[32];
12433 * ServerDHParams params;
12434 * };
12435 */
12436 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12437 {
12438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12439 goto exit;
12440 }
12441 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12442 {
12443 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12444 goto exit;
12445 }
12446 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12447 {
12448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12449 goto exit;
12450 }
12451 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12452 {
12453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12454 goto exit;
12455 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012456 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012457 {
12458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12459 goto exit;
12460 }
12461
12462exit:
12463 mbedtls_md_free( &ctx );
12464
12465 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012466 mbedtls_ssl_pend_fatal_alert( ssl,
12467 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012468
12469 return( ret );
12470}
12471#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12472 MBEDTLS_SSL_PROTO_TLS1_2 */
12473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012474#endif /* MBEDTLS_SSL_TLS_C */