blob: e94bc6482fc9f0e6eabf71b328c24b0ab462d7b6 [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 */
Hanno Becker298a4702019-08-16 10:21:32 +01001203/* Force compilers to inline this function if it's used only
1204 * from one place, because at least ARMC5 doesn't do that
1205 * automatically. */
1206#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1207MBEDTLS_ALWAYS_INLINE static inline
1208#else
1209static
1210#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1211int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001212 int ciphersuite,
1213 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001214#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1216 int encrypt_then_mac,
1217#endif
1218#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1219 int trunc_hmac,
1220#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001221#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001222#if defined(MBEDTLS_ZLIB_SUPPORT)
1223 int compression,
1224#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001225 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001226 int minor_ver,
1227 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001228 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001229{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001230 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 unsigned char keyblk[256];
1232 unsigned char *key1;
1233 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001234 unsigned char *mac_enc;
1235 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001236 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001237 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001238 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001239 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 const mbedtls_cipher_info_t *cipher_info;
1241 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001242
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001243#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1244 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1245 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001246 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001247 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001248#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001249
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001250 /*
1251 * Some data just needs copying into the structure
1252 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001253#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1254 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001255 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001256#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001257
1258#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001259 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001260#else
1261 ((void) minor_ver);
1262#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001264#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1265 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1266#endif
1267
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001268 /*
1269 * Get various info structures
1270 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001271 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001272 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001273 {
1274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001275 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001276 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1277 }
1278
Hanno Becker473f98f2019-06-26 10:27:32 +01001279 cipher_info = mbedtls_cipher_info_from_type(
1280 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001281 if( cipher_info == NULL )
1282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001284 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001286 }
1287
Hanno Becker473f98f2019-06-26 10:27:32 +01001288 md_info = mbedtls_md_info_from_type(
1289 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001290 if( md_info == NULL )
1291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001293 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001295 }
1296
Hanno Beckera5a2b082019-05-15 14:03:01 +01001297#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001298 /* Copy own and peer's CID if the use of the CID
1299 * extension has been negotiated. */
1300 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1301 {
1302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001303
Hanno Becker4932f9f2019-05-03 15:23:51 +01001304 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001305 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001306 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001307 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001308
1309 transform->out_cid_len = ssl->handshake->peer_cid_len;
1310 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1311 ssl->handshake->peer_cid_len );
1312 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1313 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001314 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001315#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001316
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001318 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001319 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001320 ret = ssl_prf( minor_ver,
1321 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1322 master, 48, "key expansion", randbytes, 64,
1323 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001324 if( ret != 0 )
1325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001327 return( ret );
1328 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001331 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001332 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001333 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 /*
1337 * Determine the appropriate key, IV and MAC length.
1338 */
Paul Bakker68884e32013-01-07 18:20:04 +01001339
Hanno Beckere7f2df02017-12-27 08:17:40 +00001340 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001341
Hanno Beckerf1229442018-01-03 15:32:31 +00001342#if defined(MBEDTLS_GCM_C) || \
1343 defined(MBEDTLS_CCM_C) || \
1344 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001346 cipher_info->mode == MBEDTLS_MODE_CCM ||
1347 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001349 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001350 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001351 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1352 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001353
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001354 /* All modes haves 96-bit IVs;
1355 * GCM and CCM has 4 implicit and 8 explicit bytes
1356 * ChachaPoly has all 12 bytes implicit
1357 */
Paul Bakker68884e32013-01-07 18:20:04 +01001358 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001359 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1360 transform->fixed_ivlen = 12;
1361 else
1362 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001363 }
1364 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001365#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1366#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1367 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1368 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001369 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001370 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1372 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001375 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001378 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001379 mac_key_len = mbedtls_md_get_size( md_info );
1380 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001383 /*
1384 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1385 * (rfc 6066 page 13 or rfc 2104 section 4),
1386 * so we only need to adjust the length here.
1387 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001388 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001391
1392#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1393 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001394 * HMAC implementation which also truncates the key
1395 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001396 mac_key_len = transform->maclen;
1397#endif
1398 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001400
1401 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001402 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001404 else
1405#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1406 {
1407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Hanno Beckera9d5c452019-07-25 16:47:12 +01001411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001412 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001413 (unsigned) transform->ivlen,
1414 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
1416 /*
1417 * Finally setup the cipher contexts, IVs and MAC secrets.
1418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001420 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001422 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001423 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Paul Bakker68884e32013-01-07 18:20:04 +01001425 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001426 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001427
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001428 /*
1429 * This is not used in TLS v1.1.
1430 */
Paul Bakker48916f92012-09-16 19:57:18 +00001431 iv_copy_len = ( transform->fixed_ivlen ) ?
1432 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001433 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1434 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001435 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 }
1437 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#endif /* MBEDTLS_SSL_CLI_C */
1439#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001440 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001442 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001443 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
Hanno Becker81c7b182017-11-09 18:39:33 +00001445 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001446 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001448 /*
1449 * This is not used in TLS v1.1.
1450 */
Paul Bakker48916f92012-09-16 19:57:18 +00001451 iv_copy_len = ( transform->fixed_ivlen ) ?
1452 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001453 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1454 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001455 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001457 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1461 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001462 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001463
Hanno Becker92231322018-01-03 15:32:51 +00001464#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001466 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001467 {
Hanno Becker92231322018-01-03 15:32:51 +00001468 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001472 }
1473
Hanno Becker81c7b182017-11-09 18:39:33 +00001474 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1475 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001476 }
1477 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1479#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1480 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001481 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001482 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001483 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1484 For AEAD-based ciphersuites, there is nothing to do here. */
1485 if( mac_key_len != 0 )
1486 {
1487 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1488 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1489 }
Paul Bakker68884e32013-01-07 18:20:04 +01001490 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001491 else
1492#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001496 }
Hanno Becker92231322018-01-03 15:32:51 +00001497#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1500 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001501 {
1502 int ret = 0;
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001505
Hanno Beckere7f2df02017-12-27 08:17:40 +00001506 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001507 transform->iv_enc, transform->iv_dec,
1508 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001509 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001510 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1513 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001514 }
1515 }
Hanno Becker92231322018-01-03 15:32:51 +00001516#else
1517 ((void) mac_dec);
1518 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001520
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001521#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1522 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001523 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001524 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001525 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001526 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001527 iv_copy_len );
1528 }
1529#endif
1530
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001531 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001532 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001535 return( ret );
1536 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001537
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001538 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001539 cipher_info ) ) != 0 )
1540 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001542 return( ret );
1543 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
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_ENCRYPT ) ) != 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 Bakkerea6ad3f2013-09-02 14:57:01 +02001552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001554 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001558 return( ret );
1559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#if defined(MBEDTLS_CIPHER_MODE_CBC)
1562 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1565 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001569 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1572 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001575 return( ret );
1576 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001579
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001580 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001581
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001582 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001584 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001587
Paul Bakker48916f92012-09-16 19:57:18 +00001588 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1589 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001590
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001591 if( deflateInit( &transform->ctx_deflate,
1592 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001593 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1596 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001597 }
1598 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 return( 0 );
1602}
1603
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001604#if defined(MBEDTLS_SSL_PROTO_SSL3)
1605static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1606 unsigned char hash[36],
1607 size_t *hlen )
1608{
1609 mbedtls_md5_context md5;
1610 mbedtls_sha1_context sha1;
1611 unsigned char pad_1[48];
1612 unsigned char pad_2[48];
1613
1614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1615
1616 mbedtls_md5_init( &md5 );
1617 mbedtls_sha1_init( &sha1 );
1618
1619 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1620 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1621
1622 memset( pad_1, 0x36, 48 );
1623 memset( pad_2, 0x5C, 48 );
1624
1625 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1626 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1627 mbedtls_md5_finish_ret( &md5, hash );
1628
1629 mbedtls_md5_starts_ret( &md5 );
1630 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1631 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1632 mbedtls_md5_update_ret( &md5, hash, 16 );
1633 mbedtls_md5_finish_ret( &md5, hash );
1634
1635 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1636 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1637 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1638
1639 mbedtls_sha1_starts_ret( &sha1 );
1640 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1641 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1642 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1644
1645 *hlen = 36;
1646
1647 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1649
1650 mbedtls_md5_free( &md5 );
1651 mbedtls_sha1_free( &sha1 );
1652
1653 return;
1654}
1655#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1656
1657#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1658static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1659 unsigned char hash[36],
1660 size_t *hlen )
1661{
1662 mbedtls_md5_context md5;
1663 mbedtls_sha1_context sha1;
1664
1665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1666
1667 mbedtls_md5_init( &md5 );
1668 mbedtls_sha1_init( &sha1 );
1669
1670 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1671 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1672
1673 mbedtls_md5_finish_ret( &md5, hash );
1674 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1675
1676 *hlen = 36;
1677
1678 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1680
1681 mbedtls_md5_free( &md5 );
1682 mbedtls_sha1_free( &sha1 );
1683
1684 return;
1685}
1686#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1687
1688#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1689#if defined(MBEDTLS_SHA256_C)
1690static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1691 unsigned char hash[32],
1692 size_t *hlen )
1693{
1694 mbedtls_sha256_context sha256;
1695
1696 mbedtls_sha256_init( &sha256 );
1697
1698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1699
1700 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1701 mbedtls_sha256_finish_ret( &sha256, hash );
1702
1703 *hlen = 32;
1704
1705 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1707
1708 mbedtls_sha256_free( &sha256 );
1709
1710 return;
1711}
1712#endif /* MBEDTLS_SHA256_C */
1713
1714#if defined(MBEDTLS_SHA512_C)
1715static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1716 unsigned char hash[48],
1717 size_t *hlen )
1718{
1719 mbedtls_sha512_context sha512;
1720
1721 mbedtls_sha512_init( &sha512 );
1722
1723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1724
1725 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1726 mbedtls_sha512_finish_ret( &sha512, hash );
1727
1728 *hlen = 48;
1729
1730 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1732
1733 mbedtls_sha512_free( &sha512 );
1734
1735 return;
1736}
1737#endif /* MBEDTLS_SHA512_C */
1738#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1739
Hanno Becker2f41b242019-08-15 17:29:43 +01001740int mbedtls_ssl_calc_verify( int minor_ver,
1741 mbedtls_md_type_t hash,
1742 mbedtls_ssl_context const *ssl,
1743 unsigned char *dst,
1744 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001745{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001746#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1747 (void) hash;
1748#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001749
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001750#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001751 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001752 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001753 else
1754#endif
1755#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001756 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001757 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001758 else
1759#endif
1760#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1761#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001762 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1763 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001765 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001766 }
1767 else
1768#endif
1769#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001770 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001771 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001772 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001773 }
1774 else
1775#endif
1776#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1777 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1779 }
1780
1781 return( 0 );
1782}
1783
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001784/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001785 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001786 *
1787 * Parameters:
1788 * [in/out] handshake
1789 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1790 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001791 * [out] master
1792 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001793 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001794static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001795 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001796 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001797{
1798 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001799
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001800/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1801/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1802/* (void) ssl; */
1803/* #endif */
1804
1805 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1806 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001807
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001808#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001809 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001810 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1812 return( 0 );
1813 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001814#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815
1816 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1817 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001818
1819#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001820 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1821 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001822 {
1823 unsigned char session_hash[48];
1824 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001825
Hanno Becker2f41b242019-08-15 17:29:43 +01001826 mbedtls_ssl_calc_verify(
1827 mbedtls_ssl_get_minor_ver( ssl ),
1828 mbedtls_ssl_suite_get_mac( ciphersuite ),
1829 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001830
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001831 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1832 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001833
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001834 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1835 mbedtls_ssl_suite_get_mac( ciphersuite ),
1836 handshake->premaster, handshake->pmslen,
1837 "extended master secret",
1838 session_hash, hash_len,
1839 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001840 }
1841 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001842#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001843 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001844 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1845 mbedtls_ssl_suite_get_mac( ciphersuite ),
1846 handshake->premaster, handshake->pmslen,
1847 "master secret",
1848 handshake->randbytes, 64,
1849 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001850 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001851 if( ret != 0 )
1852 {
1853 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1854 return( ret );
1855 }
1856
1857 mbedtls_platform_zeroize( handshake->premaster,
1858 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001859
1860 return( 0 );
1861}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001862
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001863int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1864{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001865 int ret;
1866
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1868
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001869 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001870 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001871 ssl->session_negotiate->master,
1872 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001873 if( ret != 0 )
1874 {
1875 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1876 return( ret );
1877 }
1878
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001879 /* Swap the client and server random values:
1880 * - MS derivation wanted client+server (RFC 5246 8.1)
1881 * - key derivation wants server+client (RFC 5246 6.3) */
1882 {
1883 unsigned char tmp[64];
1884 memcpy( tmp, ssl->handshake->randbytes, 64 );
1885 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1886 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1887 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1888 }
1889
1890 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001891 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001892 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1893 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001894#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001895#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001896 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001897#endif
1898#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001899 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001900#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001901#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001902#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001903 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001904#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001905 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001906 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001907 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1908 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001909 if( ret != 0 )
1910 {
1911 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1912 return( ret );
1913 }
1914
1915 /* We no longer need Server/ClientHello.random values */
1916 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1917 sizeof( ssl->handshake->randbytes ) );
1918
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001919 /* Allocate compression buffer */
1920#if defined(MBEDTLS_ZLIB_SUPPORT)
1921 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1922 ssl->compress_buf == NULL )
1923 {
1924 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1925 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1926 if( ssl->compress_buf == NULL )
1927 {
1928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001929 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001930 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1931 }
1932 }
1933#endif
1934
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1936
1937 return( 0 );
1938}
1939
Hanno Becker09d23642019-07-22 17:18:18 +01001940int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1941{
1942 int ret;
1943
1944 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1945 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1946
Hanno Beckera3c2c172019-07-23 16:51:57 +01001947#if defined(MBEDTLS_USE_TINYCRYPT)
1948 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1949 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1950 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1951 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA )
1952 {
1953 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001954 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001955
1956 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1957 ssl->handshake->ecdh_privkey,
1958 ssl->handshake->premaster,
1959 uecc_curve ) )
1960 {
1961 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1962 }
1963
1964 ssl->handshake->pmslen = NUM_ECC_BYTES;
1965 }
1966 else
1967#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001968#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1969 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1970 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1971 {
1972 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1973 ssl->handshake->premaster,
1974 MBEDTLS_PREMASTER_SIZE,
1975 &ssl->handshake->pmslen,
1976 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001977 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001978 {
1979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1980 return( ret );
1981 }
1982
1983 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1984 }
1985 else
1986#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001987#if defined(MBEDTLS_ECDH_C) && \
1988 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1989 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1990 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1991 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01001992 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1993 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
1994 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1995 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1996 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1997 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
1998 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1999 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2000 {
2001 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2002 &ssl->handshake->pmslen,
2003 ssl->handshake->premaster,
2004 MBEDTLS_MPI_MAX_SIZE,
2005 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002006 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002007 {
2008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2009 return( ret );
2010 }
2011
2012 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2013 }
2014 else
2015#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2016 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2017 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2018 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2019#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2020 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2021 {
2022 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002023 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002024 {
2025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2026 return( ret );
2027 }
2028 }
2029 else
2030#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2031#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2032 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2033 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2034 {
2035 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2036 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2037 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002038 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002039 if( ret != 0 )
2040 {
2041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2042 return( ret );
2043 }
2044 }
2045 else
2046#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2047#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2048 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2049 == MBEDTLS_KEY_EXCHANGE_RSA )
2050 {
2051 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002052 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002053 * ssl_rsa_generate_partial_pms(). Only the
2054 * PMS length needs to be set. */
2055 ssl->handshake->pmslen = 48;
2056 }
2057 else
2058#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2059 {
2060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2062 }
2063
2064 return( 0 );
2065}
2066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002067#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2068int 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 +02002069{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002070 unsigned char *p = ssl->handshake->premaster;
2071 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002072 const unsigned char *psk = ssl->conf->psk;
2073 size_t psk_len = ssl->conf->psk_len;
2074
2075 /* If the psk callback was called, use its result */
2076 if( ssl->handshake->psk != NULL )
2077 {
2078 psk = ssl->handshake->psk;
2079 psk_len = ssl->handshake->psk_len;
2080 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002081
2082 /*
2083 * PMS = struct {
2084 * opaque other_secret<0..2^16-1>;
2085 * opaque psk<0..2^16-1>;
2086 * };
2087 * with "other_secret" depending on the particular key exchange
2088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2090 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002091 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002092 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002094
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002095 *(p++) = (unsigned char)( psk_len >> 8 );
2096 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002097
2098 if( end < p || (size_t)( end - p ) < psk_len )
2099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2100
2101 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002102 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002103 }
2104 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2106#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2107 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002108 {
2109 /*
2110 * other_secret already set by the ClientKeyExchange message,
2111 * and is 48 bytes long
2112 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002113 if( end - p < 2 )
2114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2115
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002116 *p++ = 0;
2117 *p++ = 48;
2118 p += 48;
2119 }
2120 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2122#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2123 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002124 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002125 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002126 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002127
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002128 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002130 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002131 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002132 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002135 return( ret );
2136 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002137 *(p++) = (unsigned char)( len >> 8 );
2138 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002139 p += len;
2140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002142 }
2143 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2145#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2146 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002147 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002148 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002149 size_t zlen;
2150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002152 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002153 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002154 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002157 return( ret );
2158 }
2159
2160 *(p++) = (unsigned char)( zlen >> 8 );
2161 *(p++) = (unsigned char)( zlen );
2162 p += zlen;
2163
Janos Follath3fbdada2018-08-15 10:26:53 +01002164 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2165 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002166 }
2167 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002172 }
2173
2174 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002175 if( end - p < 2 )
2176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002177
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002178 *(p++) = (unsigned char)( psk_len >> 8 );
2179 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002180
2181 if( end < p || (size_t)( end - p ) < psk_len )
2182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2183
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002184 memcpy( p, psk, psk_len );
2185 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002186
2187 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2188
2189 return( 0 );
2190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002191#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002194/*
2195 * SSLv3.0 MAC functions
2196 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002197#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002198static void ssl_mac( mbedtls_md_context_t *md_ctx,
2199 const unsigned char *secret,
2200 const unsigned char *buf, size_t len,
2201 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002202 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002203{
2204 unsigned char header[11];
2205 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002206 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2208 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002209
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002210 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002211 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002212 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002213 else
Paul Bakker68884e32013-01-07 18:20:04 +01002214 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002215
2216 memcpy( header, ctr, 8 );
2217 header[ 8] = (unsigned char) type;
2218 header[ 9] = (unsigned char)( len >> 8 );
2219 header[10] = (unsigned char)( len );
2220
Paul Bakker68884e32013-01-07 18:20:04 +01002221 memset( padding, 0x36, 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 );
2225 mbedtls_md_update( md_ctx, header, 11 );
2226 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002227 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002228
Paul Bakker68884e32013-01-07 18:20:04 +01002229 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 mbedtls_md_starts( md_ctx );
2231 mbedtls_md_update( md_ctx, secret, md_size );
2232 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002233 mbedtls_md_update( md_ctx, out, md_size );
2234 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002237
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002238/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002239 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002240#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002241 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2242 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2243 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2244/* This function makes sure every byte in the memory region is accessed
2245 * (in ascending addresses order) */
2246static void ssl_read_memory( unsigned char *p, size_t len )
2247{
2248 unsigned char acc = 0;
2249 volatile unsigned char force;
2250
2251 for( ; len != 0; p++, len-- )
2252 acc ^= *p;
2253
2254 force = acc;
2255 (void) force;
2256}
2257#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2258
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002259/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002261 */
Hanno Becker3307b532017-12-27 21:37:21 +00002262
Hanno Beckera5a2b082019-05-15 14:03:01 +01002263#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002264/* This functions transforms a DTLS plaintext fragment and a record content
2265 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002266 *
2267 * struct {
2268 * opaque content[DTLSPlaintext.length];
2269 * ContentType real_type;
2270 * uint8 zeros[length_of_padding];
2271 * } DTLSInnerPlaintext;
2272 *
2273 * Input:
2274 * - `content`: The beginning of the buffer holding the
2275 * plaintext to be wrapped.
2276 * - `*content_size`: The length of the plaintext in Bytes.
2277 * - `max_len`: The number of Bytes available starting from
2278 * `content`. This must be `>= *content_size`.
2279 * - `rec_type`: The desired record content type.
2280 *
2281 * Output:
2282 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2283 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2284 *
2285 * Returns:
2286 * - `0` on success.
2287 * - A negative error code if `max_len` didn't offer enough space
2288 * for the expansion.
2289 */
2290static int ssl_cid_build_inner_plaintext( unsigned char *content,
2291 size_t *content_size,
2292 size_t remaining,
2293 uint8_t rec_type )
2294{
2295 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002296 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2297 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2298 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002299
2300 /* Write real content type */
2301 if( remaining == 0 )
2302 return( -1 );
2303 content[ len ] = rec_type;
2304 len++;
2305 remaining--;
2306
2307 if( remaining < pad )
2308 return( -1 );
2309 memset( content + len, 0, pad );
2310 len += pad;
2311 remaining -= pad;
2312
2313 *content_size = len;
2314 return( 0 );
2315}
2316
Hanno Becker7dc25772019-05-20 15:08:01 +01002317/* This function parses a DTLSInnerPlaintext structure.
2318 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002319static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2320 size_t *content_size,
2321 uint8_t *rec_type )
2322{
2323 size_t remaining = *content_size;
2324
2325 /* Determine length of padding by skipping zeroes from the back. */
2326 do
2327 {
2328 if( remaining == 0 )
2329 return( -1 );
2330 remaining--;
2331 } while( content[ remaining ] == 0 );
2332
2333 *content_size = remaining;
2334 *rec_type = content[ remaining ];
2335
2336 return( 0 );
2337}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002338#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002339
Hanno Becker99abf512019-05-20 14:50:53 +01002340/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002341 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002342static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002343 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002344 mbedtls_record *rec )
2345{
Hanno Becker99abf512019-05-20 14:50:53 +01002346 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002347 *
2348 * additional_data = seq_num + TLSCompressed.type +
2349 * TLSCompressed.version + TLSCompressed.length;
2350 *
Hanno Becker99abf512019-05-20 14:50:53 +01002351 * For the CID extension, this is extended as follows
2352 * (quoting draft-ietf-tls-dtls-connection-id-05,
2353 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002354 *
2355 * additional_data = seq_num + DTLSPlaintext.type +
2356 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002357 * cid +
2358 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002359 * length_of_DTLSInnerPlaintext;
2360 */
2361
Hanno Becker3307b532017-12-27 21:37:21 +00002362 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2363 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002364 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002365
Hanno Beckera5a2b082019-05-15 14:03:01 +01002366#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002367 if( rec->cid_len != 0 )
2368 {
2369 memcpy( add_data + 11, rec->cid, rec->cid_len );
2370 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2371 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2372 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2373 *add_data_len = 13 + 1 + rec->cid_len;
2374 }
2375 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002376#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002377 {
2378 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2379 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2380 *add_data_len = 13;
2381 }
Hanno Becker3307b532017-12-27 21:37:21 +00002382}
2383
Hanno Becker611a83b2018-01-03 14:27:32 +00002384int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2385 mbedtls_ssl_transform *transform,
2386 mbedtls_record *rec,
2387 int (*f_rng)(void *, unsigned char *, size_t),
2388 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002391 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002392 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002393 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002394 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002395 size_t post_avail;
2396
2397 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002398#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002399 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002400 ((void) ssl);
2401#endif
2402
2403 /* The PRNG is used for dynamic IV generation that's used
2404 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2405#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2406 ( defined(MBEDTLS_AES_C) || \
2407 defined(MBEDTLS_ARIA_C) || \
2408 defined(MBEDTLS_CAMELLIA_C) ) && \
2409 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2410 ((void) f_rng);
2411 ((void) p_rng);
2412#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
Hanno Becker3307b532017-12-27 21:37:21 +00002416 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002417 {
Hanno Becker3307b532017-12-27 21:37:21 +00002418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2420 }
Hanno Becker505089d2019-05-01 09:45:57 +01002421 if( rec == NULL
2422 || rec->buf == NULL
2423 || rec->buf_len < rec->data_offset
2424 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002425#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002426 || rec->cid_len != 0
2427#endif
2428 )
Hanno Becker3307b532017-12-27 21:37:21 +00002429 {
2430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002432 }
2433
Hanno Becker3307b532017-12-27 21:37:21 +00002434 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002435 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002437 data, rec->data_len );
2438
2439 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2440
2441 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2442 {
2443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2444 (unsigned) rec->data_len,
2445 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2446 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2447 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002448
Hanno Beckera5a2b082019-05-15 14:03:01 +01002449#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002450 /*
2451 * Add CID information
2452 */
2453 rec->cid_len = transform->out_cid_len;
2454 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2455 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002456
2457 if( rec->cid_len != 0 )
2458 {
2459 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002460 * Wrap plaintext into DTLSInnerPlaintext structure.
2461 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002462 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002463 * Note that this changes `rec->data_len`, and hence
2464 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002465 */
2466 if( ssl_cid_build_inner_plaintext( data,
2467 &rec->data_len,
2468 post_avail,
2469 rec->type ) != 0 )
2470 {
2471 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2472 }
2473
2474 rec->type = MBEDTLS_SSL_MSG_CID;
2475 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002476#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002477
Hanno Becker92c930f2019-04-29 17:31:37 +01002478 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2479
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002481 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002483#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 if( mode == MBEDTLS_MODE_STREAM ||
2485 ( mode == MBEDTLS_MODE_CBC
2486#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002487 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002488#endif
2489 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002490 {
Hanno Becker3307b532017-12-27 21:37:21 +00002491 if( post_avail < transform->maclen )
2492 {
2493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2494 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2495 }
2496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002498 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2499 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002500 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002501 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002502 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2503 data, rec->data_len, rec->ctr, rec->type, mac );
2504 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002505 }
2506 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002507#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2509 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002510 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2511 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002512 {
Hanno Becker992b6872017-11-09 18:57:39 +00002513 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2514
Hanno Beckere83efe62019-04-29 13:52:53 +01002515 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002516
Hanno Becker3307b532017-12-27 21:37:21 +00002517 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002518 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002519 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2520 data, rec->data_len );
2521 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2522 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2523
2524 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002525 }
2526 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002527#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2530 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002531 }
2532
Hanno Becker3307b532017-12-27 21:37:21 +00002533 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2534 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002535
Hanno Becker3307b532017-12-27 21:37:21 +00002536 rec->data_len += transform->maclen;
2537 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002538 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002539 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002540#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002542 /*
2543 * Encrypt
2544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2546 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002547 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002548 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002549 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002551 "including %d bytes of padding",
2552 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Hanno Becker3307b532017-12-27 21:37:21 +00002554 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2555 transform->iv_enc, transform->ivlen,
2556 data, rec->data_len,
2557 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002560 return( ret );
2561 }
2562
Hanno Becker3307b532017-12-27 21:37:21 +00002563 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002567 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
Paul Bakker68884e32013-01-07 18:20:04 +01002569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002571
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002572#if defined(MBEDTLS_GCM_C) || \
2573 defined(MBEDTLS_CCM_C) || \
2574 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002576 mode == MBEDTLS_MODE_CCM ||
2577 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002578 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002579 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002580 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002581 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002582
Hanno Becker3307b532017-12-27 21:37:21 +00002583 /* Check that there's space for both the authentication tag
2584 * and the explicit IV before and after the record content. */
2585 if( post_avail < transform->taglen ||
2586 rec->data_offset < explicit_iv_len )
2587 {
2588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2589 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2590 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002591
Paul Bakker68884e32013-01-07 18:20:04 +01002592 /*
2593 * Generate IV
2594 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002595 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2596 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002597 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002598 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002599 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2600 explicit_iv_len );
2601 /* Prefix record content with explicit IV. */
2602 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002603 }
2604 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2605 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002606 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002607 unsigned char i;
2608
2609 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2610
2611 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002612 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 }
2614 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002615 {
2616 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002619 }
2620
Hanno Beckere83efe62019-04-29 13:52:53 +01002621 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002622
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002623 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2624 iv, transform->ivlen );
2625 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002626 data - explicit_iv_len, explicit_iv_len );
2627 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002628 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002630 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002631 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002632
Paul Bakker68884e32013-01-07 18:20:04 +01002633 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002634 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002635 */
Hanno Becker3307b532017-12-27 21:37:21 +00002636
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002637 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002638 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002639 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002640 data, rec->data_len, /* source */
2641 data, &rec->data_len, /* destination */
2642 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002645 return( ret );
2646 }
2647
Hanno Becker3307b532017-12-27 21:37:21 +00002648 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2649 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002650
Hanno Becker3307b532017-12-27 21:37:21 +00002651 rec->data_len += transform->taglen + explicit_iv_len;
2652 rec->data_offset -= explicit_iv_len;
2653 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002654 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2658#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002659 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002662 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002663 size_t padlen, i;
2664 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002665
Hanno Becker3307b532017-12-27 21:37:21 +00002666 /* Currently we're always using minimal padding
2667 * (up to 255 bytes would be allowed). */
2668 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2669 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 padlen = 0;
2671
Hanno Becker3307b532017-12-27 21:37:21 +00002672 /* Check there's enough space in the buffer for the padding. */
2673 if( post_avail < padlen + 1 )
2674 {
2675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2676 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2677 }
2678
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002680 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Hanno Becker3307b532017-12-27 21:37:21 +00002682 rec->data_len += padlen + 1;
2683 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002686 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002687 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2688 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002689 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002690 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2691 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002692 {
Hanno Becker3307b532017-12-27 21:37:21 +00002693 if( f_rng == NULL )
2694 {
2695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2697 }
2698
2699 if( rec->data_offset < transform->ivlen )
2700 {
2701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2702 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2703 }
2704
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002705 /*
2706 * Generate IV
2707 */
Hanno Becker3307b532017-12-27 21:37:21 +00002708 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002709 if( ret != 0 )
2710 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002711
Hanno Becker3307b532017-12-27 21:37:21 +00002712 memcpy( data - transform->ivlen, transform->iv_enc,
2713 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002714
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002715 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002719 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002720 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002721 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
Hanno Becker3307b532017-12-27 21:37:21 +00002723 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2724 transform->iv_enc,
2725 transform->ivlen,
2726 data, rec->data_len,
2727 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002730 return( ret );
2731 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002732
Hanno Becker3307b532017-12-27 21:37:21 +00002733 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002740 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2741 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002742 {
2743 /*
2744 * Save IV in SSL3 and TLS1
2745 */
Hanno Becker3307b532017-12-27 21:37:21 +00002746 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2747 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 }
Hanno Becker3307b532017-12-27 21:37:21 +00002749 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002750#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002751 {
2752 data -= transform->ivlen;
2753 rec->data_offset -= transform->ivlen;
2754 rec->data_len += transform->ivlen;
2755 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002758 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002759 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002760 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2761
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002762 /*
2763 * MAC(MAC_write_key, seq_num +
2764 * TLSCipherText.type +
2765 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002766 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002767 * IV + // except for TLS 1.0
2768 * ENC(content + padding + padding_length));
2769 */
Hanno Becker3307b532017-12-27 21:37:21 +00002770
2771 if( post_avail < transform->maclen)
2772 {
2773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2774 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2775 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002776
Hanno Beckere83efe62019-04-29 13:52:53 +01002777 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002780 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002781 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002782
Hanno Becker3307b532017-12-27 21:37:21 +00002783 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002784 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002785 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2786 data, rec->data_len );
2787 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2788 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002789
Hanno Becker3307b532017-12-27 21:37:21 +00002790 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002791
Hanno Becker3307b532017-12-27 21:37:21 +00002792 rec->data_len += transform->maclen;
2793 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002794 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002795 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002798 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002800 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002804 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002806 /* Make extra sure authentication was performed, exactly once */
2807 if( auth_done != 1 )
2808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2810 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002811 }
2812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
2815 return( 0 );
2816}
2817
Hanno Becker40478be2019-07-12 08:23:59 +01002818int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002819 mbedtls_ssl_transform *transform,
2820 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002821{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002822 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002824 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002825#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002826 size_t padlen = 0, correct = 1;
2827#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002828 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002829 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002830 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002831
Hanno Becker611a83b2018-01-03 14:27:32 +00002832#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002833 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002834 ((void) ssl);
2835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002838 if( rec == NULL ||
2839 rec->buf == NULL ||
2840 rec->buf_len < rec->data_offset ||
2841 rec->buf_len - rec->data_offset < rec->data_len )
2842 {
2843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002845 }
2846
Hanno Becker4c6876b2017-12-27 21:28:58 +00002847 data = rec->buf + rec->data_offset;
2848 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Hanno Beckera5a2b082019-05-15 14:03:01 +01002850#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002851 /*
2852 * Match record's CID with incoming CID.
2853 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002854 if( rec->cid_len != transform->in_cid_len ||
2855 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2856 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002857 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002858 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002859#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2862 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002863 {
2864 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002865 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2866 transform->iv_dec,
2867 transform->ivlen,
2868 data, rec->data_len,
2869 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002872 return( ret );
2873 }
2874
Hanno Becker4c6876b2017-12-27 21:28:58 +00002875 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002879 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 }
Paul Bakker68884e32013-01-07 18:20:04 +01002881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002883#if defined(MBEDTLS_GCM_C) || \
2884 defined(MBEDTLS_CCM_C) || \
2885 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002887 mode == MBEDTLS_MODE_CCM ||
2888 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002889 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002890 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002891 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002892
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002893 /*
2894 * Prepare IV from explicit and implicit data.
2895 */
2896
2897 /* Check that there's enough space for the explicit IV
2898 * (at the beginning of the record) and the MAC (at the
2899 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002900 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002903 "+ taglen (%d)", rec->data_len,
2904 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002906 }
Paul Bakker68884e32013-01-07 18:20:04 +01002907
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002908#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002909 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2910 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002911 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002912
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002913 /* Fixed */
2914 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2915 /* Explicit */
2916 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002917 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002918 else
2919#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2920#if defined(MBEDTLS_CHACHAPOLY_C)
2921 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002922 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002923 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002924 unsigned char i;
2925
2926 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2927
2928 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002929 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002930 }
2931 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002932#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002933 {
2934 /* Reminder if we ever add an AEAD mode with a different size */
2935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2937 }
2938
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002939 /* Group changes to data, data_len, and add_data, because
2940 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002941 data += explicit_iv_len;
2942 rec->data_offset += explicit_iv_len;
2943 rec->data_len -= explicit_iv_len + transform->taglen;
2944
Hanno Beckere83efe62019-04-29 13:52:53 +01002945 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002946 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002947 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002948
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002949 /* Because of the check above, we know that there are
2950 * explicit_iv_len Bytes preceeding data, and taglen
2951 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002952 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002953 * mbedtls_cipher_auth_decrypt() below. */
2954
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002955 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002956 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002957 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002958
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002959 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002960 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002961 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002962 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2963 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002964 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002965 data, rec->data_len,
2966 data, &olen,
2967 data + rec->data_len,
2968 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2973 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002974
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002975 return( ret );
2976 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002977 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002978
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002979 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002980 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2983 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002984 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002985 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002986 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2988#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002989 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002992 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002993
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 /*
Paul Bakker45829992013-01-03 14:52:21 +01002995 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002998 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2999 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003000 {
3001 /* The ciphertext is prefixed with the CBC IV. */
3002 minlen += transform->ivlen;
3003 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003004#endif
Paul Bakker45829992013-01-03 14:52:21 +01003005
Hanno Becker4c6876b2017-12-27 21:28:58 +00003006 /* Size considerations:
3007 *
3008 * - The CBC cipher text must not be empty and hence
3009 * at least of size transform->ivlen.
3010 *
3011 * Together with the potential IV-prefix, this explains
3012 * the first of the two checks below.
3013 *
3014 * - The record must contain a MAC, either in plain or
3015 * encrypted, depending on whether Encrypt-then-MAC
3016 * is used or not.
3017 * - If it is, the message contains the IV-prefix,
3018 * the CBC ciphertext, and the MAC.
3019 * - If it is not, the padded plaintext, and hence
3020 * the CBC ciphertext, has at least length maclen + 1
3021 * because there is at least the padding length byte.
3022 *
3023 * As the CBC ciphertext is not empty, both cases give the
3024 * lower bound minlen + maclen + 1 on the record size, which
3025 * we test for in the second check below.
3026 */
3027 if( rec->data_len < minlen + transform->ivlen ||
3028 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003031 "+ 1 ) ( + expl IV )", rec->data_len,
3032 transform->ivlen,
3033 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003035 }
3036
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003037 /*
3038 * Authenticate before decrypt if enabled
3039 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003041 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003042 {
Hanno Becker992b6872017-11-09 18:57:39 +00003043 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003046
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003047 /* Update data_len in tandem with add_data.
3048 *
3049 * The subtraction is safe because of the previous check
3050 * data_len >= minlen + maclen + 1.
3051 *
3052 * Afterwards, we know that data + data_len is followed by at
3053 * least maclen Bytes, which justifies the call to
3054 * mbedtls_ssl_safer_memcmp() below.
3055 *
3056 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003057 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003058 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003059
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003060 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003061 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3062 add_data_len );
3063 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3064 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003065 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3066 data, rec->data_len );
3067 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3068 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003069
Hanno Becker4c6876b2017-12-27 21:28:58 +00003070 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3071 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003072 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003073 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003074
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003075 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003076 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3077 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003081 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003082 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003083 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003085
3086 /*
3087 * Check length sanity
3088 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003089
3090 /* We know from above that data_len > minlen >= 0,
3091 * so the following check in particular implies that
3092 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003093 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003096 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003098 }
3099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003101 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003103 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003104 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3105 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003106 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003107 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003108 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003109
Hanno Becker4c6876b2017-12-27 21:28:58 +00003110 data += transform->ivlen;
3111 rec->data_offset += transform->ivlen;
3112 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003113 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003115
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003116 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3117
Hanno Becker4c6876b2017-12-27 21:28:58 +00003118 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3119 transform->iv_dec, transform->ivlen,
3120 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003123 return( ret );
3124 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003125
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003126 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003127 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003131 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003134 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3135 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003136 {
3137 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003138 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3139 * records is equivalent to CBC decryption of the concatenation
3140 * of the records; in other words, IVs are maintained across
3141 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003142 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003143 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3144 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003147
Hanno Becker4c6876b2017-12-27 21:28:58 +00003148 /* Safe since data_len >= minlen + maclen + 1, so after having
3149 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003150 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3151 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003152 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003153
Hanno Becker4c6876b2017-12-27 21:28:58 +00003154 if( auth_done == 1 )
3155 {
3156 correct *= ( rec->data_len >= padlen + 1 );
3157 padlen *= ( rec->data_len >= padlen + 1 );
3158 }
3159 else
Paul Bakker45829992013-01-03 14:52:21 +01003160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003162 if( rec->data_len < transform->maclen + padlen + 1 )
3163 {
3164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3165 rec->data_len,
3166 transform->maclen,
3167 padlen + 1 ) );
3168 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003169#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003170
3171 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3172 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003173 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Hanno Becker4c6876b2017-12-27 21:28:58 +00003175 padlen++;
3176
3177 /* Regardless of the validity of the padding,
3178 * we have data_len >= padlen here. */
3179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003181 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3182 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003184 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#if defined(MBEDTLS_SSL_DEBUG_ALL)
3187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003188 "should be no more than %d",
3189 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003190#endif
Paul Bakker45829992013-01-03 14:52:21 +01003191 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 }
3193 }
3194 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3196#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3197 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003198 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3199 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003201 /* The padding check involves a series of up to 256
3202 * consecutive memory reads at the end of the record
3203 * plaintext buffer. In order to hide the length and
3204 * validity of the padding, always perform exactly
3205 * `min(256,plaintext_len)` reads (but take into account
3206 * only the last `padlen` bytes for the padding check). */
3207 size_t pad_count = 0;
3208 size_t real_count = 0;
3209 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003210
Hanno Becker4c6876b2017-12-27 21:28:58 +00003211 /* Index of first padding byte; it has been ensured above
3212 * that the subtraction is safe. */
3213 size_t const padding_idx = rec->data_len - padlen;
3214 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3215 size_t const start_idx = rec->data_len - num_checks;
3216 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003217
Hanno Becker4c6876b2017-12-27 21:28:58 +00003218 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003219 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003220 real_count |= ( idx >= padding_idx );
3221 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003222 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003223 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003226 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003228#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003229 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003230 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003231 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3233 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003237 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003238
Hanno Becker4c6876b2017-12-27 21:28:58 +00003239 /* If the padding was found to be invalid, padlen == 0
3240 * and the subtraction is safe. If the padding was found valid,
3241 * padlen hasn't been changed and the previous assertion
3242 * data_len >= padlen still holds. */
3243 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003245 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003247 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3250 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003251 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003252
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003253#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003255 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
3258 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003259 * Authenticate if not done yet.
3260 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003262#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003263 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003264 {
Hanno Becker992b6872017-11-09 18:57:39 +00003265 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003266
Hanno Becker4c6876b2017-12-27 21:28:58 +00003267 /* If the initial value of padlen was such that
3268 * data_len < maclen + padlen + 1, then padlen
3269 * got reset to 1, and the initial check
3270 * data_len >= minlen + maclen + 1
3271 * guarantees that at this point we still
3272 * have at least data_len >= maclen.
3273 *
3274 * If the initial value of padlen was such that
3275 * data_len >= maclen + padlen + 1, then we have
3276 * subtracted either padlen + 1 (if the padding was correct)
3277 * or 0 (if the padding was incorrect) since then,
3278 * hence data_len >= maclen in any case.
3279 */
3280 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003281 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003284 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3285 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003286 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003287 ssl_mac( &transform->md_ctx_dec,
3288 transform->mac_dec,
3289 data, rec->data_len,
3290 rec->ctr, rec->type,
3291 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003292 }
3293 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3295#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3296 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003297 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3298 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003299 {
3300 /*
3301 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003302 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003303 *
3304 * Known timing attacks:
3305 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3306 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003307 * To compensate for different timings for the MAC calculation
3308 * depending on how much padding was removed (which is determined
3309 * by padlen), process extra_run more blocks through the hash
3310 * function.
3311 *
3312 * The formula in the paper is
3313 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3314 * where L1 is the size of the header plus the decrypted message
3315 * plus CBC padding and L2 is the size of the header plus the
3316 * decrypted message. This is for an underlying hash function
3317 * with 64-byte blocks.
3318 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3319 * correctly. We round down instead of up, so -56 is the correct
3320 * value for our calculations instead of -55.
3321 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003322 * Repeat the formula rather than defining a block_size variable.
3323 * This avoids requiring division by a variable at runtime
3324 * (which would be marginally less efficient and would require
3325 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003326 */
3327 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003328 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003329
3330 /*
3331 * The next two sizes are the minimum and maximum values of
3332 * in_msglen over all padlen values.
3333 *
3334 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003335 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003336 *
3337 * Note that max_len + maclen is never more than the buffer
3338 * length, as we previously did in_msglen -= maclen too.
3339 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003340 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003341 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3342
Hanno Becker4c6876b2017-12-27 21:28:58 +00003343 memset( tmp, 0, sizeof( tmp ) );
3344
3345 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003346 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003347#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3348 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003349 case MBEDTLS_MD_MD5:
3350 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003351 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003352 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003353 extra_run =
3354 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3355 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003356 break;
3357#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003358#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003359 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003360 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003361 extra_run =
3362 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3363 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003364 break;
3365#endif
3366 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003368 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3369 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003370
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003371 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003372
Hanno Beckere83efe62019-04-29 13:52:53 +01003373 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3374 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003375 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3376 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003377 /* Make sure we access everything even when padlen > 0. This
3378 * makes the synchronisation requirements for just-in-time
3379 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003380 ssl_read_memory( data + rec->data_len, padlen );
3381 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003382
3383 /* Call mbedtls_md_process at least once due to cache attacks
3384 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003385 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003386 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003387
Hanno Becker4c6876b2017-12-27 21:28:58 +00003388 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003389
3390 /* Make sure we access all the memory that could contain the MAC,
3391 * before we check it in the next code block. This makes the
3392 * synchronisation requirements for just-in-time Prime+Probe
3393 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003394 ssl_read_memory( data + min_len,
3395 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003396 }
3397 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3399 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003404
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003405#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003406 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3407 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003408#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003409
Hanno Becker4c6876b2017-12-27 21:28:58 +00003410 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3411 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413#if defined(MBEDTLS_SSL_DEBUG_ALL)
3414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003415#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003416 correct = 0;
3417 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003418 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003419 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003420
3421 /*
3422 * Finally check the correct flag
3423 */
3424 if( correct == 0 )
3425 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003426#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003427
3428 /* Make extra sure authentication was performed, exactly once */
3429 if( auth_done != 1 )
3430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3432 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
Hanno Beckera5a2b082019-05-15 14:03:01 +01003435#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003436 if( rec->cid_len != 0 )
3437 {
3438 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3439 &rec->type );
3440 if( ret != 0 )
3441 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3442 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003443#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003446
3447 return( 0 );
3448}
3449
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003450#undef MAC_NONE
3451#undef MAC_PLAINTEXT
3452#undef MAC_CIPHERTEXT
3453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003455/*
3456 * Compression/decompression functions
3457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003459{
3460 int ret;
3461 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003462 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003463 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003464 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003467
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003468 if( len_pre == 0 )
3469 return( 0 );
3470
Paul Bakker2770fbd2012-07-03 13:30:23 +00003471 memcpy( msg_pre, ssl->out_msg, len_pre );
3472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003474 ssl->out_msglen ) );
3475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003476 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477 ssl->out_msg, ssl->out_msglen );
3478
Paul Bakker48916f92012-09-16 19:57:18 +00003479 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3480 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3481 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003482 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003483
Paul Bakker48916f92012-09-16 19:57:18 +00003484 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003485 if( ret != Z_OK )
3486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3488 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003489 }
3490
Angus Grattond8213d02016-05-25 20:56:48 +10003491 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003492 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003495 ssl->out_msglen ) );
3496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003497 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003498 ssl->out_msg, ssl->out_msglen );
3499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003501
3502 return( 0 );
3503}
3504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003505static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003506{
3507 int ret;
3508 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003509 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003510 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003511 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003515 if( len_pre == 0 )
3516 return( 0 );
3517
Paul Bakker2770fbd2012-07-03 13:30:23 +00003518 memcpy( msg_pre, ssl->in_msg, len_pre );
3519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003521 ssl->in_msglen ) );
3522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003524 ssl->in_msg, ssl->in_msglen );
3525
Paul Bakker48916f92012-09-16 19:57:18 +00003526 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3527 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3528 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003529 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003530 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003531
Paul Bakker48916f92012-09-16 19:57:18 +00003532 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003533 if( ret != Z_OK )
3534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3536 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003537 }
3538
Angus Grattond8213d02016-05-25 20:56:48 +10003539 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003540 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003543 ssl->in_msglen ) );
3544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003546 ssl->in_msg, ssl->in_msglen );
3547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003549
3550 return( 0 );
3551}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3555static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003557#if defined(MBEDTLS_SSL_PROTO_DTLS)
3558static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003559{
3560 /* If renegotiation is not enforced, retransmit until we would reach max
3561 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003562 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003563 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003564 uint32_t ratio =
3565 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3566 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003567 unsigned char doublings = 1;
3568
3569 while( ratio != 0 )
3570 {
3571 ++doublings;
3572 ratio >>= 1;
3573 }
3574
3575 if( ++ssl->renego_records_seen > doublings )
3576 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003578 return( 0 );
3579 }
3580 }
3581
3582 return( ssl_write_hello_request( ssl ) );
3583}
3584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003586
Paul Bakker5121ce52009-01-03 21:22:43 +00003587/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003588 * Fill the input message buffer by appending data to it.
3589 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003590 *
3591 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3592 * available (from this read and/or a previous one). Otherwise, an error code
3593 * is returned (possibly EOF or WANT_READ).
3594 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003595 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3596 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3597 * since we always read a whole datagram at once.
3598 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003599 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003600 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003603{
Paul Bakker23986e52011-04-24 08:57:21 +00003604 int ret;
3605 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
Hanno Beckera58a8962019-06-13 16:11:15 +01003609 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3610 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003613 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003615 }
3616
Angus Grattond8213d02016-05-25 20:56:48 +10003617 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003621 }
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003624 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003625 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003626 uint32_t timeout;
3627
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003628 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003629 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3630 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003631 {
3632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3633 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3635 }
3636
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003637 /*
3638 * The point is, we need to always read a full datagram at once, so we
3639 * sometimes read more then requested, and handle the additional data.
3640 * It could be the rest of the current record (while fetching the
3641 * header) and/or some other records in the same datagram.
3642 */
3643
3644 /*
3645 * Move to the next record in the already read datagram if applicable
3646 */
3647 if( ssl->next_record_offset != 0 )
3648 {
3649 if( ssl->in_left < ssl->next_record_offset )
3650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003653 }
3654
3655 ssl->in_left -= ssl->next_record_offset;
3656
3657 if( ssl->in_left != 0 )
3658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003660 ssl->next_record_offset ) );
3661 memmove( ssl->in_hdr,
3662 ssl->in_hdr + ssl->next_record_offset,
3663 ssl->in_left );
3664 }
3665
3666 ssl->next_record_offset = 0;
3667 }
3668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003670 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003671
3672 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003673 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003674 */
3675 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003678 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003679 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003680
3681 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003682 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003683 * are not at the beginning of a new record, the caller did something
3684 * wrong.
3685 */
3686 if( ssl->in_left != 0 )
3687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3689 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003690 }
3691
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003692 /*
3693 * Don't even try to read if time's out already.
3694 * This avoids by-passing the timer when repeatedly receiving messages
3695 * that will end up being dropped.
3696 */
3697 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003698 {
3699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003700 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003701 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003702 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003703 {
Angus Grattond8213d02016-05-25 20:56:48 +10003704 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003706 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003707 timeout = ssl->handshake->retransmit_timeout;
3708 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003709 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003712
Hanno Beckera58a8962019-06-13 16:11:15 +01003713 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3714 {
3715 ret = mbedtls_ssl_get_recv_timeout( ssl )
3716 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3717 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003718 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003719 {
3720 ret = mbedtls_ssl_get_recv( ssl )
3721 ( ssl->p_bio, ssl->in_hdr, len );
3722 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003725
3726 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003728 }
3729
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003730 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003733 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003736 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003737 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003740 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003741 }
3742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003746 return( ret );
3747 }
3748
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003749 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003750 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003752 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3753 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003755 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003756 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003759 return( ret );
3760 }
3761
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003762 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003763 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003765 }
3766
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 if( ret < 0 )
3768 return( ret );
3769
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003770 ssl->in_left = ret;
3771 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003772 MBEDTLS_SSL_TRANSPORT_ELSE
3773#endif /* MBEDTLS_SSL_PROTO_DTLS */
3774#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003777 ssl->in_left, nb_want ) );
3778
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003779 while( ssl->in_left < nb_want )
3780 {
3781 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003782
3783 if( ssl_check_timer( ssl ) != 0 )
3784 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3785 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003786 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003787 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003788 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003789 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3790 ssl->in_hdr + ssl->in_left, len,
3791 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003792 }
3793 else
3794 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003795 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003796 ssl->in_hdr + ssl->in_left, len );
3797 }
3798 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003801 ssl->in_left, nb_want ) );
3802 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003803
3804 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003806
3807 if( ret < 0 )
3808 return( ret );
3809
mohammad160352aecb92018-03-28 23:41:40 -07003810 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003811 {
Darryl Green11999bb2018-03-13 15:22:58 +00003812 MBEDTLS_SSL_DEBUG_MSG( 1,
3813 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003814 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003815 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3816 }
3817
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003818 ssl->in_left += ret;
3819 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003820 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003821#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003824
3825 return( 0 );
3826}
3827
3828/*
3829 * Flush any data not yet written
3830 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003831int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003832{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003833 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003834 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003837
Hanno Beckera58a8962019-06-13 16:11:15 +01003838 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003841 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003843 }
3844
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003845 /* Avoid incrementing counter if data is flushed */
3846 if( ssl->out_left == 0 )
3847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003849 return( 0 );
3850 }
3851
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 while( ssl->out_left > 0 )
3853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003855 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856
Hanno Becker2b1e3542018-08-06 11:19:13 +01003857 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003858 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
3862 if( ret <= 0 )
3863 return( ret );
3864
mohammad160352aecb92018-03-28 23:41:40 -07003865 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003866 {
Darryl Green11999bb2018-03-13 15:22:58 +00003867 MBEDTLS_SSL_DEBUG_MSG( 1,
3868 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003869 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003870 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3871 }
3872
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 ssl->out_left -= ret;
3874 }
3875
Hanno Becker2b1e3542018-08-06 11:19:13 +01003876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003877 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003878 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003879 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003880 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003881 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003882#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003883#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003884 {
3885 ssl->out_hdr = ssl->out_buf + 8;
3886 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003887#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003888 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003891
3892 return( 0 );
3893}
3894
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003895/*
3896 * Functions to handle the DTLS retransmission state machine
3897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003899/*
3900 * Append current handshake message to current outgoing flight
3901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3906 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3907 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003908
3909 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003910 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003911 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003914 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003915 }
3916
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003917 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003918 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003921 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003922 }
3923
3924 /* Copy current handshake message with headers */
3925 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3926 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003927 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928 msg->next = NULL;
3929
3930 /* Append to the current flight */
3931 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003932 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003933 else
3934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003935 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003936 while( cur->next != NULL )
3937 cur = cur->next;
3938 cur->next = msg;
3939 }
3940
Hanno Becker3b235902018-08-06 09:54:53 +01003941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003942 return( 0 );
3943}
3944
3945/*
3946 * Free the current flight of handshake messages
3947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003949{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 mbedtls_ssl_flight_item *cur = flight;
3951 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952
3953 while( cur != NULL )
3954 {
3955 next = cur->next;
3956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 mbedtls_free( cur->p );
3958 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003959
3960 cur = next;
3961 }
3962}
3963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003964#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3965static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003966#endif
3967
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003968/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003969 * Swap transform_out and out_ctr with the alternative ones
3970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003974 unsigned char tmp_out_ctr[8];
3975
3976 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003979 return;
3980 }
3981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003983
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003984 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003985 tmp_transform = ssl->transform_out;
3986 ssl->transform_out = ssl->handshake->alt_transform_out;
3987 ssl->handshake->alt_transform_out = tmp_transform;
3988
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003989 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003990 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3991 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003992 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003993
3994 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003995 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3998 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4003 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004004 }
4005 }
4006#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004007}
4008
4009/*
4010 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004011 */
4012int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4013{
4014 int ret = 0;
4015
4016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4017
4018 ret = mbedtls_ssl_flight_transmit( ssl );
4019
4020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4021
4022 return( ret );
4023}
4024
4025/*
4026 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004027 *
4028 * Need to remember the current message in case flush_output returns
4029 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004030 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004031 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004032int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004034 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004038 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004040
4041 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004042 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004043 ssl_swap_epochs( ssl );
4044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004046 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004047
4048 while( ssl->handshake->cur_msg != NULL )
4049 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004050 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004051 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004052
Hanno Beckere1dcb032018-08-17 16:47:58 +01004053 int const is_finished =
4054 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4055 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4056
Hanno Becker04da1892018-08-14 13:22:10 +01004057 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4058 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4059
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004060 /* Swap epochs before sending Finished: we can't do it after
4061 * sending ChangeCipherSpec, in case write returns WANT_READ.
4062 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004063 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004064 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004066 ssl_swap_epochs( ssl );
4067 }
4068
Hanno Becker67bc7c32018-08-06 11:33:50 +01004069 ret = ssl_get_remaining_payload_in_datagram( ssl );
4070 if( ret < 0 )
4071 return( ret );
4072 max_frag_len = (size_t) ret;
4073
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004074 /* CCS is copied as is, while HS messages may need fragmentation */
4075 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4076 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004077 if( max_frag_len == 0 )
4078 {
4079 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4080 return( ret );
4081
4082 continue;
4083 }
4084
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004085 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004086 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004087 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004088
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004089 /* Update position inside current message */
4090 ssl->handshake->cur_msg_p += cur->len;
4091 }
4092 else
4093 {
4094 const unsigned char * const p = ssl->handshake->cur_msg_p;
4095 const size_t hs_len = cur->len - 12;
4096 const size_t frag_off = p - ( cur->p + 12 );
4097 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004098 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004099
Hanno Beckere1dcb032018-08-17 16:47:58 +01004100 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004101 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004102 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004103 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004104
Hanno Becker67bc7c32018-08-06 11:33:50 +01004105 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4106 return( ret );
4107
4108 continue;
4109 }
4110 max_hs_frag_len = max_frag_len - 12;
4111
4112 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4113 max_hs_frag_len : rem_len;
4114
4115 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004116 {
4117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004118 (unsigned) cur_hs_frag_len,
4119 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004120 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004121
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004122 /* Messages are stored with handshake headers as if not fragmented,
4123 * copy beginning of headers then fill fragmentation fields.
4124 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4125 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004126
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004127 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4128 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4129 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4130
Hanno Becker67bc7c32018-08-06 11:33:50 +01004131 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4132 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4133 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004134
4135 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4136
Hanno Becker3f7b9732018-08-28 09:53:25 +01004137 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004138 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4139 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004140 ssl->out_msgtype = cur->type;
4141
4142 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004143 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004144 }
4145
4146 /* If done with the current message move to the next one if any */
4147 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4148 {
4149 if( cur->next != NULL )
4150 {
4151 ssl->handshake->cur_msg = cur->next;
4152 ssl->handshake->cur_msg_p = cur->next->p + 12;
4153 }
4154 else
4155 {
4156 ssl->handshake->cur_msg = NULL;
4157 ssl->handshake->cur_msg_p = NULL;
4158 }
4159 }
4160
4161 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004162 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004165 return( ret );
4166 }
4167 }
4168
Hanno Becker67bc7c32018-08-06 11:33:50 +01004169 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4170 return( ret );
4171
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004172 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4174 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004175 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004178 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4179 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004180
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004182
4183 return( 0 );
4184}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004185
4186/*
4187 * To be called when the last message of an incoming flight is received.
4188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004189void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004190{
4191 /* We won't need to resend that one any more */
4192 ssl_flight_free( ssl->handshake->flight );
4193 ssl->handshake->flight = NULL;
4194 ssl->handshake->cur_msg = NULL;
4195
4196 /* The next incoming flight will start with this msg_seq */
4197 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4198
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004199 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004200 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004201
Hanno Becker0271f962018-08-16 13:23:47 +01004202 /* Clear future message buffering structure. */
4203 ssl_buffering_free( ssl );
4204
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004205 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004206 ssl_set_timer( ssl, 0 );
4207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004208 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4209 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004212 }
4213 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004215}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004216
4217/*
4218 * To be called when the last message of an outgoing flight is send.
4219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004221{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004222 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004223 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4226 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004229 }
4230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004234
Paul Bakker5121ce52009-01-03 21:22:43 +00004235/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004236 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004237 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004238
4239/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004240 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004241 *
4242 * - fill in handshake headers
4243 * - update handshake checksum
4244 * - DTLS: save message for resending
4245 * - then pass to the record layer
4246 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004247 * DTLS: except for HelloRequest, messages are only queued, and will only be
4248 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004249 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004250 * Inputs:
4251 * - ssl->out_msglen: 4 + actual handshake message len
4252 * (4 is the size of handshake headers for TLS)
4253 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4254 * - ssl->out_msg + 4: the handshake message body
4255 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004256 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004257 * - ssl->out_msglen: the length of the record contents
4258 * (including handshake headers but excluding record headers)
4259 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004260 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004261int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004262{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004263 int ret;
4264 const size_t hs_len = ssl->out_msglen - 4;
4265 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004266
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4268
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004269 /*
4270 * Sanity checks
4271 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004272 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004273 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4274 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004275 /* In SSLv3, the client might send a NoCertificate alert. */
4276#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004277 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004278 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004279 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4280 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004281#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4282 {
4283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4285 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004287
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004288 /* Whenever we send anything different from a
4289 * HelloRequest we should be in a handshake - double check. */
4290 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4291 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004292 ssl->handshake == NULL )
4293 {
4294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4295 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004299 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004300 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004301 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004302 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004305 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004306#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004307
Hanno Beckerb50a2532018-08-06 11:52:54 +01004308 /* Double-check that we did not exceed the bounds
4309 * of the outgoing record buffer.
4310 * This should never fail as the various message
4311 * writing functions must obey the bounds of the
4312 * outgoing record buffer, but better be safe.
4313 *
4314 * Note: We deliberately do not check for the MTU or MFL here.
4315 */
4316 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4317 {
4318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4319 "size %u, maximum %u",
4320 (unsigned) ssl->out_msglen,
4321 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4322 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4323 }
4324
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004325 /*
4326 * Fill handshake headers
4327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004329 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004330 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4331 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4332 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004334 /*
4335 * DTLS has additional fields in the Handshake layer,
4336 * between the length field and the actual payload:
4337 * uint16 message_seq;
4338 * uint24 fragment_offset;
4339 * uint24 fragment_length;
4340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004342 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004343 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004344 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004345 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004346 {
4347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4348 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004349 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004350 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4352 }
4353
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004354 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004355 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004356
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004357 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004358 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004359 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004360 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4361 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4362 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004363 }
4364 else
4365 {
4366 ssl->out_msg[4] = 0;
4367 ssl->out_msg[5] = 0;
4368 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004369
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004370 /* Handshake hashes are computed without fragmentation,
4371 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004372 memset( ssl->out_msg + 6, 0x00, 3 );
4373 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004376
Hanno Becker0207e532018-08-28 10:28:28 +01004377 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004378 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004379 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004380 }
4381
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004382 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004384 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004385 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4386 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004387 {
4388 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004390 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004391 return( ret );
4392 }
4393 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004394 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004395#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004396 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004397 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004398 {
4399 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4400 return( ret );
4401 }
4402 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004403
4404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4405
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004406 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004407}
4408
4409/*
4410 * Record layer functions
4411 */
4412
4413/*
4414 * Write current record.
4415 *
4416 * Uses:
4417 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4418 * - ssl->out_msglen: length of the record content (excl headers)
4419 * - ssl->out_msg: record content
4420 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004421int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004422{
4423 int ret, done = 0;
4424 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004425 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004426
4427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004430 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004432 {
4433 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004436 return( ret );
4437 }
4438
4439 len = ssl->out_msglen;
4440 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004443#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4444 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004448 ret = mbedtls_ssl_hw_record_write( ssl );
4449 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4452 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004453 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004454
4455 if( ret == 0 )
4456 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004459 if( !done )
4460 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004461 unsigned i;
4462 size_t protected_record_size;
4463
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004464 /* Skip writing the record content type to after the encryption,
4465 * as it may change when using the CID extension. */
4466
Hanno Becker2881d802019-05-22 14:44:53 +01004467 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4468 mbedtls_ssl_get_minor_ver( ssl ),
4469 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004470
Hanno Becker19859472018-08-06 09:40:20 +01004471 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004472 ssl->out_len[0] = (unsigned char)( len >> 8 );
4473 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004474
Paul Bakker48916f92012-09-16 19:57:18 +00004475 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004476 {
Hanno Becker3307b532017-12-27 21:37:21 +00004477 mbedtls_record rec;
4478
4479 rec.buf = ssl->out_iv;
4480 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4481 ( ssl->out_iv - ssl->out_buf );
4482 rec.data_len = ssl->out_msglen;
4483 rec.data_offset = ssl->out_msg - rec.buf;
4484
4485 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004486 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4487 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004488 ssl->conf->transport, rec.ver );
4489 rec.type = ssl->out_msgtype;
4490
Hanno Beckera5a2b082019-05-15 14:03:01 +01004491#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004492 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004493 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004494#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004495
Hanno Becker611a83b2018-01-03 14:27:32 +00004496 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004497 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004498 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004501 return( ret );
4502 }
4503
Hanno Becker3307b532017-12-27 21:37:21 +00004504 if( rec.data_offset != 0 )
4505 {
4506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4507 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4508 }
4509
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004510 /* Update the record content type and CID. */
4511 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004512#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004513 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004514#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004515 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004516 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4517 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004518 }
4519
Hanno Becker43395762019-05-03 14:46:38 +01004520 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004521
4522#if defined(MBEDTLS_SSL_PROTO_DTLS)
4523 /* In case of DTLS, double-check that we don't exceed
4524 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004525 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004526 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004527 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004528 if( ret < 0 )
4529 return( ret );
4530
4531 if( protected_record_size > (size_t) ret )
4532 {
4533 /* Should never happen */
4534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4535 }
4536 }
4537#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004538
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004539 /* Now write the potentially updated record content type. */
4540 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004543 "version = [%d:%d], msglen = %d",
4544 ssl->out_hdr[0], ssl->out_hdr[1],
4545 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004548 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004549
4550 ssl->out_left += protected_record_size;
4551 ssl->out_hdr += protected_record_size;
4552 ssl_update_out_pointers( ssl, ssl->transform_out );
4553
Hanno Becker04484622018-08-06 09:49:38 +01004554 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4555 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4556 break;
4557
4558 /* The loop goes to its end iff the counter is wrapping */
4559 if( i == ssl_ep_len( ssl ) )
4560 {
4561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4562 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 }
4565
Hanno Becker67bc7c32018-08-06 11:33:50 +01004566#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004567 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004568 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004569 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004570 size_t remaining;
4571 ret = ssl_get_remaining_payload_in_datagram( ssl );
4572 if( ret < 0 )
4573 {
4574 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4575 ret );
4576 return( ret );
4577 }
4578
4579 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004580 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004581 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004582 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004583 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004584 else
4585 {
Hanno Becker513815a2018-08-20 11:56:09 +01004586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004587 }
4588 }
4589#endif /* MBEDTLS_SSL_PROTO_DTLS */
4590
4591 if( ( flush == SSL_FORCE_FLUSH ) &&
4592 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004595 return( ret );
4596 }
4597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004599
4600 return( 0 );
4601}
4602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004604
4605static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4606{
4607 if( ssl->in_msglen < ssl->in_hslen ||
4608 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4609 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4610 {
4611 return( 1 );
4612 }
4613 return( 0 );
4614}
Hanno Becker44650b72018-08-16 12:51:11 +01004615
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004616static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004617{
4618 return( ( ssl->in_msg[9] << 16 ) |
4619 ( ssl->in_msg[10] << 8 ) |
4620 ssl->in_msg[11] );
4621}
4622
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004623static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004624{
4625 return( ( ssl->in_msg[6] << 16 ) |
4626 ( ssl->in_msg[7] << 8 ) |
4627 ssl->in_msg[8] );
4628}
4629
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004630static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004631{
4632 uint32_t msg_len, frag_off, frag_len;
4633
4634 msg_len = ssl_get_hs_total_len( ssl );
4635 frag_off = ssl_get_hs_frag_off( ssl );
4636 frag_len = ssl_get_hs_frag_len( ssl );
4637
4638 if( frag_off > msg_len )
4639 return( -1 );
4640
4641 if( frag_len > msg_len - frag_off )
4642 return( -1 );
4643
4644 if( frag_len + 12 > ssl->in_msglen )
4645 return( -1 );
4646
4647 return( 0 );
4648}
4649
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004650/*
4651 * Mark bits in bitmask (used for DTLS HS reassembly)
4652 */
4653static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4654{
4655 unsigned int start_bits, end_bits;
4656
4657 start_bits = 8 - ( offset % 8 );
4658 if( start_bits != 8 )
4659 {
4660 size_t first_byte_idx = offset / 8;
4661
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004662 /* Special case */
4663 if( len <= start_bits )
4664 {
4665 for( ; len != 0; len-- )
4666 mask[first_byte_idx] |= 1 << ( start_bits - len );
4667
4668 /* Avoid potential issues with offset or len becoming invalid */
4669 return;
4670 }
4671
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004672 offset += start_bits; /* Now offset % 8 == 0 */
4673 len -= start_bits;
4674
4675 for( ; start_bits != 0; start_bits-- )
4676 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4677 }
4678
4679 end_bits = len % 8;
4680 if( end_bits != 0 )
4681 {
4682 size_t last_byte_idx = ( offset + len ) / 8;
4683
4684 len -= end_bits; /* Now len % 8 == 0 */
4685
4686 for( ; end_bits != 0; end_bits-- )
4687 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4688 }
4689
4690 memset( mask + offset / 8, 0xFF, len / 8 );
4691}
4692
4693/*
4694 * Check that bitmask is full
4695 */
4696static int ssl_bitmask_check( unsigned char *mask, size_t len )
4697{
4698 size_t i;
4699
4700 for( i = 0; i < len / 8; i++ )
4701 if( mask[i] != 0xFF )
4702 return( -1 );
4703
4704 for( i = 0; i < len % 8; i++ )
4705 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4706 return( -1 );
4707
4708 return( 0 );
4709}
4710
Hanno Becker56e205e2018-08-16 09:06:12 +01004711/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004712static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004713 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004714{
Hanno Becker56e205e2018-08-16 09:06:12 +01004715 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004716
Hanno Becker56e205e2018-08-16 09:06:12 +01004717 alloc_len = 12; /* Handshake header */
4718 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004719
Hanno Beckerd07df862018-08-16 09:14:58 +01004720 if( add_bitmap )
4721 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004722
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004723 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004724}
Hanno Becker56e205e2018-08-16 09:06:12 +01004725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004727
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004728static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004729{
4730 return( ( ssl->in_msg[1] << 16 ) |
4731 ( ssl->in_msg[2] << 8 ) |
4732 ssl->in_msg[3] );
4733}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004734
Simon Butcher99000142016-10-13 17:21:01 +01004735int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004740 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004742 }
4743
Hanno Becker12555c62018-08-16 12:47:53 +01004744 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004747 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004748 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004751 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004752 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004753 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004754 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004755
Hanno Becker44650b72018-08-16 12:51:11 +01004756 if( ssl_check_hs_header( ssl ) != 0 )
4757 {
4758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4759 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4760 }
4761
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004762 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004763 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4764 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4765 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4766 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004767 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004768 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4769 {
4770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4771 recv_msg_seq,
4772 ssl->handshake->in_msg_seq ) );
4773 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4774 }
4775
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004776 /* Retransmit only on last message from previous flight, to avoid
4777 * too many retransmissions.
4778 * Besides, No sane server ever retransmits HelloVerifyRequest */
4779 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004783 "message_seq = %d, start_of_flight = %d",
4784 recv_msg_seq,
4785 ssl->handshake->in_flight_start_seq ) );
4786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004787 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004790 return( ret );
4791 }
4792 }
4793 else
4794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004796 "message_seq = %d, expected = %d",
4797 recv_msg_seq,
4798 ssl->handshake->in_msg_seq ) );
4799 }
4800
Hanno Becker90333da2017-10-10 11:27:13 +01004801 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004802 }
4803 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004804
Hanno Becker6d97ef52018-08-16 13:09:04 +01004805 /* Message reassembly is handled alongside buffering of future
4806 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004807 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004808 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004809 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004812 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004813 }
4814 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004815 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004817#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004818 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004819 /* With TLS we don't handle fragmentation (for now) */
4820 if( ssl->in_msglen < ssl->in_hslen )
4821 {
4822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4823 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4824 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004825 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004826#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004827
Simon Butcher99000142016-10-13 17:21:01 +01004828 return( 0 );
4829}
4830
4831void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4832{
Hanno Becker0271f962018-08-16 13:23:47 +01004833 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004834
Hanno Becker0271f962018-08-16 13:23:47 +01004835 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004836 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004837
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004838 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004839#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004840 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004841 ssl->handshake != NULL )
4842 {
Hanno Becker0271f962018-08-16 13:23:47 +01004843 unsigned offset;
4844 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004845
Hanno Becker0271f962018-08-16 13:23:47 +01004846 /* Increment handshake sequence number */
4847 hs->in_msg_seq++;
4848
4849 /*
4850 * Clear up handshake buffering and reassembly structure.
4851 */
4852
4853 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004854 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004855
4856 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004857 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4858 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004859 offset++, hs_buf++ )
4860 {
4861 *hs_buf = *(hs_buf + 1);
4862 }
4863
4864 /* Create a fresh last entry */
4865 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004866 }
4867#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004868}
4869
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004870/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004871 * DTLS anti-replay: RFC 6347 4.1.2.6
4872 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004873 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4874 * Bit n is set iff record number in_window_top - n has been seen.
4875 *
4876 * Usually, in_window_top is the last record number seen and the lsb of
4877 * in_window is set. The only exception is the initial state (record number 0
4878 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004880#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4881static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004882{
4883 ssl->in_window_top = 0;
4884 ssl->in_window = 0;
4885}
4886
4887static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4888{
4889 return( ( (uint64_t) buf[0] << 40 ) |
4890 ( (uint64_t) buf[1] << 32 ) |
4891 ( (uint64_t) buf[2] << 24 ) |
4892 ( (uint64_t) buf[3] << 16 ) |
4893 ( (uint64_t) buf[4] << 8 ) |
4894 ( (uint64_t) buf[5] ) );
4895}
4896
4897/*
4898 * Return 0 if sequence number is acceptable, -1 otherwise
4899 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004900int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004901{
4902 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4903 uint64_t bit;
4904
Hanno Becker7f376f42019-06-12 16:20:48 +01004905 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4906 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4907 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004908 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004909 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004910
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004911 if( rec_seqnum > ssl->in_window_top )
4912 return( 0 );
4913
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004914 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004915
4916 if( bit >= 64 )
4917 return( -1 );
4918
4919 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4920 return( -1 );
4921
4922 return( 0 );
4923}
4924
4925/*
4926 * Update replay window on new validated record
4927 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004929{
4930 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4931
Hanno Becker7f376f42019-06-12 16:20:48 +01004932 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4933 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4934 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004935 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004936 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004937
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004938 if( rec_seqnum > ssl->in_window_top )
4939 {
4940 /* Update window_top and the contents of the window */
4941 uint64_t shift = rec_seqnum - ssl->in_window_top;
4942
4943 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004944 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004945 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004946 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004947 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004948 ssl->in_window |= 1;
4949 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004950
4951 ssl->in_window_top = rec_seqnum;
4952 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004953 else
4954 {
4955 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004956 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004957
4958 if( bit < 64 ) /* Always true, but be extra sure */
4959 ssl->in_window |= (uint64_t) 1 << bit;
4960 }
4961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004962#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004963
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004964#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004965/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004966static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4967
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004968/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004969 * Without any SSL context, check if a datagram looks like a ClientHello with
4970 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004971 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004972 *
4973 * - if cookie is valid, return 0
4974 * - if ClientHello looks superficially valid but cookie is not,
4975 * fill obuf and set olen, then
4976 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4977 * - otherwise return a specific error code
4978 */
4979static int ssl_check_dtls_clihlo_cookie(
4980 mbedtls_ssl_cookie_write_t *f_cookie_write,
4981 mbedtls_ssl_cookie_check_t *f_cookie_check,
4982 void *p_cookie,
4983 const unsigned char *cli_id, size_t cli_id_len,
4984 const unsigned char *in, size_t in_len,
4985 unsigned char *obuf, size_t buf_len, size_t *olen )
4986{
4987 size_t sid_len, cookie_len;
4988 unsigned char *p;
4989
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004990 /*
4991 * Structure of ClientHello with record and handshake headers,
4992 * and expected values. We don't need to check a lot, more checks will be
4993 * done when actually parsing the ClientHello - skipping those checks
4994 * avoids code duplication and does not make cookie forging any easier.
4995 *
4996 * 0-0 ContentType type; copied, must be handshake
4997 * 1-2 ProtocolVersion version; copied
4998 * 3-4 uint16 epoch; copied, must be 0
4999 * 5-10 uint48 sequence_number; copied
5000 * 11-12 uint16 length; (ignored)
5001 *
5002 * 13-13 HandshakeType msg_type; (ignored)
5003 * 14-16 uint24 length; (ignored)
5004 * 17-18 uint16 message_seq; copied
5005 * 19-21 uint24 fragment_offset; copied, must be 0
5006 * 22-24 uint24 fragment_length; (ignored)
5007 *
5008 * 25-26 ProtocolVersion client_version; (ignored)
5009 * 27-58 Random random; (ignored)
5010 * 59-xx SessionID session_id; 1 byte len + sid_len content
5011 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5012 * ...
5013 *
5014 * Minimum length is 61 bytes.
5015 */
5016 if( in_len < 61 ||
5017 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5018 in[3] != 0 || in[4] != 0 ||
5019 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5020 {
5021 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5022 }
5023
5024 sid_len = in[59];
5025 if( sid_len > in_len - 61 )
5026 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5027
5028 cookie_len = in[60 + sid_len];
5029 if( cookie_len > in_len - 60 )
5030 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5031
5032 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5033 cli_id, cli_id_len ) == 0 )
5034 {
5035 /* Valid cookie */
5036 return( 0 );
5037 }
5038
5039 /*
5040 * If we get here, we've got an invalid cookie, let's prepare HVR.
5041 *
5042 * 0-0 ContentType type; copied
5043 * 1-2 ProtocolVersion version; copied
5044 * 3-4 uint16 epoch; copied
5045 * 5-10 uint48 sequence_number; copied
5046 * 11-12 uint16 length; olen - 13
5047 *
5048 * 13-13 HandshakeType msg_type; hello_verify_request
5049 * 14-16 uint24 length; olen - 25
5050 * 17-18 uint16 message_seq; copied
5051 * 19-21 uint24 fragment_offset; copied
5052 * 22-24 uint24 fragment_length; olen - 25
5053 *
5054 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5055 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5056 *
5057 * Minimum length is 28.
5058 */
5059 if( buf_len < 28 )
5060 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5061
5062 /* Copy most fields and adapt others */
5063 memcpy( obuf, in, 25 );
5064 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5065 obuf[25] = 0xfe;
5066 obuf[26] = 0xff;
5067
5068 /* Generate and write actual cookie */
5069 p = obuf + 28;
5070 if( f_cookie_write( p_cookie,
5071 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5072 {
5073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5074 }
5075
5076 *olen = p - obuf;
5077
5078 /* Go back and fill length fields */
5079 obuf[27] = (unsigned char)( *olen - 28 );
5080
5081 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5082 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5083 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5084
5085 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5086 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5087
5088 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5089}
5090
5091/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005092 * Handle possible client reconnect with the same UDP quadruplet
5093 * (RFC 6347 Section 4.2.8).
5094 *
5095 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5096 * that looks like a ClientHello.
5097 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005098 * - if the input looks like a ClientHello without cookies,
5099 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005100 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005101 * - if the input looks like a ClientHello with a valid cookie,
5102 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005103 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005104 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005105 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005106 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005107 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5108 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005109 */
5110static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5111{
5112 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005113 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005114
Hanno Becker87b56262019-07-10 14:37:41 +01005115 if( ssl->conf->f_cookie_write == NULL ||
5116 ssl->conf->f_cookie_check == NULL )
5117 {
5118 /* If we can't use cookies to verify reachability of the peer,
5119 * drop the record. */
5120 return( 0 );
5121 }
5122
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005123 ret = ssl_check_dtls_clihlo_cookie(
5124 ssl->conf->f_cookie_write,
5125 ssl->conf->f_cookie_check,
5126 ssl->conf->p_cookie,
5127 ssl->cli_id, ssl->cli_id_len,
5128 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005129 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005130
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005131 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5132
5133 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005134 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005135 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005136 * If the error is permanent we'll catch it later,
5137 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005138 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005139 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005140 }
5141
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005142 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005143 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005144 /* Got a valid cookie, partially reset context */
5145 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5146 {
5147 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5148 return( ret );
5149 }
5150
5151 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005152 }
5153
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005154 return( ret );
5155}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005156#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005157
Hanno Becker46483f12019-05-03 13:25:54 +01005158static int ssl_check_record_type( uint8_t record_type )
5159{
5160 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5161 record_type != MBEDTLS_SSL_MSG_ALERT &&
5162 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5163 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5164 {
5165 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5166 }
5167
5168 return( 0 );
5169}
5170
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005171/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005172 * ContentType type;
5173 * ProtocolVersion version;
5174 * uint16 epoch; // DTLS only
5175 * uint48 sequence_number; // DTLS only
5176 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005177 *
5178 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005179 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005180 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5181 *
5182 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005183 * 1. proceed with the record if this function returns 0
5184 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5185 * 3. return CLIENT_RECONNECT if this function return that value
5186 * 4. drop the whole datagram if this function returns anything else.
5187 * Point 2 is needed when the peer is resending, and we have already received
5188 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005189 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005190static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005191 unsigned char *buf,
5192 size_t len,
5193 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005194{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005195 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005196
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005197 size_t const rec_hdr_type_offset = 0;
5198 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005199
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005200 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5201 rec_hdr_type_len;
5202 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005203
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005204 size_t const rec_hdr_ctr_len = 8;
5205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005206 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005207 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5208 rec_hdr_version_len;
5209
Hanno Beckera5a2b082019-05-15 14:03:01 +01005210#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005211 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5212 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005213 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005214#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5215#endif /* MBEDTLS_SSL_PROTO_DTLS */
5216
5217 size_t rec_hdr_len_offset; /* To be determined */
5218 size_t const rec_hdr_len_len = 2;
5219
5220 /*
5221 * Check minimum lengths for record header.
5222 */
5223
5224#if defined(MBEDTLS_SSL_PROTO_DTLS)
5225 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5226 {
5227 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5228 }
5229 MBEDTLS_SSL_TRANSPORT_ELSE
5230#endif /* MBEDTLS_SSL_PROTO_DTLS */
5231#if defined(MBEDTLS_SSL_PROTO_TLS)
5232 {
5233 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5234 }
5235#endif /* MBEDTLS_SSL_PROTO_DTLS */
5236
5237 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5238 {
5239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5240 (unsigned) len,
5241 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5242 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5243 }
5244
5245 /*
5246 * Parse and validate record content type
5247 */
5248
5249 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005250
5251 /* Check record content type */
5252#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5253 rec->cid_len = 0;
5254
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005255 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005256 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5257 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005258 {
5259 /* Shift pointers to account for record header including CID
5260 * struct {
5261 * ContentType special_type = tls12_cid;
5262 * ProtocolVersion version;
5263 * uint16 epoch;
5264 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005265 * opaque cid[cid_length]; // Additional field compared to
5266 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005267 * uint16 length;
5268 * opaque enc_content[DTLSCiphertext.length];
5269 * } DTLSCiphertext;
5270 */
5271
5272 /* So far, we only support static CID lengths
5273 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005274 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5275 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005276
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005277 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005278 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5280 (unsigned) len,
5281 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005282 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005283 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005284
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005285 /* configured CID len is guaranteed at most 255, see
5286 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5287 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005288 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005289 }
5290 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005291#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005292 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005293 if( ssl_check_record_type( rec->type ) )
5294 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5296 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005297 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5298 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005299 }
5300
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005301 /*
5302 * Parse and validate record version
5303 */
5304
Hanno Becker8061c6e2019-07-26 08:07:03 +01005305 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5306 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005307 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5308 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005309 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005310
Hanno Becker2881d802019-05-22 14:44:53 +01005311 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5314 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005315 }
5316
Hanno Beckere965bd32019-06-12 14:04:34 +01005317 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5320 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 }
5322
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005323 /*
5324 * Parse/Copy record sequence number.
5325 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005326
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005327#if defined(MBEDTLS_SSL_PROTO_DTLS)
5328 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5329 {
5330 /* Copy explicit record sequence number from input buffer. */
5331 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5332 rec_hdr_ctr_len );
5333 }
5334 MBEDTLS_SSL_TRANSPORT_ELSE
5335#endif /* MBEDTLS_SSL_PROTO_DTLS */
5336#if defined(MBEDTLS_SSL_PROTO_TLS)
5337 {
5338 /* Copy implicit record sequence number from SSL context structure. */
5339 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5340 }
5341#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005342
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005343 /*
5344 * Parse record length.
5345 */
5346
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005347 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005348 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5349 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005350 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5351
Hanno Becker8b09b732019-05-08 12:03:28 +01005352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005353 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005354 rec->type,
5355 major_ver, minor_ver, rec->data_len ) );
5356
5357 rec->buf = buf;
5358 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005359
Hanno Beckerec014082019-07-26 08:20:27 +01005360 if( rec->data_len == 0 )
5361 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5362
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005363 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005364 * DTLS-related tests.
5365 * Check epoch before checking length constraint because
5366 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5367 * message gets duplicated before the corresponding Finished message,
5368 * the second ChangeCipherSpec should be discarded because it belongs
5369 * to an old epoch, but not because its length is shorter than
5370 * the minimum record length for packets using the new record transform.
5371 * Note that these two kinds of failures are handled differently,
5372 * as an unexpected record is silently skipped but an invalid
5373 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005374 */
5375#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005376 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005377 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005378 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005379
Hanno Beckere0452772019-07-10 17:12:07 +01005380 /* Check that the datagram is large enough to contain a record
5381 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005382 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005383 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5385 (unsigned) len,
5386 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005387 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5388 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005389
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005390 /* Records from other, non-matching epochs are silently discarded.
5391 * (The case of same-port Client reconnects must be considered in
5392 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005393 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005394 {
5395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5396 "expected %d, received %d",
5397 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005398
5399 /* Records from the next epoch are considered for buffering
5400 * (concretely: early Finished messages). */
5401 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5402 {
5403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5404 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5405 }
5406
Hanno Becker87b56262019-07-10 14:37:41 +01005407 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005408 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005409#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005410 /* For records from the correct epoch, check whether their
5411 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005412 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005413 {
5414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5415 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5416 }
5417#endif
5418 }
5419#endif /* MBEDTLS_SSL_PROTO_DTLS */
5420
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005421 return( 0 );
5422}
Paul Bakker5121ce52009-01-03 21:22:43 +00005423
Hanno Becker87b56262019-07-10 14:37:41 +01005424
5425#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5426static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5427{
5428 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5429
5430 /*
5431 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5432 * access the first byte of record content (handshake type), as we
5433 * have an active transform (possibly iv_len != 0), so use the
5434 * fact that the record header len is 13 instead.
5435 */
5436 if( rec_epoch == 0 &&
5437 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5438 MBEDTLS_SSL_IS_SERVER &&
5439 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5440 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5441 ssl->in_left > 13 &&
5442 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5443 {
5444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5445 "from the same port" ) );
5446 return( ssl_handle_possible_reconnect( ssl ) );
5447 }
5448
5449 return( 0 );
5450}
5451#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5452
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005453/*
5454 * If applicable, decrypt (and decompress) record content
5455 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005456static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5457 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005458{
5459 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005462 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5465 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 ret = mbedtls_ssl_hw_record_read( ssl );
5470 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5473 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005474 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005475
5476 if( ret == 0 )
5477 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005478 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005480 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005482 unsigned char const old_msg_type = rec->type;
5483
Hanno Becker611a83b2018-01-03 14:27:32 +00005484 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005485 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005488
Hanno Beckera5a2b082019-05-15 14:03:01 +01005489#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005490 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005491 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5492 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005493 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005495 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005496 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005497#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005498
Paul Bakker5121ce52009-01-03 21:22:43 +00005499 return( ret );
5500 }
5501
Hanno Becker106f3da2019-07-12 09:35:58 +01005502 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005503 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005504 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005505 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005507
Paul Bakker5121ce52009-01-03 21:22:43 +00005508 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005509 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005510
Hanno Beckera5a2b082019-05-15 14:03:01 +01005511#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005512 /* We have already checked the record content type
5513 * in ssl_parse_record_header(), failing or silently
5514 * dropping the record in the case of an unknown type.
5515 *
5516 * Since with the use of CIDs, the record content type
5517 * might change during decryption, re-check the record
5518 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005519 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005520 {
5521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5522 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5523 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005524#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005525
Hanno Becker106f3da2019-07-12 09:35:58 +01005526 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005527 {
5528#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005529 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005530 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005531 {
5532 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5534 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5535 }
5536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5537
5538 ssl->nb_zero++;
5539
5540 /*
5541 * Three or more empty messages may be a DoS attack
5542 * (excessive CPU consumption).
5543 */
5544 if( ssl->nb_zero > 3 )
5545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005547 "messages, possible DoS attack" ) );
5548 /* Treat the records as if they were not properly authenticated,
5549 * thereby failing the connection if we see more than allowed
5550 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005551 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5552 }
5553 }
5554 else
5555 ssl->nb_zero = 0;
5556
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005557 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5558#if defined(MBEDTLS_SSL_PROTO_TLS)
5559 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005560 {
5561 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005562 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005563 if( ++ssl->in_ctr[i - 1] != 0 )
5564 break;
5565
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005566 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005567 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005568 {
5569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5570 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5571 }
5572 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005573#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005574
Paul Bakker5121ce52009-01-03 21:22:43 +00005575 }
5576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005577#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005578 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005580 {
5581 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005584 return( ret );
5585 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005590 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005593 }
5594#endif
5595
Hanno Beckerf0242852019-07-09 17:30:02 +01005596 /* Check actual (decrypted) record content length against
5597 * configured maximum. */
5598 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5599 {
5600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5601 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5602 }
5603
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005604 return( 0 );
5605}
5606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005608
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005609/*
5610 * Read a record.
5611 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005612 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5613 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5614 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005615 */
Hanno Becker1097b342018-08-15 14:09:41 +01005616
5617/* Helper functions for mbedtls_ssl_read_record(). */
5618static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005619static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5620static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005621
Hanno Becker327c93b2018-08-15 13:56:18 +01005622int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005623 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005624{
5625 int ret;
5626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005628
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005629 if( ssl->keep_current_message == 0 )
5630 {
5631 do {
Simon Butcher99000142016-10-13 17:21:01 +01005632
Hanno Becker26994592018-08-15 14:14:59 +01005633 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005634 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005635 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005636
Hanno Beckere74d5562018-08-15 14:26:08 +01005637 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005638 {
Hanno Becker40f50842018-08-15 14:48:01 +01005639#if defined(MBEDTLS_SSL_PROTO_DTLS)
5640 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005641
Hanno Becker40f50842018-08-15 14:48:01 +01005642 /* We only check for buffered messages if the
5643 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005644 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005645 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005646 {
Hanno Becker40f50842018-08-15 14:48:01 +01005647 if( ssl_load_buffered_message( ssl ) == 0 )
5648 have_buffered = 1;
5649 }
5650
5651 if( have_buffered == 0 )
5652#endif /* MBEDTLS_SSL_PROTO_DTLS */
5653 {
5654 ret = ssl_get_next_record( ssl );
5655 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5656 continue;
5657
5658 if( ret != 0 )
5659 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005660 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005661 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005662 return( ret );
5663 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005664 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005665 }
5666
5667 ret = mbedtls_ssl_handle_message_type( ssl );
5668
Hanno Becker40f50842018-08-15 14:48:01 +01005669#if defined(MBEDTLS_SSL_PROTO_DTLS)
5670 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5671 {
5672 /* Buffer future message */
5673 ret = ssl_buffer_message( ssl );
5674 if( ret != 0 )
5675 return( ret );
5676
5677 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5678 }
5679#endif /* MBEDTLS_SSL_PROTO_DTLS */
5680
Hanno Becker90333da2017-10-10 11:27:13 +01005681 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5682 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005683
5684 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005685 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005686 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005687 return( ret );
5688 }
5689
Hanno Becker327c93b2018-08-15 13:56:18 +01005690 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005691 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005692 {
5693 mbedtls_ssl_update_handshake_status( ssl );
5694 }
Simon Butcher99000142016-10-13 17:21:01 +01005695 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005696 else
Simon Butcher99000142016-10-13 17:21:01 +01005697 {
Hanno Becker02f59072018-08-15 14:00:24 +01005698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005699 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005700 }
5701
5702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5703
5704 return( 0 );
5705}
5706
Hanno Becker40f50842018-08-15 14:48:01 +01005707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005708static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005709{
Hanno Becker40f50842018-08-15 14:48:01 +01005710 if( ssl->in_left > ssl->next_record_offset )
5711 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005712
Hanno Becker40f50842018-08-15 14:48:01 +01005713 return( 0 );
5714}
5715
5716static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5717{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005718 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005719 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005720 int ret = 0;
5721
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005722 if( hs == NULL )
5723 return( -1 );
5724
Hanno Beckere00ae372018-08-20 09:39:42 +01005725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5726
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005727 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5728 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5729 {
5730 /* Check if we have seen a ChangeCipherSpec before.
5731 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005732 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005733 {
5734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5735 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005736 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005737 }
5738
Hanno Becker39b8bc92018-08-28 17:17:13 +01005739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005740 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5741 ssl->in_msglen = 1;
5742 ssl->in_msg[0] = 1;
5743
5744 /* As long as they are equal, the exact value doesn't matter. */
5745 ssl->in_left = 0;
5746 ssl->next_record_offset = 0;
5747
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005748 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005749 goto exit;
5750 }
Hanno Becker37f95322018-08-16 13:55:32 +01005751
Hanno Beckerb8f50142018-08-28 10:01:34 +01005752#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005753 /* Debug only */
5754 {
5755 unsigned offset;
5756 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5757 {
5758 hs_buf = &hs->buffering.hs[offset];
5759 if( hs_buf->is_valid == 1 )
5760 {
5761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5762 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005763 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005764 }
5765 }
5766 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005767#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005768
5769 /* Check if we have buffered and/or fully reassembled the
5770 * next handshake message. */
5771 hs_buf = &hs->buffering.hs[0];
5772 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5773 {
5774 /* Synthesize a record containing the buffered HS message. */
5775 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5776 ( hs_buf->data[2] << 8 ) |
5777 hs_buf->data[3];
5778
5779 /* Double-check that we haven't accidentally buffered
5780 * a message that doesn't fit into the input buffer. */
5781 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5782 {
5783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5785 }
5786
5787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5788 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5789 hs_buf->data, msg_len + 12 );
5790
5791 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5792 ssl->in_hslen = msg_len + 12;
5793 ssl->in_msglen = msg_len + 12;
5794 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5795
5796 ret = 0;
5797 goto exit;
5798 }
5799 else
5800 {
5801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5802 hs->in_msg_seq ) );
5803 }
5804
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005805 ret = -1;
5806
5807exit:
5808
5809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5810 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005811}
5812
Hanno Beckera02b0b42018-08-21 17:20:27 +01005813static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5814 size_t desired )
5815{
5816 int offset;
5817 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5819 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005820
Hanno Becker01315ea2018-08-21 17:22:17 +01005821 /* Get rid of future records epoch first, if such exist. */
5822 ssl_free_buffered_record( ssl );
5823
5824 /* Check if we have enough space available now. */
5825 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5826 hs->buffering.total_bytes_buffered ) )
5827 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005829 return( 0 );
5830 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005831
Hanno Becker4f432ad2018-08-28 10:02:32 +01005832 /* We don't have enough space to buffer the next expected handshake
5833 * message. Remove buffers used for future messages to gain space,
5834 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005835 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5836 offset >= 0; offset-- )
5837 {
5838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5839 offset ) );
5840
Hanno Beckerb309b922018-08-23 13:18:05 +01005841 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005842
5843 /* Check if we have enough space available now. */
5844 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5845 hs->buffering.total_bytes_buffered ) )
5846 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005848 return( 0 );
5849 }
5850 }
5851
5852 return( -1 );
5853}
5854
Hanno Becker40f50842018-08-15 14:48:01 +01005855static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5856{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005857 int ret = 0;
5858 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5859
5860 if( hs == NULL )
5861 return( 0 );
5862
5863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5864
5865 switch( ssl->in_msgtype )
5866 {
5867 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005869
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005870 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005871 break;
5872
5873 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005874 {
5875 unsigned recv_msg_seq_offset;
5876 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5877 mbedtls_ssl_hs_buffer *hs_buf;
5878 size_t msg_len = ssl->in_hslen - 12;
5879
5880 /* We should never receive an old handshake
5881 * message - double-check nonetheless. */
5882 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5883 {
5884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5886 }
5887
5888 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5889 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5890 {
5891 /* Silently ignore -- message too far in the future */
5892 MBEDTLS_SSL_DEBUG_MSG( 2,
5893 ( "Ignore future HS message with sequence number %u, "
5894 "buffering window %u - %u",
5895 recv_msg_seq, ssl->handshake->in_msg_seq,
5896 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5897
5898 goto exit;
5899 }
5900
5901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5902 recv_msg_seq, recv_msg_seq_offset ) );
5903
5904 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5905
5906 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005907 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005908 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005909 size_t reassembly_buf_sz;
5910
Hanno Becker37f95322018-08-16 13:55:32 +01005911 hs_buf->is_fragmented =
5912 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5913
5914 /* We copy the message back into the input buffer
5915 * after reassembly, so check that it's not too large.
5916 * This is an implementation-specific limitation
5917 * and not one from the standard, hence it is not
5918 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005919 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005920 {
5921 /* Ignore message */
5922 goto exit;
5923 }
5924
Hanno Beckere0b150f2018-08-21 15:51:03 +01005925 /* Check if we have enough space to buffer the message. */
5926 if( hs->buffering.total_bytes_buffered >
5927 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5928 {
5929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5931 }
5932
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005933 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5934 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005935
5936 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5937 hs->buffering.total_bytes_buffered ) )
5938 {
5939 if( recv_msg_seq_offset > 0 )
5940 {
5941 /* If we can't buffer a future message because
5942 * of space limitations -- ignore. */
5943 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",
5944 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5945 (unsigned) hs->buffering.total_bytes_buffered ) );
5946 goto exit;
5947 }
Hanno Beckere1801392018-08-21 16:51:05 +01005948 else
5949 {
5950 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",
5951 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5952 (unsigned) hs->buffering.total_bytes_buffered ) );
5953 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005954
Hanno Beckera02b0b42018-08-21 17:20:27 +01005955 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005956 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005957 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",
5958 (unsigned) msg_len,
5959 (unsigned) reassembly_buf_sz,
5960 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005961 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005962 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5963 goto exit;
5964 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005965 }
5966
5967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5968 msg_len ) );
5969
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005970 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5971 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005972 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005973 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005974 goto exit;
5975 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005976 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005977
5978 /* Prepare final header: copy msg_type, length and message_seq,
5979 * then add standardised fragment_offset and fragment_length */
5980 memcpy( hs_buf->data, ssl->in_msg, 6 );
5981 memset( hs_buf->data + 6, 0, 3 );
5982 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5983
5984 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005985
5986 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005987 }
5988 else
5989 {
5990 /* Make sure msg_type and length are consistent */
5991 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5992 {
5993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5994 /* Ignore */
5995 goto exit;
5996 }
5997 }
5998
Hanno Becker4422bbb2018-08-20 09:40:19 +01005999 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006000 {
6001 size_t frag_len, frag_off;
6002 unsigned char * const msg = hs_buf->data + 12;
6003
6004 /*
6005 * Check and copy current fragment
6006 */
6007
6008 /* Validation of header fields already done in
6009 * mbedtls_ssl_prepare_handshake_record(). */
6010 frag_off = ssl_get_hs_frag_off( ssl );
6011 frag_len = ssl_get_hs_frag_len( ssl );
6012
6013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6014 frag_off, frag_len ) );
6015 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6016
6017 if( hs_buf->is_fragmented )
6018 {
6019 unsigned char * const bitmask = msg + msg_len;
6020 ssl_bitmask_set( bitmask, frag_off, frag_len );
6021 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6022 msg_len ) == 0 );
6023 }
6024 else
6025 {
6026 hs_buf->is_complete = 1;
6027 }
6028
6029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6030 hs_buf->is_complete ? "" : "not yet " ) );
6031 }
6032
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006033 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006034 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006035
6036 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006037 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006038 break;
6039 }
6040
6041exit:
6042
6043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6044 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006045}
6046#endif /* MBEDTLS_SSL_PROTO_DTLS */
6047
Hanno Becker1097b342018-08-15 14:09:41 +01006048static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006049{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006050 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006051 * Consume last content-layer message and potentially
6052 * update in_msglen which keeps track of the contents'
6053 * consumption state.
6054 *
6055 * (1) Handshake messages:
6056 * Remove last handshake message, move content
6057 * and adapt in_msglen.
6058 *
6059 * (2) Alert messages:
6060 * Consume whole record content, in_msglen = 0.
6061 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006062 * (3) Change cipher spec:
6063 * Consume whole record content, in_msglen = 0.
6064 *
6065 * (4) Application data:
6066 * Don't do anything - the record layer provides
6067 * the application data as a stream transport
6068 * and consumes through mbedtls_ssl_read only.
6069 *
6070 */
6071
6072 /* Case (1): Handshake messages */
6073 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006074 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006075 /* Hard assertion to be sure that no application data
6076 * is in flight, as corrupting ssl->in_msglen during
6077 * ssl->in_offt != NULL is fatal. */
6078 if( ssl->in_offt != NULL )
6079 {
6080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6082 }
6083
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006084 /*
6085 * Get next Handshake message in the current record
6086 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006087
Hanno Becker4a810fb2017-05-24 16:27:30 +01006088 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006089 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006090 * current handshake content: If DTLS handshake
6091 * fragmentation is used, that's the fragment
6092 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006093 * size here is faulty and should be changed at
6094 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006095 * (2) While it doesn't seem to cause problems, one
6096 * has to be very careful not to assume that in_hslen
6097 * is always <= in_msglen in a sensible communication.
6098 * Again, it's wrong for DTLS handshake fragmentation.
6099 * The following check is therefore mandatory, and
6100 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006101 * Additionally, ssl->in_hslen might be arbitrarily out of
6102 * bounds after handling a DTLS message with an unexpected
6103 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006104 */
6105 if( ssl->in_hslen < ssl->in_msglen )
6106 {
6107 ssl->in_msglen -= ssl->in_hslen;
6108 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6109 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006110
Hanno Becker4a810fb2017-05-24 16:27:30 +01006111 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6112 ssl->in_msg, ssl->in_msglen );
6113 }
6114 else
6115 {
6116 ssl->in_msglen = 0;
6117 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006118
Hanno Becker4a810fb2017-05-24 16:27:30 +01006119 ssl->in_hslen = 0;
6120 }
6121 /* Case (4): Application data */
6122 else if( ssl->in_offt != NULL )
6123 {
6124 return( 0 );
6125 }
6126 /* Everything else (CCS & Alerts) */
6127 else
6128 {
6129 ssl->in_msglen = 0;
6130 }
6131
Hanno Becker1097b342018-08-15 14:09:41 +01006132 return( 0 );
6133}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006134
Hanno Beckere74d5562018-08-15 14:26:08 +01006135static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6136{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006137 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006138 return( 1 );
6139
6140 return( 0 );
6141}
6142
Hanno Becker5f066e72018-08-16 14:56:31 +01006143#if defined(MBEDTLS_SSL_PROTO_DTLS)
6144
6145static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6146{
6147 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6148 if( hs == NULL )
6149 return;
6150
Hanno Becker01315ea2018-08-21 17:22:17 +01006151 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006152 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006153 hs->buffering.total_bytes_buffered -=
6154 hs->buffering.future_record.len;
6155
6156 mbedtls_free( hs->buffering.future_record.data );
6157 hs->buffering.future_record.data = NULL;
6158 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006159}
6160
6161static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6162{
6163 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6164 unsigned char * rec;
6165 size_t rec_len;
6166 unsigned rec_epoch;
6167
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006168 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006169 return( 0 );
6170
6171 if( hs == NULL )
6172 return( 0 );
6173
Hanno Becker5f066e72018-08-16 14:56:31 +01006174 rec = hs->buffering.future_record.data;
6175 rec_len = hs->buffering.future_record.len;
6176 rec_epoch = hs->buffering.future_record.epoch;
6177
6178 if( rec == NULL )
6179 return( 0 );
6180
Hanno Becker4cb782d2018-08-20 11:19:05 +01006181 /* Only consider loading future records if the
6182 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006183 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006184 return( 0 );
6185
Hanno Becker5f066e72018-08-16 14:56:31 +01006186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6187
6188 if( rec_epoch != ssl->in_epoch )
6189 {
6190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6191 goto exit;
6192 }
6193
6194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6195
6196 /* Double-check that the record is not too large */
6197 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6198 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6199 {
6200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6202 }
6203
6204 memcpy( ssl->in_hdr, rec, rec_len );
6205 ssl->in_left = rec_len;
6206 ssl->next_record_offset = 0;
6207
6208 ssl_free_buffered_record( ssl );
6209
6210exit:
6211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6212 return( 0 );
6213}
6214
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006215static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6216 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006217{
6218 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006219
6220 /* Don't buffer future records outside handshakes. */
6221 if( hs == NULL )
6222 return( 0 );
6223
6224 /* Only buffer handshake records (we are only interested
6225 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006226 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006227 return( 0 );
6228
6229 /* Don't buffer more than one future epoch record. */
6230 if( hs->buffering.future_record.data != NULL )
6231 return( 0 );
6232
Hanno Becker01315ea2018-08-21 17:22:17 +01006233 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006234 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006235 hs->buffering.total_bytes_buffered ) )
6236 {
6237 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 +01006238 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006239 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006240 return( 0 );
6241 }
6242
Hanno Becker5f066e72018-08-16 14:56:31 +01006243 /* Buffer record */
6244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6245 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006246 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006247
6248 /* ssl_parse_record_header() only considers records
6249 * of the next epoch as candidates for buffering. */
6250 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006251 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006252
6253 hs->buffering.future_record.data =
6254 mbedtls_calloc( 1, hs->buffering.future_record.len );
6255 if( hs->buffering.future_record.data == NULL )
6256 {
6257 /* If we run out of RAM trying to buffer a
6258 * record from the next epoch, just ignore. */
6259 return( 0 );
6260 }
6261
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006262 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006263
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006264 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006265 return( 0 );
6266}
6267
6268#endif /* MBEDTLS_SSL_PROTO_DTLS */
6269
Hanno Beckere74d5562018-08-15 14:26:08 +01006270static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006271{
6272 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006273 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006274
Hanno Becker5f066e72018-08-16 14:56:31 +01006275#if defined(MBEDTLS_SSL_PROTO_DTLS)
6276 /* We might have buffered a future record; if so,
6277 * and if the epoch matches now, load it.
6278 * On success, this call will set ssl->in_left to
6279 * the length of the buffered record, so that
6280 * the calls to ssl_fetch_input() below will
6281 * essentially be no-ops. */
6282 ret = ssl_load_buffered_record( ssl );
6283 if( ret != 0 )
6284 return( ret );
6285#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006286
Hanno Becker8b09b732019-05-08 12:03:28 +01006287 /* Ensure that we have enough space available for the default form
6288 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6289 * with no space for CIDs counted in). */
6290 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6291 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006294 return( ret );
6295 }
6296
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006297 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6298 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006301 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006302 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006303 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6304 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006305 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006306 if( ret != 0 )
6307 return( ret );
6308
6309 /* Fall through to handling of unexpected records */
6310 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6311 }
6312
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006313 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6314 {
Hanno Becker87b56262019-07-10 14:37:41 +01006315#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006316 /* Reset in pointers to default state for TLS/DTLS records,
6317 * assuming no CID and no offset between record content and
6318 * record plaintext. */
6319 ssl_update_in_pointers( ssl );
6320
Hanno Becker69412452019-07-12 08:33:49 +01006321 /* Setup internal message pointers from record structure. */
6322 ssl->in_msgtype = rec.type;
6323#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6324 ssl->in_len = ssl->in_cid + rec.cid_len;
6325#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006326 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006327 ssl->in_msglen = rec.data_len;
6328
Hanno Becker87b56262019-07-10 14:37:41 +01006329 ret = ssl_check_client_reconnect( ssl );
6330 if( ret != 0 )
6331 return( ret );
6332#endif
6333
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006334 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006335 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006336
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6338 "(header)" ) );
6339 }
6340 else
6341 {
6342 /* Skip invalid record and the rest of the datagram */
6343 ssl->next_record_offset = 0;
6344 ssl->in_left = 0;
6345
6346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6347 "(header)" ) );
6348 }
6349
6350 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006351 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006352 }
Hanno Becker87b56262019-07-10 14:37:41 +01006353 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006354#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006355 {
6356 return( ret );
6357 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006358 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006361 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006362 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006363 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006364 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006365 if( ssl->next_record_offset < ssl->in_left )
6366 {
6367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6368 }
6369 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006370 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006371#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006372#if defined(MBEDTLS_SSL_PROTO_TLS)
6373 {
Hanno Beckere0452772019-07-10 17:12:07 +01006374 /*
6375 * Fetch record contents from underlying transport.
6376 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006377 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006378 if( ret != 0 )
6379 {
6380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6381 return( ret );
6382 }
6383
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006384 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006385 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006386#endif /* MBEDTLS_SSL_PROTO_TLS */
6387
6388 /*
6389 * Decrypt record contents.
6390 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006391
Hanno Beckera89610a2019-07-11 13:07:45 +01006392 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006395 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006396 {
6397 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006398 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006399 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006400 /* Except when waiting for Finished as a bad mac here
6401 * probably means something went wrong in the handshake
6402 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6403 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6404 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6405 {
6406#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6407 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6408 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006409 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006410 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6411 }
6412#endif
6413 return( ret );
6414 }
6415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006417 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6418 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6421 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006422 }
6423#endif
6424
Hanno Becker4a810fb2017-05-24 16:27:30 +01006425 /* As above, invalid records cause
6426 * dismissal of the whole datagram. */
6427
6428 ssl->next_record_offset = 0;
6429 ssl->in_left = 0;
6430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006432 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006433 }
6434
6435 return( ret );
6436 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006437 MBEDTLS_SSL_TRANSPORT_ELSE
6438#endif /* MBEDTLS_SSL_PROTO_DTLS */
6439#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006440 {
6441 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006442#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6443 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006444 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006445 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006447 }
6448#endif
6449 return( ret );
6450 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006451#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006452 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006453
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006454
6455 /* Reset in pointers to default state for TLS/DTLS records,
6456 * assuming no CID and no offset between record content and
6457 * record plaintext. */
6458 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006459#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6460 ssl->in_len = ssl->in_cid + rec.cid_len;
6461#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006462 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006463
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006464 /* The record content type may change during decryption,
6465 * so re-read it. */
6466 ssl->in_msgtype = rec.type;
6467 /* Also update the input buffer, because unfortunately
6468 * the server-side ssl_parse_client_hello() reparses the
6469 * record header when receiving a ClientHello initiating
6470 * a renegotiation. */
6471 ssl->in_hdr[0] = rec.type;
6472 ssl->in_msg = rec.buf + rec.data_offset;
6473 ssl->in_msglen = rec.data_len;
6474 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6475 ssl->in_len[1] = (unsigned char)( rec.data_len );
6476
Simon Butcher99000142016-10-13 17:21:01 +01006477 return( 0 );
6478}
6479
6480int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6481{
6482 int ret;
6483
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006484 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006485 * Handle particular types of records
6486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006488 {
Simon Butcher99000142016-10-13 17:21:01 +01006489 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6490 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006491 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 }
6494
Hanno Beckere678eaa2018-08-21 14:57:46 +01006495 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006496 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006497 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006498 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6500 ssl->in_msglen ) );
6501 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006502 }
6503
Hanno Beckere678eaa2018-08-21 14:57:46 +01006504 if( ssl->in_msg[0] != 1 )
6505 {
6506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6507 ssl->in_msg[0] ) );
6508 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6509 }
6510
6511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006512 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006513 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6514 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6515 {
6516 if( ssl->handshake == NULL )
6517 {
6518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6519 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6520 }
6521
6522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6523 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6524 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006525#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006526 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006528 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006530 if( ssl->in_msglen != 2 )
6531 {
6532 /* Note: Standard allows for more than one 2 byte alert
6533 to be packed in a single message, but Mbed TLS doesn't
6534 currently support this. */
6535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6536 ssl->in_msglen ) );
6537 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6538 }
6539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006541 ssl->in_msg[0], ssl->in_msg[1] ) );
6542
6543 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006544 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006549 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 }
6552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6554 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6557 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006558 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006559
6560#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6561 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6562 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6563 {
Hanno Becker90333da2017-10-10 11:27:13 +01006564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006565 /* Will be handled when trying to parse ServerHello */
6566 return( 0 );
6567 }
6568#endif
6569
6570#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006571 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006572 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6573 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006574 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6575 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6576 {
6577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6578 /* Will be handled in mbedtls_ssl_parse_certificate() */
6579 return( 0 );
6580 }
6581#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6582
6583 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006584 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006585 }
6586
Hanno Beckerc76c6192017-06-06 10:03:17 +01006587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006588 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006589 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006590 /* Drop unexpected ApplicationData records,
6591 * except at the beginning of renegotiations */
6592 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6593 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6594#if defined(MBEDTLS_SSL_RENEGOTIATION)
6595 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6596 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006597#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006598 )
6599 {
6600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6601 return( MBEDTLS_ERR_SSL_NON_FATAL );
6602 }
6603
6604 if( ssl->handshake != NULL &&
6605 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6606 {
6607 ssl_handshake_wrapup_free_hs_transform( ssl );
6608 }
6609 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006610#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006611
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 return( 0 );
6613}
6614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006616{
6617 int ret;
6618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6620 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6621 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006622 {
6623 return( ret );
6624 }
6625
6626 return( 0 );
6627}
6628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006630 unsigned char level,
6631 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006632{
6633 int ret;
6634
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006635 if( ssl == NULL || ssl->conf == NULL )
6636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006642 ssl->out_msglen = 2;
6643 ssl->out_msg[0] = level;
6644 ssl->out_msg[1] = message;
6645
Hanno Becker67bc7c32018-08-06 11:33:50 +01006646 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006649 return( ret );
6650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006652
6653 return( 0 );
6654}
6655
Hanno Becker17572472019-02-08 07:19:04 +00006656#if defined(MBEDTLS_X509_CRT_PARSE_C)
6657static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6658{
6659#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6660 if( session->peer_cert != NULL )
6661 {
6662 mbedtls_x509_crt_free( session->peer_cert );
6663 mbedtls_free( session->peer_cert );
6664 session->peer_cert = NULL;
6665 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006666#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006667 if( session->peer_cert_digest != NULL )
6668 {
6669 /* Zeroization is not necessary. */
6670 mbedtls_free( session->peer_cert_digest );
6671 session->peer_cert_digest = NULL;
6672 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6673 session->peer_cert_digest_len = 0;
6674 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006675#else
6676 ((void) session);
6677#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006678}
6679#endif /* MBEDTLS_X509_CRT_PARSE_C */
6680
Paul Bakker5121ce52009-01-03 21:22:43 +00006681/*
6682 * Handshake functions
6683 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006684#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006685/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006687{
Hanno Beckerdf645962019-06-26 13:02:22 +01006688 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6689 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692
Hanno Becker5097cba2019-02-05 13:36:46 +00006693 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006696 ssl->state++;
6697 return( 0 );
6698 }
6699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006702}
6703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006705{
Hanno Beckerdf645962019-06-26 13:02:22 +01006706 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6707 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006710
Hanno Becker5097cba2019-02-05 13:36:46 +00006711 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006714 ssl->state++;
6715 return( 0 );
6716 }
6717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006720}
Gilles Peskinef9828522017-05-03 12:28:43 +02006721
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006722#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006723/* Some certificate support -> implement write and parse */
6724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006725int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006728 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006730 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6731 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006734
Hanno Becker5097cba2019-02-05 13:36:46 +00006735 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738 ssl->state++;
6739 return( 0 );
6740 }
6741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006743 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6744 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006745 {
6746 if( ssl->client_auth == 0 )
6747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006749 ssl->state++;
6750 return( 0 );
6751 }
6752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006754 /*
6755 * If using SSLv3 and got no cert, send an Alert message
6756 * (otherwise an empty Certificate message will be sent).
6757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006759 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006760 {
6761 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6763 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6764 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006767 goto write_msg;
6768 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771#endif /* MBEDTLS_SSL_CLI_C */
6772#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006773 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6778 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 }
6780 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006781#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006784
6785 /*
6786 * 0 . 0 handshake type
6787 * 1 . 3 handshake length
6788 * 4 . 6 length of all certs
6789 * 7 . 9 length of cert. 1
6790 * 10 . n-1 peer certificate
6791 * n . n+2 length of cert. 2
6792 * n+3 . ... upper level cert, etc.
6793 */
6794 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006796
Paul Bakker29087132010-03-21 21:03:34 +00006797 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 {
6799 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006800 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006803 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 }
6806
6807 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6808 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6809 ssl->out_msg[i + 2] = (unsigned char)( n );
6810
6811 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6812 i += n; crt = crt->next;
6813 }
6814
6815 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6816 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6817 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6818
6819 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6821 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006823#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006824write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006825#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006826
6827 ssl->state++;
6828
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006829 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006830 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006831 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832 return( ret );
6833 }
6834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006836
Paul Bakkered27a042013-04-18 22:46:23 +02006837 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006838}
6839
Hanno Becker285ff0c2019-01-31 07:44:03 +00006840#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006841
6842#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006843static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6844 unsigned char *crt_buf,
6845 size_t crt_buf_len )
6846{
6847 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6848
6849 if( peer_crt == NULL )
6850 return( -1 );
6851
6852 if( peer_crt->raw.len != crt_buf_len )
6853 return( -1 );
6854
Hanno Becker68b856d2019-02-08 14:00:04 +00006855 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006856}
Hanno Becker5882dd02019-06-06 16:25:57 +01006857#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006858static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6859 unsigned char *crt_buf,
6860 size_t crt_buf_len )
6861{
6862 int ret;
6863 unsigned char const * const peer_cert_digest =
6864 ssl->session->peer_cert_digest;
6865 mbedtls_md_type_t const peer_cert_digest_type =
6866 ssl->session->peer_cert_digest_type;
6867 mbedtls_md_info_t const * const digest_info =
6868 mbedtls_md_info_from_type( peer_cert_digest_type );
6869 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6870 size_t digest_len;
6871
6872 if( peer_cert_digest == NULL || digest_info == NULL )
6873 return( -1 );
6874
6875 digest_len = mbedtls_md_get_size( digest_info );
6876 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6877 return( -1 );
6878
6879 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6880 if( ret != 0 )
6881 return( -1 );
6882
6883 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6884}
Hanno Becker5882dd02019-06-06 16:25:57 +01006885#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006886#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006887
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006888/*
6889 * Once the certificate message is read, parse it into a cert chain and
6890 * perform basic checks, but leave actual verification to the caller
6891 */
Hanno Becker35e41772019-02-05 15:37:23 +00006892static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6893 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006894{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006895 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006896#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6897 int crt_cnt=0;
6898#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006899 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006900 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006905 mbedtls_ssl_pend_fatal_alert( ssl,
6906 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006908 }
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6911 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006914 mbedtls_ssl_pend_fatal_alert( ssl,
6915 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006917 }
6918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006920
Paul Bakker5121ce52009-01-03 21:22:43 +00006921 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006923 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006924 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006925
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006926 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006930 mbedtls_ssl_pend_fatal_alert( ssl,
6931 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006933 }
6934
Hanno Becker33c3dc82019-01-30 14:46:46 +00006935 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6936 i += 3;
6937
Hanno Becker33c3dc82019-01-30 14:46:46 +00006938 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006939 while( i < ssl->in_hslen )
6940 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006941 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006942 if ( i + 3 > ssl->in_hslen ) {
6943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006944 mbedtls_ssl_pend_fatal_alert( ssl,
6945 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006946 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6947 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006948 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6949 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006950 if( ssl->in_msg[i] != 0 )
6951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006953 mbedtls_ssl_pend_fatal_alert( ssl,
6954 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 }
6957
Hanno Becker33c3dc82019-01-30 14:46:46 +00006958 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006959 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6960 | (unsigned int) ssl->in_msg[i + 2];
6961 i += 3;
6962
6963 if( n < 128 || i + n > ssl->in_hslen )
6964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006966 mbedtls_ssl_pend_fatal_alert( ssl,
6967 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006969 }
6970
Hanno Becker33c3dc82019-01-30 14:46:46 +00006971 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006972#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6973 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006974 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6975 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006976 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006977 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006978 /* During client-side renegotiation, check that the server's
6979 * end-CRTs hasn't changed compared to the initial handshake,
6980 * mitigating the triple handshake attack. On success, reuse
6981 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006982 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6983 if( ssl_check_peer_crt_unchanged( ssl,
6984 &ssl->in_msg[i],
6985 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006986 {
Hanno Becker35e41772019-02-05 15:37:23 +00006987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006988 mbedtls_ssl_pend_fatal_alert( ssl,
6989 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00006990 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006991 }
Hanno Becker35e41772019-02-05 15:37:23 +00006992
6993 /* Now we can safely free the original chain. */
6994 ssl_clear_peer_cert( ssl->session );
6995 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006996#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6997
Hanno Becker33c3dc82019-01-30 14:46:46 +00006998 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006999#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007000 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007001#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007002 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007003 * it in-place from the input buffer instead of making a copy. */
7004 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7005#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007006 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007008 case 0: /*ok*/
7009 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7010 /* Ignore certificate with an unknown algorithm: maybe a
7011 prior certificate was already trusted. */
7012 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007013
Hanno Becker33c3dc82019-01-30 14:46:46 +00007014 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7015 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7016 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007017
Hanno Becker33c3dc82019-01-30 14:46:46 +00007018 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7019 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7020 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007021
Hanno Becker33c3dc82019-01-30 14:46:46 +00007022 default:
7023 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7024 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007025 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007026 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7027 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007028 }
7029
7030 i += n;
7031 }
7032
Hanno Becker35e41772019-02-05 15:37:23 +00007033 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007034 return( 0 );
7035}
7036
Hanno Beckerb8a08572019-02-05 12:49:06 +00007037#if defined(MBEDTLS_SSL_SRV_C)
7038static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7039{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007040 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007041 return( -1 );
7042
7043#if defined(MBEDTLS_SSL_PROTO_SSL3)
7044 /*
7045 * Check if the client sent an empty certificate
7046 */
Hanno Becker2881d802019-05-22 14:44:53 +01007047 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007048 {
7049 if( ssl->in_msglen == 2 &&
7050 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7051 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7052 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7053 {
7054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7055 return( 0 );
7056 }
7057
7058 return( -1 );
7059 }
7060#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7061
7062#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7063 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7064 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7065 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7066 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7067 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7068 {
7069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7070 return( 0 );
7071 }
7072
Hanno Beckerb8a08572019-02-05 12:49:06 +00007073#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7074 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007075
7076 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007077}
7078#endif /* MBEDTLS_SSL_SRV_C */
7079
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007080/* Check if a certificate message is expected.
7081 * Return either
7082 * - SSL_CERTIFICATE_EXPECTED, or
7083 * - SSL_CERTIFICATE_SKIP
7084 * indicating whether a Certificate message is expected or not.
7085 */
7086#define SSL_CERTIFICATE_EXPECTED 0
7087#define SSL_CERTIFICATE_SKIP 1
7088static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7089 int authmode )
7090{
Hanno Becker473f98f2019-06-26 10:27:32 +01007091 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007092 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007093
7094 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7095 return( SSL_CERTIFICATE_SKIP );
7096
7097#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007098 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007099 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007100 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7101 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7102 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007103 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007104 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007105
7106 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7107 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007108 ssl->session_negotiate->verify_result =
7109 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7110 return( SSL_CERTIFICATE_SKIP );
7111 }
7112 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007113#else
7114 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007115#endif /* MBEDTLS_SSL_SRV_C */
7116
7117 return( SSL_CERTIFICATE_EXPECTED );
7118}
7119
Hanno Becker3cf50612019-02-05 14:36:34 +00007120static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7121 int authmode,
7122 mbedtls_x509_crt *chain,
7123 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007124{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007125 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007126 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007127 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007128 mbedtls_x509_crt *ca_chain;
7129 mbedtls_x509_crl *ca_crl;
7130
7131 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7132 return( 0 );
7133
7134#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7135 if( ssl->handshake->sni_ca_chain != NULL )
7136 {
7137 ca_chain = ssl->handshake->sni_ca_chain;
7138 ca_crl = ssl->handshake->sni_ca_crl;
7139 }
7140 else
7141#endif
7142 {
7143 ca_chain = ssl->conf->ca_chain;
7144 ca_crl = ssl->conf->ca_crl;
7145 }
7146
7147 /*
7148 * Main check: verify certificate
7149 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007150 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007151 chain,
7152 ca_chain, ca_crl,
7153 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007154#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007155 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007156#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007157 &ssl->session_negotiate->verify_result,
7158 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7159
Hanno Becker8c13ee62019-02-26 16:48:17 +00007160 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007161 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007162 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007163 }
7164
7165#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007166 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007167 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7168#endif
7169
7170 /*
7171 * Secondary checks: always done, but change 'ret' only if it was 0
7172 */
7173
7174#if defined(MBEDTLS_ECP_C)
7175 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007176 int ret;
Hanno Becker8c13ee62019-02-26 16:48:17 +00007177 mbedtls_pk_context *pk;
7178 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7179 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007180 {
7181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007182 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007183 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007184
7185 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007186 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
7187 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
7188
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007189 mbedtls_x509_crt_pk_release( chain );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007190
7191 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007192 {
7193 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7194
7195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007196 if( verify_ret == 0 )
7197 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007198 }
7199 }
7200#endif /* MBEDTLS_ECP_C */
7201
7202 if( mbedtls_ssl_check_cert_usage( chain,
7203 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007204 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7205 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007206 &ssl->session_negotiate->verify_result ) != 0 )
7207 {
7208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007209 if( verify_ret == 0 )
7210 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007211 }
7212
7213 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7214 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7215 * with details encoded in the verification flags. All other kinds
7216 * of error codes, including those from the user provided f_vrfy
7217 * functions, are treated as fatal and lead to a failure of
7218 * ssl_parse_certificate even if verification was optional. */
7219 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007220 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7221 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007222 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007223 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007224 }
7225
7226 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7227 {
7228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007229 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007230 }
7231
Hanno Becker8c13ee62019-02-26 16:48:17 +00007232 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007233 {
7234 uint8_t alert;
7235
7236 /* The certificate may have been rejected for several reasons.
7237 Pick one and send the corresponding alert. Which alert to send
7238 may be a subject of debate in some cases. */
7239 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7240 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7241 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7242 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7243 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7244 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7245 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7246 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7247 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7248 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7249 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7250 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7251 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7252 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7253 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7254 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7255 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7256 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7258 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7259 else
7260 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007261 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007262 }
7263
7264#if defined(MBEDTLS_DEBUG_C)
7265 if( ssl->session_negotiate->verify_result != 0 )
7266 {
7267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7268 ssl->session_negotiate->verify_result ) );
7269 }
7270 else
7271 {
7272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7273 }
7274#endif /* MBEDTLS_DEBUG_C */
7275
Hanno Becker8c13ee62019-02-26 16:48:17 +00007276 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007277}
7278
Hanno Becker34106f62019-02-08 14:59:05 +00007279#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007280
7281#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007282static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7283 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007284{
7285 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007286 /* Remember digest of the peer's end-CRT. */
7287 ssl->session_negotiate->peer_cert_digest =
7288 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7289 if( ssl->session_negotiate->peer_cert_digest == NULL )
7290 {
7291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7292 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007293 mbedtls_ssl_pend_fatal_alert( ssl,
7294 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007295
7296 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7297 }
7298
7299 ret = mbedtls_md( mbedtls_md_info_from_type(
7300 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7301 start, len,
7302 ssl->session_negotiate->peer_cert_digest );
7303
7304 ssl->session_negotiate->peer_cert_digest_type =
7305 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7306 ssl->session_negotiate->peer_cert_digest_len =
7307 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7308
7309 return( ret );
7310}
Hanno Becker5882dd02019-06-06 16:25:57 +01007311#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007312
7313static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7314 unsigned char *start, size_t len )
7315{
7316 unsigned char *end = start + len;
7317 int ret;
7318
7319 /* Make a copy of the peer's raw public key. */
7320 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7321 ret = mbedtls_pk_parse_subpubkey( &start, end,
7322 &ssl->handshake->peer_pubkey );
7323 if( ret != 0 )
7324 {
7325 /* We should have parsed the public key before. */
7326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7327 }
7328
7329 return( 0 );
7330}
7331#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7332
Hanno Becker3cf50612019-02-05 14:36:34 +00007333int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7334{
7335 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007336 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007337#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7338 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7339 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007340 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007341#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007342 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007343#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007344 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007345 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007346
7347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7348
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007349 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7350 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007351 {
7352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007353 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007354 }
7355
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007356#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7357 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007358 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007359 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007360 chain = ssl->handshake->ecrs_peer_cert;
7361 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007362 goto crt_verify;
7363 }
7364#endif
7365
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007366 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007367 {
7368 /* mbedtls_ssl_read_record may have sent an alert already. We
7369 let it decide whether to alert. */
7370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007371 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007372 }
7373
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007374#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007375 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7376 {
7377 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007378
Hanno Beckerb8a08572019-02-05 12:49:06 +00007379 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007380 ret = 0;
7381 else
7382 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007383
Hanno Becker613d4902019-02-05 13:11:17 +00007384 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007385 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007386#endif /* MBEDTLS_SSL_SRV_C */
7387
Hanno Becker35e41772019-02-05 15:37:23 +00007388 /* Clear existing peer CRT structure in case we tried to
7389 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007390 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007391
7392 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7393 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007394 {
7395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7396 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007397 mbedtls_ssl_pend_fatal_alert( ssl,
7398 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007399
Hanno Beckere4aeb762019-02-05 17:19:52 +00007400 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7401 goto exit;
7402 }
7403 mbedtls_x509_crt_init( chain );
7404
7405 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007406 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007407 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007408
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007409#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7410 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007411 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007412
7413crt_verify:
7414 if( ssl->handshake->ecrs_enabled)
7415 rs_ctx = &ssl->handshake->ecrs_ctx;
7416#endif
7417
Hanno Becker3cf50612019-02-05 14:36:34 +00007418 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007419 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007420 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007421 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007422
Hanno Becker3008d282019-02-05 17:02:28 +00007423#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007424 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007425 size_t pk_len;
7426 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007427
Hanno Becker34106f62019-02-08 14:59:05 +00007428 /* We parse the CRT chain without copying, so
7429 * these pointers point into the input buffer,
7430 * and are hence still valid after freeing the
7431 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007432
Hanno Becker5882dd02019-06-06 16:25:57 +01007433#if defined(MBEDTLS_SSL_RENEGOTIATION)
7434 unsigned char *crt_start;
7435 size_t crt_len;
7436
Hanno Becker34106f62019-02-08 14:59:05 +00007437 crt_start = chain->raw.p;
7438 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007439#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007440
Hanno Becker8c13ee62019-02-26 16:48:17 +00007441 pk_start = chain->cache->pk_raw.p;
7442 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007443
7444 /* Free the CRT structures before computing
7445 * digest and copying the peer's public key. */
7446 mbedtls_x509_crt_free( chain );
7447 mbedtls_free( chain );
7448 chain = NULL;
7449
Hanno Becker5882dd02019-06-06 16:25:57 +01007450#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007451 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007452 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007453 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007454#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007455
Hanno Becker34106f62019-02-08 14:59:05 +00007456 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007457 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007458 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007459 }
Hanno Becker34106f62019-02-08 14:59:05 +00007460#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7461 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007462 ssl->session_negotiate->peer_cert = chain;
7463 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007464#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007467
Hanno Becker613d4902019-02-05 13:11:17 +00007468exit:
7469
Hanno Beckere4aeb762019-02-05 17:19:52 +00007470 if( ret == 0 )
7471 ssl->state++;
7472
7473#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7474 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7475 {
7476 ssl->handshake->ecrs_peer_cert = chain;
7477 chain = NULL;
7478 }
7479#endif
7480
7481 if( chain != NULL )
7482 {
7483 mbedtls_x509_crt_free( chain );
7484 mbedtls_free( chain );
7485 }
7486
Paul Bakker5121ce52009-01-03 21:22:43 +00007487 return( ret );
7488}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007489#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007492{
7493 int ret;
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007498 ssl->out_msglen = 1;
7499 ssl->out_msg[0] = 1;
7500
Paul Bakker5121ce52009-01-03 21:22:43 +00007501 ssl->state++;
7502
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007503 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007504 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007506 return( ret );
7507 }
7508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007510
7511 return( 0 );
7512}
7513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007515{
7516 int ret;
7517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007519
Hanno Becker327c93b2018-08-15 13:56:18 +01007520 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007523 return( ret );
7524 }
7525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007529 mbedtls_ssl_pend_fatal_alert( ssl,
7530 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007532 }
7533
Hanno Beckere678eaa2018-08-21 14:57:46 +01007534 /* CCS records are only accepted if they have length 1 and content '1',
7535 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007536
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007537 /*
7538 * Switch to our negotiated transform and session parameters for inbound
7539 * data.
7540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007542 ssl->transform_in = ssl->transform_negotiate;
7543 ssl->session_in = ssl->session_negotiate;
7544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007546 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007549 ssl_dtls_replay_reset( ssl );
7550#endif
7551
7552 /* Increment epoch */
7553 if( ++ssl->in_epoch == 0 )
7554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007556 /* This is highly unlikely to happen for legitimate reasons, so
7557 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007559 }
7560 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007561 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007563#if defined(MBEDTLS_SSL_PROTO_TLS)
7564 {
7565 memset( ssl->in_ctr, 0, 8 );
7566 }
7567#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007568
Hanno Beckerf5970a02019-05-08 09:38:41 +01007569 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7572 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007577 mbedtls_ssl_pend_fatal_alert( ssl,
7578 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007580 }
7581 }
7582#endif
7583
Paul Bakker5121ce52009-01-03 21:22:43 +00007584 ssl->state++;
7585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007587
7588 return( 0 );
7589}
7590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007592{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7594 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007595 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007596 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007597#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7599#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007600 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007601#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007603 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007606}
7607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007609{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007611
7612 /*
7613 * Free our handshake params
7614 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007615 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007617 ssl->handshake = NULL;
7618
7619 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007620 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007621 */
7622 if( ssl->transform )
7623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 mbedtls_ssl_transform_free( ssl->transform );
7625 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007626 }
7627 ssl->transform = ssl->transform_negotiate;
7628 ssl->transform_negotiate = NULL;
7629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007631}
7632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007634{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637#if defined(MBEDTLS_SSL_RENEGOTIATION)
7638 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007641 ssl->renego_records_seen = 0;
7642 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007643#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007644
7645 /*
7646 * Free the previous session and switch in the current one
7647 */
Paul Bakker0a597072012-09-25 21:55:46 +00007648 if( ssl->session )
7649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007651 /* RFC 7366 3.1: keep the EtM state */
7652 ssl->session_negotiate->encrypt_then_mac =
7653 ssl->session->encrypt_then_mac;
7654#endif
7655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 mbedtls_ssl_session_free( ssl->session );
7657 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007658 }
7659 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007660 ssl->session_negotiate = NULL;
7661
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007662#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007663 /*
7664 * Add cache entry
7665 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007666 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007667 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007668 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007669 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007670 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007672 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007673#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007676 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007677 ssl->handshake->flight != NULL )
7678 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007679 /* Cancel handshake timer */
7680 ssl_set_timer( ssl, 0 );
7681
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007682 /* Keep last flight around in case we need to resend it:
7683 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007685 }
7686 else
7687#endif
7688 ssl_handshake_wrapup_free_hs_transform( ssl );
7689
Paul Bakker48916f92012-09-16 19:57:18 +00007690 ssl->state++;
7691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007693}
7694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007696{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007697 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007700
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007701 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007702
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007703 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7704 mbedtls_ssl_suite_get_mac(
7705 mbedtls_ssl_ciphersuite_from_id(
7706 mbedtls_ssl_session_get_ciphersuite(
7707 ssl->session_negotiate ) ) ),
7708 ssl, ssl->out_msg + 4,
7709 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007710
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007711 /*
7712 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7713 * may define some other value. Currently (early 2016), no defined
7714 * ciphersuite does this (and this is unlikely to change as activity has
7715 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7716 */
Hanno Becker2881d802019-05-22 14:44:53 +01007717 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007720 ssl->verify_data_len = hash_len;
7721 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007722#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007723
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7726 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007727
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007728#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007729 /*
7730 * In case of session resuming, invert the client and server
7731 * ChangeCipherSpec messages order.
7732 */
Paul Bakker0a597072012-09-25 21:55:46 +00007733 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007736 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7737 MBEDTLS_SSL_IS_CLIENT )
7738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007740 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007743 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7744 MBEDTLS_SSL_IS_SERVER )
7745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007746 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007747 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007748#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007749 }
7750 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007751#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007752 ssl->state++;
7753
Paul Bakker48916f92012-09-16 19:57:18 +00007754 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007755 * Switch to our negotiated transform and session parameters for outbound
7756 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007761 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007762 {
7763 unsigned char i;
7764
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007765 /* Remember current epoch settings for resending */
7766 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007767 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007768
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007769 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007770 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007771
7772 /* Increment epoch */
7773 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007774 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007775 break;
7776
7777 /* The loop goes to its end iff the counter is wrapping */
7778 if( i == 0 )
7779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7781 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007782 }
7783 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007784 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007786#if defined(MBEDTLS_SSL_PROTO_TLS)
7787 {
7788 memset( ssl->cur_out_ctr, 0, 8 );
7789 }
7790#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007791
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007792 ssl->transform_out = ssl->transform_negotiate;
7793 ssl->session_out = ssl->session_negotiate;
7794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7796 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7801 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007802 }
7803 }
7804#endif
7805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007807 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007809#endif
7810
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007811 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007812 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007813 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007814 return( ret );
7815 }
7816
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007817#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007818 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007819 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7820 {
7821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7822 return( ret );
7823 }
7824#endif
7825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007827
7828 return( 0 );
7829}
7830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007832#define SSL_MAX_HASH_LEN 36
7833#else
7834#define SSL_MAX_HASH_LEN 12
7835#endif
7836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007838{
Paul Bakker23986e52011-04-24 08:57:21 +00007839 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007840 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007841 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007844
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007845 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7846 mbedtls_ssl_suite_get_mac(
7847 mbedtls_ssl_ciphersuite_from_id(
7848 mbedtls_ssl_session_get_ciphersuite(
7849 ssl->session_negotiate ) ) ),
7850 ssl, buf,
7851 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007852
Hanno Becker327c93b2018-08-15 13:56:18 +01007853 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007856 return( ret );
7857 }
7858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007862 mbedtls_ssl_pend_fatal_alert( ssl,
7863 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007865 }
7866
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007867 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007869 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007870 hash_len = 36;
7871 else
7872#endif
7873 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7876 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007879 mbedtls_ssl_pend_fatal_alert( ssl,
7880 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007882 }
7883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007885 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007888 mbedtls_ssl_pend_fatal_alert( ssl,
7889 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007891 }
7892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007894 ssl->verify_data_len = hash_len;
7895 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007896#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007897
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007898#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007899 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007902 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007906 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007908#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007909 }
7910 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007911#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007912 ssl->state++;
7913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007915 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007917#endif
7918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007920
7921 return( 0 );
7922}
7923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007925{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7929 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7930 mbedtls_md5_init( &handshake->fin_md5 );
7931 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007932 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7933 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7936#if defined(MBEDTLS_SHA256_C)
7937 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007938 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007939#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#if defined(MBEDTLS_SHA512_C)
7941 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007942 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007943#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007945
Hanno Becker7e5437a2017-04-28 17:15:26 +01007946#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7947 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7948 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7949#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951#if defined(MBEDTLS_DHM_C)
7952 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#if defined(MBEDTLS_ECDH_C)
7955 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007956#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007957#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007958 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007959#if defined(MBEDTLS_SSL_CLI_C)
7960 handshake->ecjpake_cache = NULL;
7961 handshake->ecjpake_cache_len = 0;
7962#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007963#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007964
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007965#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007966 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007967#endif
7968
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007969#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7970 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7971#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007972
7973#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7974 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7975 mbedtls_pk_init( &handshake->peer_pubkey );
7976#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007977}
7978
Hanno Becker611a83b2018-01-03 14:27:32 +00007979void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7984 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007985
Hanno Becker92231322018-01-03 15:32:51 +00007986#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 mbedtls_md_init( &transform->md_ctx_enc );
7988 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007989#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007990}
7991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007993{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007995}
7996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007998{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007999 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008000 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008001 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008002 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008004 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008005 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008006
8007 /*
8008 * Either the pointers are now NULL or cleared properly and can be freed.
8009 * Now allocate missing structures.
8010 */
8011 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008012 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008013 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008014 }
Paul Bakker48916f92012-09-16 19:57:18 +00008015
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008016 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008017 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008018 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008019 }
Paul Bakker48916f92012-09-16 19:57:18 +00008020
Paul Bakker82788fb2014-10-20 13:59:19 +02008021 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008022 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008023 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008024 }
Paul Bakker48916f92012-09-16 19:57:18 +00008025
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008026 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008027 if( ssl->handshake == NULL ||
8028 ssl->transform_negotiate == NULL ||
8029 ssl->session_negotiate == NULL )
8030 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033 mbedtls_free( ssl->handshake );
8034 mbedtls_free( ssl->transform_negotiate );
8035 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008036
8037 ssl->handshake = NULL;
8038 ssl->transform_negotiate = NULL;
8039 ssl->session_negotiate = NULL;
8040
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008041 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008042 }
8043
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008044 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008046 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008047 ssl_handshake_params_init( ssl->handshake );
8048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008049#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008050 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008051 {
8052 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008053
Hanno Becker2d9623f2019-06-13 12:07:05 +01008054 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008055 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8056 else
8057 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8058 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008059#endif
8060
Paul Bakker48916f92012-09-16 19:57:18 +00008061 return( 0 );
8062}
8063
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008064#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008065/* Dummy cookie callbacks for defaults */
8066static int ssl_cookie_write_dummy( void *ctx,
8067 unsigned char **p, unsigned char *end,
8068 const unsigned char *cli_id, size_t cli_id_len )
8069{
8070 ((void) ctx);
8071 ((void) p);
8072 ((void) end);
8073 ((void) cli_id);
8074 ((void) cli_id_len);
8075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008077}
8078
8079static int ssl_cookie_check_dummy( void *ctx,
8080 const unsigned char *cookie, size_t cookie_len,
8081 const unsigned char *cli_id, size_t cli_id_len )
8082{
8083 ((void) ctx);
8084 ((void) cookie);
8085 ((void) cookie_len);
8086 ((void) cli_id);
8087 ((void) cli_id_len);
8088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008090}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008091#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008092
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008093/* Once ssl->out_hdr as the address of the beginning of the
8094 * next outgoing record is set, deduce the other pointers.
8095 *
8096 * Note: For TLS, we save the implicit record sequence number
8097 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8098 * and the caller has to make sure there's space for this.
8099 */
8100
8101static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8102 mbedtls_ssl_transform *transform )
8103{
8104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008105 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008106 {
8107 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008108#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008109 ssl->out_cid = ssl->out_ctr + 8;
8110 ssl->out_len = ssl->out_cid;
8111 if( transform != NULL )
8112 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008113#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008114 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008115#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008116 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008117 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008118 MBEDTLS_SSL_TRANSPORT_ELSE
8119#endif /* MBEDTLS_SSL_PROTO_DTLS */
8120#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008121 {
8122 ssl->out_ctr = ssl->out_hdr - 8;
8123 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008124#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008125 ssl->out_cid = ssl->out_len;
8126#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008127 ssl->out_iv = ssl->out_hdr + 5;
8128 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008129#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008130
8131 /* Adjust out_msg to make space for explicit IV, if used. */
8132 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008133 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008134 {
8135 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8136 }
8137 else
8138 ssl->out_msg = ssl->out_iv;
8139}
8140
8141/* Once ssl->in_hdr as the address of the beginning of the
8142 * next incoming record is set, deduce the other pointers.
8143 *
8144 * Note: For TLS, we save the implicit record sequence number
8145 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8146 * and the caller has to make sure there's space for this.
8147 */
8148
Hanno Beckerf5970a02019-05-08 09:38:41 +01008149static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008150{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008151 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008152 * of unprotected TLS/DTLS records, with ssl->in_msg
8153 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008154 *
8155 * When decrypting a protected record, ssl->in_msg
8156 * will be shifted to point to the beginning of the
8157 * record plaintext.
8158 */
8159
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008160#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008161 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008162 {
Hanno Becker70e79282019-05-03 14:34:53 +01008163 /* This sets the header pointers to match records
8164 * without CID. When we receive a record containing
8165 * a CID, the fields are shifted accordingly in
8166 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008167 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008168#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008169 ssl->in_cid = ssl->in_ctr + 8;
8170 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008171#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008172 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008173#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008174 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008175 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008176 MBEDTLS_SSL_TRANSPORT_ELSE
8177#endif /* MBEDTLS_SSL_PROTO_DTLS */
8178#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008179 {
8180 ssl->in_ctr = ssl->in_hdr - 8;
8181 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008183 ssl->in_cid = ssl->in_len;
8184#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008185 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008186 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008187#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008188}
8189
Paul Bakker5121ce52009-01-03 21:22:43 +00008190/*
8191 * Initialize an SSL context
8192 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008193void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8194{
8195 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8196}
8197
8198/*
8199 * Setup an SSL context
8200 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008201
8202static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8203{
8204 /* Set the incoming and outgoing record pointers. */
8205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008206 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008207 {
8208 ssl->out_hdr = ssl->out_buf;
8209 ssl->in_hdr = ssl->in_buf;
8210 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008211 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008213#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008214 {
8215 ssl->out_hdr = ssl->out_buf + 8;
8216 ssl->in_hdr = ssl->in_buf + 8;
8217 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008218#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008219
8220 /* Derive other internal pointers. */
8221 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008222 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008223}
8224
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008225int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008226 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008227{
Paul Bakker48916f92012-09-16 19:57:18 +00008228 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008229
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008230 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008231
Hanno Beckeref982d52019-07-23 15:56:18 +01008232#if defined(MBEDTLS_USE_TINYCRYPT)
8233 uECC_set_rng( &uecc_rng_wrapper );
8234#endif
8235
Paul Bakker62f2dee2012-09-28 07:31:51 +00008236 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008237 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008238 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008239
8240 /* Set to NULL in case of an error condition */
8241 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008242
Angus Grattond8213d02016-05-25 20:56:48 +10008243 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8244 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008245 {
Angus Grattond8213d02016-05-25 20:56:48 +10008246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_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;
Angus Grattond8213d02016-05-25 20:56:48 +10008249 }
8250
8251 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8252 if( ssl->out_buf == NULL )
8253 {
8254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008255 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008256 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008257 }
8258
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008259 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008260
Paul Bakker48916f92012-09-16 19:57:18 +00008261 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008262 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008263
Hanno Beckerc8f52992019-07-25 11:15:08 +01008264 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008265
Paul Bakker5121ce52009-01-03 21:22:43 +00008266 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008267
8268error:
8269 mbedtls_free( ssl->in_buf );
8270 mbedtls_free( ssl->out_buf );
8271
8272 ssl->conf = NULL;
8273
8274 ssl->in_buf = NULL;
8275 ssl->out_buf = NULL;
8276
8277 ssl->in_hdr = NULL;
8278 ssl->in_ctr = NULL;
8279 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008280 ssl->in_msg = NULL;
8281
8282 ssl->out_hdr = NULL;
8283 ssl->out_ctr = NULL;
8284 ssl->out_len = NULL;
8285 ssl->out_iv = NULL;
8286 ssl->out_msg = NULL;
8287
k-stachowiak9f7798e2018-07-31 16:52:32 +02008288 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008289}
8290
8291/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008292 * Reset an initialized and used SSL context for re-use while retaining
8293 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008294 *
8295 * If partial is non-zero, keep data in the input buffer and client ID.
8296 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008297 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008298static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008299{
Paul Bakker48916f92012-09-16 19:57:18 +00008300 int ret;
8301
Hanno Becker7e772132018-08-10 12:38:21 +01008302#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8303 !defined(MBEDTLS_SSL_SRV_C)
8304 ((void) partial);
8305#endif
8306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008308
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008309 /* Cancel any possibly running timer */
8310 ssl_set_timer( ssl, 0 );
8311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008312#if defined(MBEDTLS_SSL_RENEGOTIATION)
8313 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008314 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008315
8316 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8318 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008319#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008321
Paul Bakker7eb013f2011-10-06 12:37:39 +00008322 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008323 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008324
8325 ssl->in_msgtype = 0;
8326 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008328 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008329 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008332 ssl_dtls_replay_reset( ssl );
8333#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008334
8335 ssl->in_hslen = 0;
8336 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008337
8338 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008339
8340 ssl->out_msgtype = 0;
8341 ssl->out_msglen = 0;
8342 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8344 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008345 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008346#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008347
Hanno Becker19859472018-08-06 09:40:20 +01008348 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8349
Paul Bakker48916f92012-09-16 19:57:18 +00008350 ssl->transform_in = NULL;
8351 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008352
Hanno Becker78640902018-08-13 16:35:15 +01008353 ssl->session_in = NULL;
8354 ssl->session_out = NULL;
8355
Angus Grattond8213d02016-05-25 20:56:48 +10008356 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008357
8358#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008359 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008360#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8361 {
8362 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008363 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008364 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8367 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8370 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8373 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008374 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008375 }
8376#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008377
Paul Bakker48916f92012-09-16 19:57:18 +00008378 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008380 mbedtls_ssl_transform_free( ssl->transform );
8381 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008382 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008383 }
Paul Bakker48916f92012-09-16 19:57:18 +00008384
Paul Bakkerc0463502013-02-14 11:19:38 +01008385 if( ssl->session )
8386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008387 mbedtls_ssl_session_free( ssl->session );
8388 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008389 ssl->session = NULL;
8390 }
8391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008393 ssl->alpn_chosen = NULL;
8394#endif
8395
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008396#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008397#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008398 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008399#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008400 {
8401 mbedtls_free( ssl->cli_id );
8402 ssl->cli_id = NULL;
8403 ssl->cli_id_len = 0;
8404 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008405#endif
8406
Paul Bakker48916f92012-09-16 19:57:18 +00008407 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8408 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008409
8410 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008411}
8412
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008413/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008414 * Reset an initialized and used SSL context for re-use while retaining
8415 * all application-set variables, function pointers and data.
8416 */
8417int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8418{
8419 return( ssl_session_reset_int( ssl, 0 ) );
8420}
8421
8422/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008423 * SSL set accessors
8424 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008425#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008426void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008428 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008429}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008430#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008431
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008432void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008434 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008435}
8436
Hanno Becker7f376f42019-06-12 16:20:48 +01008437#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8438 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008439void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008440{
Hanno Becker7f376f42019-06-12 16:20:48 +01008441 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008442}
Hanno Becker7f376f42019-06-12 16:20:48 +01008443#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008444
Hanno Beckerde671542019-06-12 16:30:46 +01008445#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8446 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8447void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8448 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008449{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008450 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008451}
Hanno Beckerde671542019-06-12 16:30:46 +01008452#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008454#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008455
Hanno Becker1841b0a2018-08-24 11:13:57 +01008456void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8457 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008458{
8459 ssl->disable_datagram_packing = !allow_packing;
8460}
8461
Hanno Becker1f835fa2019-06-13 10:14:59 +01008462#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8463 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008464void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8465 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008466{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008467 conf->hs_timeout_min = min;
8468 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008469}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008470#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8471 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8472void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8473 uint32_t min, uint32_t max )
8474{
8475 ((void) conf);
8476 ((void) min);
8477 ((void) max);
8478}
8479#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8480 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8481
8482#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008483
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008484void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008485{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008486#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8487 conf->authmode = authmode;
8488#else
8489 ((void) conf);
8490 ((void) authmode);
8491#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008492}
8493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008495void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008496 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008497 void *p_vrfy )
8498{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008499 conf->f_vrfy = f_vrfy;
8500 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008503
Hanno Beckerece325c2019-06-13 15:39:27 +01008504#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008505void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008506 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008507 void *p_rng )
8508{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008509 conf->f_rng = f_rng;
8510 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008511}
Hanno Beckerece325c2019-06-13 15:39:27 +01008512#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008513
Hanno Becker14a4a442019-07-02 17:00:34 +01008514#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008515void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008516 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008517 void *p_dbg )
8518{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008519 conf->f_dbg = f_dbg;
8520 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008521}
Hanno Becker14a4a442019-07-02 17:00:34 +01008522#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008523
Hanno Beckera58a8962019-06-13 16:11:15 +01008524#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8525 !defined(MBEDTLS_SSL_CONF_SEND) && \
8526 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008528 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008529 mbedtls_ssl_send_t *f_send,
8530 mbedtls_ssl_recv_t *f_recv,
8531 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008532{
Hanno Beckera58a8962019-06-13 16:11:15 +01008533 ssl->p_bio = p_bio;
8534 ssl->f_send = f_send;
8535 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008536 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008537}
Hanno Beckera58a8962019-06-13 16:11:15 +01008538#else
8539void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8540 void *p_bio )
8541{
8542 ssl->p_bio = p_bio;
8543}
8544#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008545
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008546#if defined(MBEDTLS_SSL_PROTO_DTLS)
8547void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8548{
8549 ssl->mtu = mtu;
8550}
8551#endif
8552
Hanno Becker1f835fa2019-06-13 10:14:59 +01008553#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008554void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008555{
8556 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008557}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008558#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008559
Hanno Becker0ae6b242019-06-13 16:45:36 +01008560#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8561 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008562void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8563 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008564 mbedtls_ssl_set_timer_t *f_set_timer,
8565 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008566{
8567 ssl->p_timer = p_timer;
8568 ssl->f_set_timer = f_set_timer;
8569 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008570 /* Make sure we start with no timer running */
8571 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008572}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008573#else
8574void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8575 void *p_timer )
8576{
8577 ssl->p_timer = p_timer;
8578 /* Make sure we start with no timer running */
8579 ssl_set_timer( ssl, 0 );
8580}
8581#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008582
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008583#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008584void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008585 void *p_cache,
8586 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8587 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008588{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008589 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008590 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008591 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008592}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008593#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008594
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008595#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008597{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008598 int ret;
8599
8600 if( ssl == NULL ||
8601 session == NULL ||
8602 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008603 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008606 }
8607
Hanno Becker58fccf22019-02-06 14:30:46 +00008608 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8609 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008610 return( ret );
8611
Paul Bakker0a597072012-09-25 21:55:46 +00008612 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008613
8614 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008615}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008616#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008617
Hanno Becker73f4cb12019-06-27 13:51:07 +01008618#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008619void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008620 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008622 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8623 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8624 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8625 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008626}
8627
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008628void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008629 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008630 int major, int minor )
8631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008633 return;
8634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008636 return;
8637
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008638 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008639}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008640#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008643void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008644 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008645{
8646 conf->cert_profile = profile;
8647}
8648
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008649/* Append a new keycert entry to a (possibly empty) list */
8650static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8651 mbedtls_x509_crt *cert,
8652 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008653{
niisato8ee24222018-06-25 19:05:48 +09008654 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008655
niisato8ee24222018-06-25 19:05:48 +09008656 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8657 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008658 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008659
niisato8ee24222018-06-25 19:05:48 +09008660 new_cert->cert = cert;
8661 new_cert->key = key;
8662 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008663
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008664 /* Update head is the list was null, else add to the end */
8665 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008666 {
niisato8ee24222018-06-25 19:05:48 +09008667 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008668 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008669 else
8670 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008671 mbedtls_ssl_key_cert *cur = *head;
8672 while( cur->next != NULL )
8673 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008674 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008675 }
8676
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008677 return( 0 );
8678}
8679
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008680int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008681 mbedtls_x509_crt *own_cert,
8682 mbedtls_pk_context *pk_key )
8683{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008684 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008685}
8686
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008687void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008688 mbedtls_x509_crt *ca_chain,
8689 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008690{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008691 conf->ca_chain = ca_chain;
8692 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008695
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008696#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8697int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8698 mbedtls_x509_crt *own_cert,
8699 mbedtls_pk_context *pk_key )
8700{
8701 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8702 own_cert, pk_key ) );
8703}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008704
8705void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8706 mbedtls_x509_crt *ca_chain,
8707 mbedtls_x509_crl *ca_crl )
8708{
8709 ssl->handshake->sni_ca_chain = ca_chain;
8710 ssl->handshake->sni_ca_crl = ca_crl;
8711}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008712
8713void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8714 int authmode )
8715{
8716 ssl->handshake->sni_authmode = authmode;
8717}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008718#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8719
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008720#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008721/*
8722 * Set EC J-PAKE password for current handshake
8723 */
8724int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8725 const unsigned char *pw,
8726 size_t pw_len )
8727{
8728 mbedtls_ecjpake_role role;
8729
Janos Follath8eb64132016-06-03 15:40:57 +01008730 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008731 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8732
Hanno Becker2d9623f2019-06-13 12:07:05 +01008733 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008734 role = MBEDTLS_ECJPAKE_SERVER;
8735 else
8736 role = MBEDTLS_ECJPAKE_CLIENT;
8737
8738 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8739 role,
8740 MBEDTLS_MD_SHA256,
8741 MBEDTLS_ECP_DP_SECP256R1,
8742 pw, pw_len ) );
8743}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008744#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008746#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008747int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008748 const unsigned char *psk, size_t psk_len,
8749 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008750{
Paul Bakker6db455e2013-09-18 17:29:31 +02008751 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008754 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008756
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008757 /* Identity len will be encoded on two bytes */
8758 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008759 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008760 {
8761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8762 }
8763
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008764 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008765 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008766 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008767
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008768 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008769 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008770 conf->psk_len = 0;
8771 }
8772 if( conf->psk_identity != NULL )
8773 {
8774 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008775 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008776 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008777 }
8778
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008779 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8780 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008781 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008782 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008783 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008784 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008785 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008786 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008787 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008788
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008789 conf->psk_len = psk_len;
8790 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008791
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008792 memcpy( conf->psk, psk, conf->psk_len );
8793 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008794
8795 return( 0 );
8796}
8797
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008798int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8799 const unsigned char *psk, size_t psk_len )
8800{
8801 if( psk == NULL || ssl->handshake == NULL )
8802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8803
8804 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8806
8807 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008808 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008809 mbedtls_platform_zeroize( ssl->handshake->psk,
8810 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008811 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008812 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008813 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008814
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008815 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008816 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008817
8818 ssl->handshake->psk_len = psk_len;
8819 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8820
8821 return( 0 );
8822}
8823
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008824void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008826 size_t),
8827 void *p_psk )
8828{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008829 conf->f_psk = f_psk;
8830 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008833
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008834#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008835
8836#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008837int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008838{
8839 int ret;
8840
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008841 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8842 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8843 {
8844 mbedtls_mpi_free( &conf->dhm_P );
8845 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008846 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008847 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008848
8849 return( 0 );
8850}
Hanno Becker470a8c42017-10-04 15:28:46 +01008851#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008852
Hanno Beckera90658f2017-10-04 15:29:08 +01008853int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8854 const unsigned char *dhm_P, size_t P_len,
8855 const unsigned char *dhm_G, size_t G_len )
8856{
8857 int ret;
8858
8859 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8860 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8861 {
8862 mbedtls_mpi_free( &conf->dhm_P );
8863 mbedtls_mpi_free( &conf->dhm_G );
8864 return( ret );
8865 }
8866
8867 return( 0 );
8868}
Paul Bakker5121ce52009-01-03 21:22:43 +00008869
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008870int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008871{
8872 int ret;
8873
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008874 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8875 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8876 {
8877 mbedtls_mpi_free( &conf->dhm_P );
8878 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008879 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008880 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008881
8882 return( 0 );
8883}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008884#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008885
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008886#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8887/*
8888 * Set the minimum length for Diffie-Hellman parameters
8889 */
8890void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8891 unsigned int bitlen )
8892{
8893 conf->dhm_min_bitlen = bitlen;
8894}
8895#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8896
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008897#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008898/*
8899 * Set allowed/preferred hashes for handshake signatures
8900 */
8901void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8902 const int *hashes )
8903{
Hanno Becker56595f42019-06-19 16:31:38 +01008904#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008905 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008906#else
8907 ((void) conf);
8908 ((void) hashes);
8909#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008910}
Hanno Becker947194e2017-04-07 13:25:49 +01008911#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008912
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008913#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008914#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008915/*
8916 * Set the allowed elliptic curves
8917 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008918void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008919 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008920{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008921 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008922}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008923#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008924#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008925
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008926#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008928{
Hanno Becker947194e2017-04-07 13:25:49 +01008929 /* Initialize to suppress unnecessary compiler warning */
8930 size_t hostname_len = 0;
8931
8932 /* Check if new hostname is valid before
8933 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008934 if( hostname != NULL )
8935 {
8936 hostname_len = strlen( hostname );
8937
8938 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8939 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8940 }
8941
8942 /* Now it's clear that we will overwrite the old hostname,
8943 * so we can free it safely */
8944
8945 if( ssl->hostname != NULL )
8946 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008947 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008948 mbedtls_free( ssl->hostname );
8949 }
8950
8951 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008952
Paul Bakker5121ce52009-01-03 21:22:43 +00008953 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008954 {
8955 ssl->hostname = NULL;
8956 }
8957 else
8958 {
8959 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008960 if( ssl->hostname == NULL )
8961 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008962
Hanno Becker947194e2017-04-07 13:25:49 +01008963 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008964
Hanno Becker947194e2017-04-07 13:25:49 +01008965 ssl->hostname[hostname_len] = '\0';
8966 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008967
8968 return( 0 );
8969}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008970#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008971
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008972#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008973void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008974 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008975 const unsigned char *, size_t),
8976 void *p_sni )
8977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008978 conf->f_sni = f_sni;
8979 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008980}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008981#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008983#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008984int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008985{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008986 size_t cur_len, tot_len;
8987 const char **p;
8988
8989 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008990 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8991 * MUST NOT be truncated."
8992 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008993 */
8994 tot_len = 0;
8995 for( p = protos; *p != NULL; p++ )
8996 {
8997 cur_len = strlen( *p );
8998 tot_len += cur_len;
8999
9000 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009002 }
9003
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009004 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009005
9006 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009007}
9008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009010{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009011 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009014
Hanno Becker33b9b252019-07-05 11:23:25 +01009015#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9016 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9017void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9018 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009019{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009020 conf->max_major_ver = major;
9021 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009022}
Hanno Becker33b9b252019-07-05 11:23:25 +01009023#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9024 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009025
Hanno Becker33b9b252019-07-05 11:23:25 +01009026#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9027 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9028void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9029 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009030{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009031 conf->min_major_ver = major;
9032 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009033}
Hanno Becker33b9b252019-07-05 11:23:25 +01009034#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9035 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009038void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009039{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009040 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009041}
9042#endif
9043
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009044#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009045void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9046 char cert_req_ca_list )
9047{
9048 conf->cert_req_ca_list = cert_req_ca_list;
9049}
9050#endif
9051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009053void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009055 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009056}
9057#endif
9058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009059#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009060#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009061void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009062{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009063 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009064}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009065#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9066#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009067void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009068 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009069{
9070 conf->enforce_extended_master_secret = ems_enf;
9071}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009072#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009073#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009074
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009075#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009076void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009077{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009078 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009079}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009080#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009083int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009084{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009085 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009086 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009089 }
9090
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009091 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009092
9093 return( 0 );
9094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009098void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009099{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009100 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009101}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009105void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009106{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009107 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009108}
9109#endif
9110
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009111#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009112void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009114 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009115}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009116#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009119void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009120{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009121 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009122}
9123
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009124void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009125{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009126 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009127}
9128
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009129void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009130 const unsigned char period[8] )
9131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009132 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009134#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009137#if defined(MBEDTLS_SSL_CLI_C)
9138void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009139{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009140 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009141}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009142#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009143
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009144#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009145void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9146 mbedtls_ssl_ticket_write_t *f_ticket_write,
9147 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9148 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009149{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009150 conf->f_ticket_write = f_ticket_write;
9151 conf->f_ticket_parse = f_ticket_parse;
9152 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009153}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009154#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009156
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009157#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9158void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9159 mbedtls_ssl_export_keys_t *f_export_keys,
9160 void *p_export_keys )
9161{
9162 conf->f_export_keys = f_export_keys;
9163 conf->p_export_keys = p_export_keys;
9164}
9165#endif
9166
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009167#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009168void mbedtls_ssl_conf_async_private_cb(
9169 mbedtls_ssl_config *conf,
9170 mbedtls_ssl_async_sign_t *f_async_sign,
9171 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9172 mbedtls_ssl_async_resume_t *f_async_resume,
9173 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009174 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009175{
9176 conf->f_async_sign_start = f_async_sign;
9177 conf->f_async_decrypt_start = f_async_decrypt;
9178 conf->f_async_resume = f_async_resume;
9179 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009180 conf->p_async_config_data = async_config_data;
9181}
9182
Gilles Peskine8f97af72018-04-26 11:46:10 +02009183void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9184{
9185 return( conf->p_async_config_data );
9186}
9187
Gilles Peskine1febfef2018-04-30 11:54:39 +02009188void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009189{
9190 if( ssl->handshake == NULL )
9191 return( NULL );
9192 else
9193 return( ssl->handshake->user_async_ctx );
9194}
9195
Gilles Peskine1febfef2018-04-30 11:54:39 +02009196void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009197 void *ctx )
9198{
9199 if( ssl->handshake != NULL )
9200 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009201}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009202#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009203
Paul Bakker5121ce52009-01-03 21:22:43 +00009204/*
9205 * SSL get accessors
9206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009207size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009208{
9209 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9210}
9211
Hanno Becker8b170a02017-10-10 11:51:19 +01009212int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9213{
9214 /*
9215 * Case A: We're currently holding back
9216 * a message for further processing.
9217 */
9218
9219 if( ssl->keep_current_message == 1 )
9220 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009222 return( 1 );
9223 }
9224
9225 /*
9226 * Case B: Further records are pending in the current datagram.
9227 */
9228
9229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009230 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009231 ssl->in_left > ssl->next_record_offset )
9232 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009234 return( 1 );
9235 }
9236#endif /* MBEDTLS_SSL_PROTO_DTLS */
9237
9238 /*
9239 * Case C: A handshake message is being processed.
9240 */
9241
Hanno Becker8b170a02017-10-10 11:51:19 +01009242 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9243 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009245 return( 1 );
9246 }
9247
9248 /*
9249 * Case D: An application data message is being processed
9250 */
9251 if( ssl->in_offt != NULL )
9252 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009254 return( 1 );
9255 }
9256
9257 /*
9258 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009259 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009260 * we implement support for multiple alerts in single records.
9261 */
9262
9263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9264 return( 0 );
9265}
9266
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009267uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009268{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009269 if( ssl->session != NULL )
9270 return( ssl->session->verify_result );
9271
9272 if( ssl->session_negotiate != NULL )
9273 return( ssl->session_negotiate->verify_result );
9274
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009275 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009276}
9277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009278const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009279{
Hanno Beckere02758c2019-06-26 15:31:31 +01009280 int suite;
9281
Paul Bakker926c8e42013-03-06 10:23:34 +01009282 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009283 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009284
Hanno Beckere02758c2019-06-26 15:31:31 +01009285 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9286 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009287}
9288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009292 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009293 {
Hanno Becker2881d802019-05-22 14:44:53 +01009294 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009297 return( "DTLSv1.0" );
9298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009300 return( "DTLSv1.2" );
9301
9302 default:
9303 return( "unknown (DTLS)" );
9304 }
9305 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009306 MBEDTLS_SSL_TRANSPORT_ELSE
9307#endif /* MBEDTLS_SSL_PROTO_DTLS */
9308#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009309 {
Hanno Becker2881d802019-05-22 14:44:53 +01009310 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009311 {
9312 case MBEDTLS_SSL_MINOR_VERSION_0:
9313 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009314
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009315 case MBEDTLS_SSL_MINOR_VERSION_1:
9316 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009317
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009318 case MBEDTLS_SSL_MINOR_VERSION_2:
9319 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009320
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009321 case MBEDTLS_SSL_MINOR_VERSION_3:
9322 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009323
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009324 default:
9325 return( "unknown" );
9326 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009327 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009328#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009329}
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009332{
Hanno Becker3136ede2018-08-17 15:28:19 +01009333 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009335
Hanno Becker43395762019-05-03 14:46:38 +01009336 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9337
Hanno Becker78640902018-08-13 16:35:15 +01009338 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009339 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341#if defined(MBEDTLS_ZLIB_SUPPORT)
9342 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9343 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009344#endif
9345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009347 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009348#if defined(MBEDTLS_GCM_C) || \
9349 defined(MBEDTLS_CCM_C) || \
9350 defined(MBEDTLS_CHACHAPOLY_C)
9351#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009353#endif
9354#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009356#endif
9357#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009358 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009359#endif
9360 transform_expansion =
9361 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009362 break;
9363
Hanno Beckera9d5c452019-07-25 16:47:12 +01009364#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9365 MBEDTLS_CHACHAPOLY_C */
9366
9367#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9368 case MBEDTLS_MODE_STREAM:
9369 transform_expansion = transform->maclen;
9370 break;
9371#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9372
9373#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009375 {
9376 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009377
9378 block_size = mbedtls_cipher_get_block_size(
9379 &transform->cipher_ctx_enc );
9380
Hanno Becker3136ede2018-08-17 15:28:19 +01009381 /* Expansion due to the addition of the MAC. */
9382 transform_expansion += transform->maclen;
9383
9384 /* Expansion due to the addition of CBC padding;
9385 * Theoretically up to 256 bytes, but we never use
9386 * more than the block size of the underlying cipher. */
9387 transform_expansion += block_size;
9388
9389 /* For TLS 1.1 or higher, an explicit IV is added
9390 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009391#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009392 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009393 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009394#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009395
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009396 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009397 }
9398#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009399
9400 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009403 }
9404
Hanno Beckera5a2b082019-05-15 14:03:01 +01009405#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009406 if( transform->out_cid_len != 0 )
9407 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009408#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009409
Hanno Becker43395762019-05-03 14:46:38 +01009410 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009411}
9412
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009413#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9414size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9415{
9416 size_t max_len;
9417
9418 /*
9419 * Assume mfl_code is correct since it was checked when set
9420 */
Angus Grattond8213d02016-05-25 20:56:48 +10009421 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009422
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009423 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009424 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009425 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009426 {
Angus Grattond8213d02016-05-25 20:56:48 +10009427 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009428 }
9429
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009430 /* During a handshake, use the value being negotiated */
9431 if( ssl->session_negotiate != NULL &&
9432 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9433 {
9434 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9435 }
9436
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009437 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009438}
9439#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9440
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009441#if defined(MBEDTLS_SSL_PROTO_DTLS)
9442static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9443{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009444 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009445 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009446 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9447 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9448 return ( 0 );
9449
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009450 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9451 return( ssl->mtu );
9452
9453 if( ssl->mtu == 0 )
9454 return( ssl->handshake->mtu );
9455
9456 return( ssl->mtu < ssl->handshake->mtu ?
9457 ssl->mtu : ssl->handshake->mtu );
9458}
9459#endif /* MBEDTLS_SSL_PROTO_DTLS */
9460
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009461int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9462{
9463 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9464
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009465#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9466 !defined(MBEDTLS_SSL_PROTO_DTLS)
9467 (void) ssl;
9468#endif
9469
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009470#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9471 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9472
9473 if( max_len > mfl )
9474 max_len = mfl;
9475#endif
9476
9477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009478 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009479 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009480 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009481 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9482 const size_t overhead = (size_t) ret;
9483
9484 if( ret < 0 )
9485 return( ret );
9486
9487 if( mtu <= overhead )
9488 {
9489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9490 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9491 }
9492
9493 if( max_len > mtu - overhead )
9494 max_len = mtu - overhead;
9495 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009496#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009497
Hanno Becker0defedb2018-08-10 12:35:02 +01009498#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9499 !defined(MBEDTLS_SSL_PROTO_DTLS)
9500 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009501#endif
9502
9503 return( (int) max_len );
9504}
9505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#if defined(MBEDTLS_X509_CRT_PARSE_C)
9507const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009508{
9509 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009510 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009511
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009512#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009513 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009514#else
9515 return( NULL );
9516#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009521int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9522 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009523{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009524 if( ssl == NULL ||
9525 dst == NULL ||
9526 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009527 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009530 }
9531
Hanno Becker58fccf22019-02-06 14:30:46 +00009532 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009535
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009536const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9537{
9538 if( ssl == NULL )
9539 return( NULL );
9540
9541 return( ssl->session );
9542}
9543
Paul Bakker5121ce52009-01-03 21:22:43 +00009544/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009545 * Define ticket header determining Mbed TLS version
9546 * and structure of the ticket.
9547 */
9548
Hanno Becker41527622019-05-16 12:50:45 +01009549/*
Hanno Becker26829e92019-05-28 14:30:45 +01009550 * Define bitflag determining compile-time settings influencing
9551 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009552 */
9553
Hanno Becker26829e92019-05-28 14:30:45 +01009554#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009555#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009556#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009557#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009558#endif /* MBEDTLS_HAVE_TIME */
9559
9560#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009561#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009562#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009563#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009564#endif /* MBEDTLS_X509_CRT_PARSE_C */
9565
9566#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009567#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009568#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009569#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009570#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9571
9572#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009573#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009574#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009575#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009576#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9577
9578#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009579#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009580#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009581#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009582#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9583
9584#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009585#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009586#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009587#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009588#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9589
Hanno Becker41527622019-05-16 12:50:45 +01009590#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9591#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9592#else
9593#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9594#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9595
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009596#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9597#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9598#else
9599#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9600#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9601
Hanno Becker88440552019-07-03 14:16:13 +01009602#if defined(MBEDTLS_ZLIB_SUPPORT)
9603#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9604#else
9605#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9606#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9607
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009608#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9609#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9610#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9611#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9612#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9613#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9614#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009615#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009616#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009617
Hanno Becker26829e92019-05-28 14:30:45 +01009618#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009619 ( (uint16_t) ( \
9620 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9621 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9622 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9623 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9624 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9625 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009626 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009627 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009628 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009629
Hanno Becker557fe9f2019-05-16 12:41:07 +01009630static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009631 MBEDTLS_VERSION_MAJOR,
9632 MBEDTLS_VERSION_MINOR,
9633 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009634 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9635 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009636};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009637
9638/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009639 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009640 * (in the presentation language of TLS, RFC 8446 section 3)
9641 *
Hanno Becker26829e92019-05-28 14:30:45 +01009642 * opaque mbedtls_version[3]; // major, minor, patch
9643 * opaque session_format[2]; // version-specific 16-bit field determining
9644 * // the format of the remaining
9645 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009646 *
9647 * Note: When updating the format, remember to keep
9648 * these version+format bytes.
9649 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009650 * // In this version, `session_format` determines
9651 * // the setting of those compile-time
9652 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009653 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009654 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009655 * uint8 ciphersuite[2]; // defined by the standard
9656 * uint8 compression; // 0 or 1
9657 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009658 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009659 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009660 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009661 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9662 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9663 * case disabled: uint8_t peer_cert_digest_type;
9664 * opaque peer_cert_digest<0..2^8-1>;
9665 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009666 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009667 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009668 * uint8 mfl_code; // up to 255 according to standard
9669 * uint8 trunc_hmac; // 0 or 1
9670 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009671 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009672 * The order is the same as in the definition of the structure, except
9673 * verify_result is put before peer_cert so that all mandatory fields come
9674 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009675 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009676static int ssl_session_save( const mbedtls_ssl_session *session,
9677 unsigned char omit_header,
9678 unsigned char *buf,
9679 size_t buf_len,
9680 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009681{
9682 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009683 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009684#if defined(MBEDTLS_HAVE_TIME)
9685 uint64_t start;
9686#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009687#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009688#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009689 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009690#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009691#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009692
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009693 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009694 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009695 /*
9696 * Add version identifier
9697 */
9698
9699 used += sizeof( ssl_serialized_session_header );
9700
9701 if( used <= buf_len )
9702 {
9703 memcpy( p, ssl_serialized_session_header,
9704 sizeof( ssl_serialized_session_header ) );
9705 p += sizeof( ssl_serialized_session_header );
9706 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009707 }
9708
9709 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009710 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009711 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009712#if defined(MBEDTLS_HAVE_TIME)
9713 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009714
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009715 if( used <= buf_len )
9716 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009717 start = (uint64_t) session->start;
9718
9719 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9720 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9721 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9722 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9723 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9724 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9725 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9726 *p++ = (unsigned char)( ( start ) & 0xFF );
9727 }
9728#endif /* MBEDTLS_HAVE_TIME */
9729
9730 /*
9731 * Basic mandatory fields
9732 */
Hanno Becker88440552019-07-03 14:16:13 +01009733 {
9734 size_t const ciphersuite_len = 2;
9735#if defined(MBEDTLS_ZLIB_SUPPORT)
9736 size_t const compression_len = 1;
9737#else
9738 size_t const compression_len = 0;
9739#endif
9740 size_t const id_len_len = 1;
9741 size_t const id_len = 32;
9742 size_t const master_len = 48;
9743 size_t const verif_result_len = 4;
9744
9745 size_t const basic_len =
9746 ciphersuite_len +
9747 compression_len +
9748 id_len_len +
9749 id_len +
9750 master_len +
9751 verif_result_len;
9752
9753 used += basic_len;
9754 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009755
9756 if( used <= buf_len )
9757 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009758 const int ciphersuite =
9759 mbedtls_ssl_session_get_ciphersuite( session );
9760 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9761 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009762
Hanno Becker88440552019-07-03 14:16:13 +01009763#if defined(MBEDTLS_ZLIB_SUPPORT)
9764 *p++ = (unsigned char)(
9765 mbedtls_ssl_session_get_compression( session ) );
9766#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009767
9768 *p++ = (unsigned char)( session->id_len & 0xFF );
9769 memcpy( p, session->id, 32 );
9770 p += 32;
9771
9772 memcpy( p, session->master, 48 );
9773 p += 48;
9774
9775 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9776 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9777 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9778 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009779 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009780
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009781 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009782 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009783 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009784#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009785#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009786 if( session->peer_cert == NULL )
9787 cert_len = 0;
9788 else
9789 cert_len = session->peer_cert->raw.len;
9790
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009791 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009792
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009793 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009794 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009795 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9796 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9797 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9798
9799 if( session->peer_cert != NULL )
9800 {
9801 memcpy( p, session->peer_cert->raw.p, cert_len );
9802 p += cert_len;
9803 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009804 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009805
Hanno Becker5882dd02019-06-06 16:25:57 +01009806#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009807 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009808 if( session->peer_cert_digest != NULL )
9809 {
9810 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9811 if( used <= buf_len )
9812 {
9813 *p++ = (unsigned char) session->peer_cert_digest_type;
9814 *p++ = (unsigned char) session->peer_cert_digest_len;
9815 memcpy( p, session->peer_cert_digest,
9816 session->peer_cert_digest_len );
9817 p += session->peer_cert_digest_len;
9818 }
9819 }
9820 else
9821 {
9822 used += 2;
9823 if( used <= buf_len )
9824 {
9825 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9826 *p++ = 0;
9827 }
9828 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009829#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009830#endif /* MBEDTLS_X509_CRT_PARSE_C */
9831
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009832 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009833 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009834 */
9835#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009836 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009837
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009838 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009839 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009840 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9841 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9842 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9843
9844 if( session->ticket != NULL )
9845 {
9846 memcpy( p, session->ticket, session->ticket_len );
9847 p += session->ticket_len;
9848 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009849
9850 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9851 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9852 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9853 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009854 }
9855#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9856
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009857 /*
9858 * Misc extension-related info
9859 */
9860#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9861 used += 1;
9862
9863 if( used <= buf_len )
9864 *p++ = session->mfl_code;
9865#endif
9866
9867#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9868 used += 1;
9869
9870 if( used <= buf_len )
9871 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9872#endif
9873
9874#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9875 used += 1;
9876
9877 if( used <= buf_len )
9878 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9879#endif
9880
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009881 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009882 *olen = used;
9883
9884 if( used > buf_len )
9885 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009886
9887 return( 0 );
9888}
9889
9890/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009891 * Public wrapper for ssl_session_save()
9892 */
9893int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9894 unsigned char *buf,
9895 size_t buf_len,
9896 size_t *olen )
9897{
9898 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9899}
9900
9901/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009902 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009903 *
9904 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009905 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009906 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009907static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009908 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009909 const unsigned char *buf,
9910 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009911{
9912 const unsigned char *p = buf;
9913 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009914 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009915#if defined(MBEDTLS_HAVE_TIME)
9916 uint64_t start;
9917#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009918#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009919#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009920 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009921#endif
9922#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009923
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009924 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009925 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009926 /*
9927 * Check version identifier
9928 */
9929
9930 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9931 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9932
9933 if( memcmp( p, ssl_serialized_session_header,
9934 sizeof( ssl_serialized_session_header ) ) != 0 )
9935 {
9936 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9937 }
9938 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009939 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009940
9941 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009942 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009943 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009944#if defined(MBEDTLS_HAVE_TIME)
9945 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009946 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9947
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009948 start = ( (uint64_t) p[0] << 56 ) |
9949 ( (uint64_t) p[1] << 48 ) |
9950 ( (uint64_t) p[2] << 40 ) |
9951 ( (uint64_t) p[3] << 32 ) |
9952 ( (uint64_t) p[4] << 24 ) |
9953 ( (uint64_t) p[5] << 16 ) |
9954 ( (uint64_t) p[6] << 8 ) |
9955 ( (uint64_t) p[7] );
9956 p += 8;
9957
9958 session->start = (time_t) start;
9959#endif /* MBEDTLS_HAVE_TIME */
9960
9961 /*
9962 * Basic mandatory fields
9963 */
Hanno Becker88440552019-07-03 14:16:13 +01009964 {
9965 size_t const ciphersuite_len = 2;
9966#if defined(MBEDTLS_ZLIB_SUPPORT)
9967 size_t const compression_len = 1;
9968#else
9969 size_t const compression_len = 0;
9970#endif
9971 size_t const id_len_len = 1;
9972 size_t const id_len = 32;
9973 size_t const master_len = 48;
9974 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009975
Hanno Becker88440552019-07-03 14:16:13 +01009976 size_t const basic_len =
9977 ciphersuite_len +
9978 compression_len +
9979 id_len_len +
9980 id_len +
9981 master_len +
9982 verif_result_len;
9983
9984 if( basic_len > (size_t)( end - p ) )
9985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9986 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009987
Hanno Beckere02758c2019-06-26 15:31:31 +01009988 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009989 p += 2;
9990
Hanno Becker73f4cb12019-06-27 13:51:07 +01009991#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +01009992 session->ciphersuite = ciphersuite;
9993#else
9994 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +01009995 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +01009996 {
9997 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9998 }
9999#endif
10000
Hanno Becker88440552019-07-03 14:16:13 +010010001#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010002 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010003#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010004
10005 session->id_len = *p++;
10006 memcpy( session->id, p, 32 );
10007 p += 32;
10008
10009 memcpy( session->master, p, 48 );
10010 p += 48;
10011
10012 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10013 ( (uint32_t) p[1] << 16 ) |
10014 ( (uint32_t) p[2] << 8 ) |
10015 ( (uint32_t) p[3] );
10016 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010017
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010018 /* Immediately clear invalid pointer values that have been read, in case
10019 * we exit early before we replaced them with valid ones. */
10020#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010021#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010022 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010023#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010024 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010025#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010026#endif
10027#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10028 session->ticket = NULL;
10029#endif
10030
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010031 /*
10032 * Peer certificate
10033 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010035#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010036 if( 3 > (size_t)( end - p ) )
10037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10038
10039 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10040 p += 3;
10041
10042 if( cert_len == 0 )
10043 {
10044 session->peer_cert = NULL;
10045 }
10046 else
10047 {
10048 int ret;
10049
10050 if( cert_len > (size_t)( end - p ) )
10051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10052
10053 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10054
10055 if( session->peer_cert == NULL )
10056 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10057
10058 mbedtls_x509_crt_init( session->peer_cert );
10059
10060 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10061 p, cert_len ) ) != 0 )
10062 {
10063 mbedtls_x509_crt_free( session->peer_cert );
10064 mbedtls_free( session->peer_cert );
10065 session->peer_cert = NULL;
10066 return( ret );
10067 }
10068
10069 p += cert_len;
10070 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010071#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010072 /* Deserialize CRT digest from the end of the ticket. */
10073 if( 2 > (size_t)( end - p ) )
10074 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10075
10076 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10077 session->peer_cert_digest_len = (size_t) *p++;
10078
10079 if( session->peer_cert_digest_len != 0 )
10080 {
Hanno Becker2326d202019-06-06 14:54:55 +010010081 const mbedtls_md_info_t *md_info =
10082 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10083 if( md_info == NULL )
10084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10085 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10087
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010088 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10090
10091 session->peer_cert_digest =
10092 mbedtls_calloc( 1, session->peer_cert_digest_len );
10093 if( session->peer_cert_digest == NULL )
10094 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10095
10096 memcpy( session->peer_cert_digest, p,
10097 session->peer_cert_digest_len );
10098 p += session->peer_cert_digest_len;
10099 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010100#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010101#endif /* MBEDTLS_X509_CRT_PARSE_C */
10102
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010103 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010104 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010105 */
10106#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10107 if( 3 > (size_t)( end - p ) )
10108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10109
10110 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10111 p += 3;
10112
10113 if( session->ticket_len != 0 )
10114 {
10115 if( session->ticket_len > (size_t)( end - p ) )
10116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10117
10118 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10119 if( session->ticket == NULL )
10120 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10121
10122 memcpy( session->ticket, p, session->ticket_len );
10123 p += session->ticket_len;
10124 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010125
10126 if( 4 > (size_t)( end - p ) )
10127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10128
10129 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10130 ( (uint32_t) p[1] << 16 ) |
10131 ( (uint32_t) p[2] << 8 ) |
10132 ( (uint32_t) p[3] );
10133 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010134#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10135
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010136 /*
10137 * Misc extension-related info
10138 */
10139#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10140 if( 1 > (size_t)( end - p ) )
10141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10142
10143 session->mfl_code = *p++;
10144#endif
10145
10146#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10147 if( 1 > (size_t)( end - p ) )
10148 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10149
10150 session->trunc_hmac = *p++;
10151#endif
10152
10153#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10154 if( 1 > (size_t)( end - p ) )
10155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10156
10157 session->encrypt_then_mac = *p++;
10158#endif
10159
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010160 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010161 if( p != end )
10162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10163
10164 return( 0 );
10165}
10166
10167/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010168 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010169 */
10170int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10171 const unsigned char *buf,
10172 size_t len )
10173{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010174 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010175
10176 if( ret != 0 )
10177 mbedtls_ssl_session_free( session );
10178
10179 return( ret );
10180}
10181
10182/*
Paul Bakker1961b702013-01-25 14:49:24 +010010183 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010188
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010189 if( ssl == NULL || ssl->conf == NULL )
10190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010193 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010197 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010199#endif
10200
Hanno Beckerb82350b2019-07-26 07:24:05 +010010201 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010202 return( ret );
10203}
10204
10205/*
10206 * Perform the SSL handshake
10207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010209{
10210 int ret = 0;
10211
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010212 if( ssl == NULL || ssl->conf == NULL )
10213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010220
10221 if( ret != 0 )
10222 break;
10223 }
10224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010226
10227 return( ret );
10228}
10229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230#if defined(MBEDTLS_SSL_RENEGOTIATION)
10231#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010232/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010233 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010236{
10237 int ret;
10238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010240
10241 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10243 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010244
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010245 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010246 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010248 return( ret );
10249 }
10250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010252
10253 return( 0 );
10254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010256
10257/*
10258 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259 * - any side: calling mbedtls_ssl_renegotiate(),
10260 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10261 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010262 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010263 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010267{
10268 int ret;
10269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010271
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010272 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10273 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010274
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010275 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10276 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010278 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010279 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010280 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010281 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10282 MBEDTLS_SSL_IS_SERVER )
10283 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010284 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010285 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010286 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010287 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010288 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010289 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010290 }
10291#endif
10292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10294 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010296 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010299 return( ret );
10300 }
10301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010303
10304 return( 0 );
10305}
10306
10307/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010308 * Renegotiate current connection on client,
10309 * or request renegotiation on server
10310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010311int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010314
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010315 if( ssl == NULL || ssl->conf == NULL )
10316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010319 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010320 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010326
10327 /* Did we already try/start sending HelloRequest? */
10328 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010330
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010331 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010336 /*
10337 * On client, either start the renegotiation process or,
10338 * if already in progress, continue the handshake
10339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010340 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010342 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010344
10345 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010348 return( ret );
10349 }
10350 }
10351 else
10352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010356 return( ret );
10357 }
10358 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010360
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010361 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010362}
10363
10364/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010365 * Check record counters and renegotiate if they're above the limit.
10366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010368{
Andres AG2196c7f2016-12-15 17:01:16 +000010369 size_t ep_len = ssl_ep_len( ssl );
10370 int in_ctr_cmp;
10371 int out_ctr_cmp;
10372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10374 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010375 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010376 {
10377 return( 0 );
10378 }
10379
Andres AG2196c7f2016-12-15 17:01:16 +000010380 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10381 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010382 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010383 ssl->conf->renego_period + ep_len, 8 - ep_len );
10384
10385 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010386 {
10387 return( 0 );
10388 }
10389
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010392}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010394
10395/*
10396 * Receive application data decrypted from the SSL layer
10397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010398int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010399{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010400 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010401 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010402
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010403 if( ssl == NULL || ssl->conf == NULL )
10404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010409 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010412 return( ret );
10413
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010414 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010416 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010417 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010418 return( ret );
10419 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010420 }
10421#endif
10422
Hanno Becker4a810fb2017-05-24 16:27:30 +010010423 /*
10424 * Check if renegotiation is necessary and/or handshake is
10425 * in process. If yes, perform/continue, and fall through
10426 * if an unexpected packet is received while the client
10427 * is waiting for the ServerHello.
10428 *
10429 * (There is no equivalent to the last condition on
10430 * the server-side as it is not treated as within
10431 * a handshake while waiting for the ClientHello
10432 * after a renegotiation request.)
10433 */
10434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010436 ret = ssl_check_ctr_renegotiate( ssl );
10437 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10438 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010441 return( ret );
10442 }
10443#endif
10444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010448 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10449 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010452 return( ret );
10453 }
10454 }
10455
Hanno Beckere41158b2017-10-23 13:30:32 +010010456 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010457 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010458 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010459 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010460 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10461 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010462 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010463 ssl_set_timer( ssl,
10464 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010465 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010466
Hanno Becker327c93b2018-08-15 13:56:18 +010010467 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010468 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010469 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10470 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010471
Hanno Becker4a810fb2017-05-24 16:27:30 +010010472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10473 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010474 }
10475
10476 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010477 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010478 {
10479 /*
10480 * OpenSSL sends empty messages to randomize the IV
10481 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010482 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010485 return( 0 );
10486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010488 return( ret );
10489 }
10490 }
10491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010495
Hanno Becker4a810fb2017-05-24 16:27:30 +010010496 /*
10497 * - For client-side, expect SERVER_HELLO_REQUEST.
10498 * - For server-side, expect CLIENT_HELLO.
10499 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10500 */
10501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010503 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10504 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010506 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010509
10510 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010512 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010513 {
10514 continue;
10515 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010516 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010517#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010518#if defined(MBEDTLS_SSL_PROTO_TLS)
10519 {
10520 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10521 }
10522#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010523 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010524#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010525
Hanno Becker4a810fb2017-05-24 16:27:30 +010010526#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010527 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10528 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010532
10533 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010534#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010535 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010536 {
10537 continue;
10538 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010539 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010540#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010541#if defined(MBEDTLS_SSL_PROTO_TLS)
10542 {
10543 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10544 }
10545#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010546 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010547#endif /* MBEDTLS_SSL_SRV_C */
10548
Hanno Becker21df7f92017-10-17 11:03:26 +010010549#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010550 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010551 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10552 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010553 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010554 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10555 {
10556 /*
10557 * Accept renegotiation request
10558 */
Paul Bakker48916f92012-09-16 19:57:18 +000010559
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010560 /* DTLS clients need to know renego is server-initiated */
10561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010562 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010563 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10564 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010565 {
10566 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10567 }
10568#endif
10569 ret = ssl_start_renegotiation( ssl );
10570 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10571 ret != 0 )
10572 {
10573 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10574 return( ret );
10575 }
10576 }
10577 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010578#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010579 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010580 /*
10581 * Refuse renegotiation
10582 */
10583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010586#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010587 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010588 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010589 /* SSLv3 does not have a "no_renegotiation" warning, so
10590 we send a fatal alert and abort the connection. */
10591 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10592 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10593 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010594 }
10595 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10597#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10598 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010599 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010600 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010601 ret = mbedtls_ssl_send_alert_message( ssl,
10602 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10603 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10604 if( ret != 0 )
10605 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010606 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010607 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10609 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010613 }
Paul Bakker48916f92012-09-16 19:57:18 +000010614 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010615
Hanno Becker90333da2017-10-10 11:27:13 +010010616 /* At this point, we don't know whether the renegotiation has been
10617 * completed or not. The cases to consider are the following:
10618 * 1) The renegotiation is complete. In this case, no new record
10619 * has been read yet.
10620 * 2) The renegotiation is incomplete because the client received
10621 * an application data record while awaiting the ServerHello.
10622 * 3) The renegotiation is incomplete because the client received
10623 * a non-handshake, non-application data message while awaiting
10624 * the ServerHello.
10625 * In each of these case, looping will be the proper action:
10626 * - For 1), the next iteration will read a new record and check
10627 * if it's application data.
10628 * - For 2), the loop condition isn't satisfied as application data
10629 * is present, hence continue is the same as break
10630 * - For 3), the loop condition is satisfied and read_record
10631 * will re-deliver the message that was held back by the client
10632 * when expecting the ServerHello.
10633 */
10634 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010635 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010636#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010637 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010638 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010639 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010640 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010641 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010644 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010645 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010646 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010647 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010648 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10652 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010655 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010656 }
10657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010658 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10661 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010662 }
10663
10664 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010665
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010666 /* We're going to return something now, cancel timer,
10667 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010668 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010669 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010670
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010671#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010672 /* If we requested renego but received AppData, resend HelloRequest.
10673 * Do it now, after setting in_offt, to avoid taking this branch
10674 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010675#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010676 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10677 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010678 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010679 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010680 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010682 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010683 return( ret );
10684 }
10685 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010687#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010688 }
10689
10690 n = ( len < ssl->in_msglen )
10691 ? len : ssl->in_msglen;
10692
10693 memcpy( buf, ssl->in_offt, n );
10694 ssl->in_msglen -= n;
10695
10696 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010697 {
10698 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010699 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010700 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010701 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010702 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010703 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010704 /* more data available */
10705 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010706 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010709
Paul Bakker23986e52011-04-24 08:57:21 +000010710 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010711}
10712
10713/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010714 * Send application data to be encrypted by the SSL layer, taking care of max
10715 * fragment length and buffer size.
10716 *
10717 * According to RFC 5246 Section 6.2.1:
10718 *
10719 * Zero-length fragments of Application data MAY be sent as they are
10720 * potentially useful as a traffic analysis countermeasure.
10721 *
10722 * Therefore, it is possible that the input message length is 0 and the
10723 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010724 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010725static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010726 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010727{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010728 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10729 const size_t max_len = (size_t) ret;
10730
10731 if( ret < 0 )
10732 {
10733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10734 return( ret );
10735 }
10736
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010737 if( len > max_len )
10738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010740 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010743 "maximum fragment length: %d > %d",
10744 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010746 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010747 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010748#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010749#if defined(MBEDTLS_SSL_PROTO_TLS)
10750 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010751 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010752 }
10753#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010754 }
Paul Bakker887bd502011-06-08 13:10:54 +000010755
Paul Bakker5121ce52009-01-03 21:22:43 +000010756 if( ssl->out_left != 0 )
10757 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010758 /*
10759 * The user has previously tried to send the data and
10760 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10761 * written. In this case, we expect the high-level write function
10762 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010766 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010767 return( ret );
10768 }
10769 }
Paul Bakker887bd502011-06-08 13:10:54 +000010770 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010771 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010772 /*
10773 * The user is trying to send a message the first time, so we need to
10774 * copy the data into the internal buffers and setup the data structure
10775 * to keep track of partial writes
10776 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010777 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010779 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010780
Hanno Becker67bc7c32018-08-06 11:33:50 +010010781 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010784 return( ret );
10785 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010786 }
10787
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010788 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010789}
10790
10791/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010792 * Write application data, doing 1/n-1 splitting if necessary.
10793 *
10794 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010795 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010796 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010798#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010799static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010800 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010801{
10802 int ret;
10803
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010804 if( ssl->conf->cbc_record_splitting ==
10805 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010806 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010807 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010808 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10809 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010810 {
10811 return( ssl_write_real( ssl, buf, len ) );
10812 }
10813
10814 if( ssl->split_done == 0 )
10815 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010816 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010817 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010818 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010819 }
10820
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010821 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10822 return( ret );
10823 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010824
10825 return( ret + 1 );
10826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010828
10829/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010830 * Write application data (public-facing wrapper)
10831 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010832int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010833{
10834 int ret;
10835
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010837
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010838 if( ssl == NULL || ssl->conf == NULL )
10839 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10840
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010841#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010842 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10843 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010844 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010845 return( ret );
10846 }
10847#endif
10848
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010849 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010850 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010851 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010852 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010853 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010854 return( ret );
10855 }
10856 }
10857
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010858#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010859 ret = ssl_write_split( ssl, buf, len );
10860#else
10861 ret = ssl_write_real( ssl, buf, len );
10862#endif
10863
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010865
10866 return( ret );
10867}
10868
10869/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010870 * Notify the peer that the connection is being closed
10871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010873{
10874 int ret;
10875
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010876 if( ssl == NULL || ssl->conf == NULL )
10877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010880
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010881 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010882 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010884 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10887 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10888 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010891 return( ret );
10892 }
10893 }
10894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010896
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010897 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010898}
10899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010901{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010902 if( transform == NULL )
10903 return;
10904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010905#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010906 deflateEnd( &transform->ctx_deflate );
10907 inflateEnd( &transform->ctx_inflate );
10908#endif
10909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10911 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010912
Hanno Becker92231322018-01-03 15:32:51 +000010913#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914 mbedtls_md_free( &transform->md_ctx_enc );
10915 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010916#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010917
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010918 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010919}
10920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921#if defined(MBEDTLS_X509_CRT_PARSE_C)
10922static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010925
10926 while( cur != NULL )
10927 {
10928 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010930 cur = next;
10931 }
10932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010933#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010934
Hanno Becker0271f962018-08-16 13:23:47 +010010935#if defined(MBEDTLS_SSL_PROTO_DTLS)
10936
10937static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10938{
10939 unsigned offset;
10940 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10941
10942 if( hs == NULL )
10943 return;
10944
Hanno Becker283f5ef2018-08-24 09:34:47 +010010945 ssl_free_buffered_record( ssl );
10946
Hanno Becker0271f962018-08-16 13:23:47 +010010947 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010948 ssl_buffering_free_slot( ssl, offset );
10949}
10950
10951static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10952 uint8_t slot )
10953{
10954 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10955 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010956
10957 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10958 return;
10959
Hanno Beckere605b192018-08-21 15:59:07 +010010960 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010961 {
Hanno Beckere605b192018-08-21 15:59:07 +010010962 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010963 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010964 mbedtls_free( hs_buf->data );
10965 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010966 }
10967}
10968
10969#endif /* MBEDTLS_SSL_PROTO_DTLS */
10970
Gilles Peskine9b562d52018-04-25 20:32:43 +020010971void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010972{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010973 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10974
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010975 if( handshake == NULL )
10976 return;
10977
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010978#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10979 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10980 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010981 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010982 handshake->async_in_progress = 0;
10983 }
10984#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10985
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010986#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10987 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10988 mbedtls_md5_free( &handshake->fin_md5 );
10989 mbedtls_sha1_free( &handshake->fin_sha1 );
10990#endif
10991#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10992#if defined(MBEDTLS_SHA256_C)
10993 mbedtls_sha256_free( &handshake->fin_sha256 );
10994#endif
10995#if defined(MBEDTLS_SHA512_C)
10996 mbedtls_sha512_free( &handshake->fin_sha512 );
10997#endif
10998#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011000#if defined(MBEDTLS_DHM_C)
11001 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003#if defined(MBEDTLS_ECDH_C)
11004 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011005#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011006#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011007 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011008#if defined(MBEDTLS_SSL_CLI_C)
11009 mbedtls_free( handshake->ecjpake_cache );
11010 handshake->ecjpake_cache = NULL;
11011 handshake->ecjpake_cache_len = 0;
11012#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011013#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011014
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011015#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11016 if( handshake->psk != NULL )
11017 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011018 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011019 mbedtls_free( handshake->psk );
11020 }
11021#endif
11022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011023#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11024 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011025 /*
11026 * Free only the linked list wrapper, not the keys themselves
11027 * since the belong to the SNI callback
11028 */
11029 if( handshake->sni_key_cert != NULL )
11030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011032
11033 while( cur != NULL )
11034 {
11035 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011037 cur = next;
11038 }
11039 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011041
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011042#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011043 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011044 if( handshake->ecrs_peer_cert != NULL )
11045 {
11046 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11047 mbedtls_free( handshake->ecrs_peer_cert );
11048 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011049#endif
11050
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011051#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11052 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11053 mbedtls_pk_free( &handshake->peer_pubkey );
11054#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011056#if defined(MBEDTLS_SSL_PROTO_DTLS)
11057 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011058 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011059 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011060#endif
11061
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011062 mbedtls_platform_zeroize( handshake,
11063 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011064}
11065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011067{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011068 if( session == NULL )
11069 return;
11070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011072 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011073#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011074
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011075#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011077#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011078
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011079 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011080}
11081
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011082#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011083
11084#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11085#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11086#else
11087#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11088#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11089
11090#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11091#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11092#else
11093#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11094#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11095
11096#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11097#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11098#else
11099#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11100#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11101
11102#if defined(MBEDTLS_SSL_ALPN)
11103#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11104#else
11105#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11106#endif /* MBEDTLS_SSL_ALPN */
11107
11108#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11109#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11110#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11111#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11112
11113#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11114 ( (uint32_t) ( \
11115 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11116 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11117 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11118 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11119 0u ) )
11120
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011121static unsigned char ssl_serialized_context_header[] = {
11122 MBEDTLS_VERSION_MAJOR,
11123 MBEDTLS_VERSION_MINOR,
11124 MBEDTLS_VERSION_PATCH,
11125 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11126 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011127 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11128 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11129 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011130};
11131
Paul Bakker5121ce52009-01-03 21:22:43 +000011132/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011133 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011134 *
11135 * The format of the serialized data is:
11136 * (in the presentation language of TLS, RFC 8446 section 3)
11137 *
11138 * // header
11139 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011140 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011141 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011142 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011143 * Note: When updating the format, remember to keep these
11144 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011145 *
11146 * // session sub-structure
11147 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11148 * // transform sub-structure
11149 * uint8 random[64]; // ServerHello.random+ClientHello.random
11150 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11151 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11152 * // fields from ssl_context
11153 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11154 * uint64 in_window_top; // DTLS: last validated record seq_num
11155 * uint64 in_window; // DTLS: bitmask for replay protection
11156 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11157 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11158 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11159 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11160 *
11161 * Note that many fields of the ssl_context or sub-structures are not
11162 * serialized, as they fall in one of the following categories:
11163 *
11164 * 1. forced value (eg in_left must be 0)
11165 * 2. pointer to dynamically-allocated memory (eg session, transform)
11166 * 3. value can be re-derived from other data (eg session keys from MS)
11167 * 4. value was temporary (eg content of input buffer)
11168 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011169 */
11170int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11171 unsigned char *buf,
11172 size_t buf_len,
11173 size_t *olen )
11174{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011175 unsigned char *p = buf;
11176 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011177 size_t session_len;
11178 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011179
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011180 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011181 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11182 * this function's documentation.
11183 *
11184 * These are due to assumptions/limitations in the implementation. Some of
11185 * them are likely to stay (no handshake in progress) some might go away
11186 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011187 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011188 /* The initial handshake must be over */
11189 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011191 if( ssl->handshake != NULL )
11192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11193 /* Double-check that sub-structures are indeed ready */
11194 if( ssl->transform == NULL || ssl->session == NULL )
11195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11196 /* There must be no pending incoming or outgoing data */
11197 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11199 if( ssl->out_left != 0 )
11200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11201 /* Protocol must be DLTS, not TLS */
11202 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11204 /* Version must be 1.2 */
11205 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11207 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11208 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11209 /* We must be using an AEAD ciphersuite */
11210 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11212 /* Renegotiation must not be enabled */
11213 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011215
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011216 /*
11217 * Version and format identifier
11218 */
11219 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011220
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011221 if( used <= buf_len )
11222 {
11223 memcpy( p, ssl_serialized_context_header,
11224 sizeof( ssl_serialized_context_header ) );
11225 p += sizeof( ssl_serialized_context_header );
11226 }
11227
11228 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011229 * Session (length + data)
11230 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011231 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011232 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11233 return( ret );
11234
11235 used += 4 + session_len;
11236 if( used <= buf_len )
11237 {
11238 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11239 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11240 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11241 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11242
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011243 ret = ssl_session_save( ssl->session, 1,
11244 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011245 if( ret != 0 )
11246 return( ret );
11247
11248 p += session_len;
11249 }
11250
11251 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011252 * Transform
11253 */
11254 used += sizeof( ssl->transform->randbytes );
11255 if( used <= buf_len )
11256 {
11257 memcpy( p, ssl->transform->randbytes,
11258 sizeof( ssl->transform->randbytes ) );
11259 p += sizeof( ssl->transform->randbytes );
11260 }
11261
11262#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11263 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11264 if( used <= buf_len )
11265 {
11266 *p++ = ssl->transform->in_cid_len;
11267 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11268 p += ssl->transform->in_cid_len;
11269
11270 *p++ = ssl->transform->out_cid_len;
11271 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11272 p += ssl->transform->out_cid_len;
11273 }
11274#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11275
11276 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011277 * Saved fields from top-level ssl_context structure
11278 */
11279#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11280 used += 4;
11281 if( used <= buf_len )
11282 {
11283 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11284 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11285 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11286 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11287 }
11288#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11289
11290#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11291 used += 16;
11292 if( used <= buf_len )
11293 {
11294 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11295 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11296 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11297 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11298 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11299 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11300 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11301 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11302
11303 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11304 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11305 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11306 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11307 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11308 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11309 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11310 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11311 }
11312#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11313
11314#if defined(MBEDTLS_SSL_PROTO_DTLS)
11315 used += 1;
11316 if( used <= buf_len )
11317 {
11318 *p++ = ssl->disable_datagram_packing;
11319 }
11320#endif /* MBEDTLS_SSL_PROTO_DTLS */
11321
11322 used += 8;
11323 if( used <= buf_len )
11324 {
11325 memcpy( p, ssl->cur_out_ctr, 8 );
11326 p += 8;
11327 }
11328
11329#if defined(MBEDTLS_SSL_PROTO_DTLS)
11330 used += 2;
11331 if( used <= buf_len )
11332 {
11333 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11334 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11335 }
11336#endif /* MBEDTLS_SSL_PROTO_DTLS */
11337
11338#if defined(MBEDTLS_SSL_ALPN)
11339 {
11340 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011341 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011342 : 0;
11343
11344 used += 1 + alpn_len;
11345 if( used <= buf_len )
11346 {
11347 *p++ = alpn_len;
11348
11349 if( ssl->alpn_chosen != NULL )
11350 {
11351 memcpy( p, ssl->alpn_chosen, alpn_len );
11352 p += alpn_len;
11353 }
11354 }
11355 }
11356#endif /* MBEDTLS_SSL_ALPN */
11357
11358 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011359 * Done
11360 */
11361 *olen = used;
11362
11363 if( used > buf_len )
11364 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011365
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011366 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11367
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011368 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011369}
11370
11371/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011372 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011373 *
11374 * This internal version is wrapped by a public function that cleans up in
11375 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011376 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011377static int ssl_context_load( mbedtls_ssl_context *ssl,
11378 const unsigned char *buf,
11379 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011380{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011381 const unsigned char *p = buf;
11382 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011383 size_t session_len;
11384 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011385
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011386 /*
11387 * The context should have been freshly setup or reset.
11388 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011389 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011390 * renegotiating, or if the user mistakenly loaded a session first.)
11391 */
11392 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11393 ssl->session != NULL )
11394 {
11395 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11396 }
11397
11398 /*
11399 * We can't check that the config matches the initial one, but we can at
11400 * least check it matches the requirements for serializing.
11401 */
11402 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011403 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11404 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11405 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11406 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11407 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11408 MBEDTLS_SSL_MINOR_VERSION_3 ||
11409 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11410 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011411 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011412 {
11413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11414 }
11415
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011416 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11417
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011418 /*
11419 * Check version identifier
11420 */
11421 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11422 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11423
11424 if( memcmp( p, ssl_serialized_context_header,
11425 sizeof( ssl_serialized_context_header ) ) != 0 )
11426 {
11427 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11428 }
11429 p += sizeof( ssl_serialized_context_header );
11430
11431 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011432 * Session
11433 */
11434 if( (size_t)( end - p ) < 4 )
11435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11436
11437 session_len = ( (size_t) p[0] << 24 ) |
11438 ( (size_t) p[1] << 16 ) |
11439 ( (size_t) p[2] << 8 ) |
11440 ( (size_t) p[3] );
11441 p += 4;
11442
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011443 /* This has been allocated by ssl_handshake_init(), called by
11444 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11445 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011446 ssl->session_in = ssl->session;
11447 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011448 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011449
11450 if( (size_t)( end - p ) < session_len )
11451 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11452
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011453 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011454 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011455 {
11456 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011457 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011458 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011459
11460 p += session_len;
11461
11462 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011463 * Transform
11464 */
11465
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011466 /* This has been allocated by ssl_handshake_init(), called by
11467 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11468 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011469 ssl->transform_in = ssl->transform;
11470 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011471 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011472
11473 /* Read random bytes and populate structure */
11474 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11476
11477 ret = ssl_populate_transform( ssl->transform,
11478 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11479 ssl->session->master,
11480#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11481#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11482 ssl->session->encrypt_then_mac,
11483#endif
11484#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11485 ssl->session->trunc_hmac,
11486#endif
11487#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11488#if defined(MBEDTLS_ZLIB_SUPPORT)
11489 ssl->session->compression,
11490#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011491 p, /* currently pointing to randbytes */
11492 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11493 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11494 ssl );
11495 if( ret != 0 )
11496 return( ret );
11497
11498 p += sizeof( ssl->transform->randbytes );
11499
11500#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11501 /* Read connection IDs and store them */
11502 if( (size_t)( end - p ) < 1 )
11503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11504
11505 ssl->transform->in_cid_len = *p++;
11506
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011507 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11509
11510 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11511 p += ssl->transform->in_cid_len;
11512
11513 ssl->transform->out_cid_len = *p++;
11514
11515 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11517
11518 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11519 p += ssl->transform->out_cid_len;
11520#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11521
11522 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011523 * Saved fields from top-level ssl_context structure
11524 */
11525#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11526 if( (size_t)( end - p ) < 4 )
11527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11528
11529 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11530 ( (uint32_t) p[1] << 16 ) |
11531 ( (uint32_t) p[2] << 8 ) |
11532 ( (uint32_t) p[3] );
11533 p += 4;
11534#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11535
11536#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11537 if( (size_t)( end - p ) < 16 )
11538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11539
11540 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11541 ( (uint64_t) p[1] << 48 ) |
11542 ( (uint64_t) p[2] << 40 ) |
11543 ( (uint64_t) p[3] << 32 ) |
11544 ( (uint64_t) p[4] << 24 ) |
11545 ( (uint64_t) p[5] << 16 ) |
11546 ( (uint64_t) p[6] << 8 ) |
11547 ( (uint64_t) p[7] );
11548 p += 8;
11549
11550 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11551 ( (uint64_t) p[1] << 48 ) |
11552 ( (uint64_t) p[2] << 40 ) |
11553 ( (uint64_t) p[3] << 32 ) |
11554 ( (uint64_t) p[4] << 24 ) |
11555 ( (uint64_t) p[5] << 16 ) |
11556 ( (uint64_t) p[6] << 8 ) |
11557 ( (uint64_t) p[7] );
11558 p += 8;
11559#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11560
11561#if defined(MBEDTLS_SSL_PROTO_DTLS)
11562 if( (size_t)( end - p ) < 1 )
11563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11564
11565 ssl->disable_datagram_packing = *p++;
11566#endif /* MBEDTLS_SSL_PROTO_DTLS */
11567
11568 if( (size_t)( end - p ) < 8 )
11569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11570
11571 memcpy( ssl->cur_out_ctr, p, 8 );
11572 p += 8;
11573
11574#if defined(MBEDTLS_SSL_PROTO_DTLS)
11575 if( (size_t)( end - p ) < 2 )
11576 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11577
11578 ssl->mtu = ( p[0] << 8 ) | p[1];
11579 p += 2;
11580#endif /* MBEDTLS_SSL_PROTO_DTLS */
11581
11582#if defined(MBEDTLS_SSL_ALPN)
11583 {
11584 uint8_t alpn_len;
11585 const char **cur;
11586
11587 if( (size_t)( end - p ) < 1 )
11588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11589
11590 alpn_len = *p++;
11591
11592 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11593 {
11594 /* alpn_chosen should point to an item in the configured list */
11595 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11596 {
11597 if( strlen( *cur ) == alpn_len &&
11598 memcmp( p, cur, alpn_len ) == 0 )
11599 {
11600 ssl->alpn_chosen = *cur;
11601 break;
11602 }
11603 }
11604 }
11605
11606 /* can only happen on conf mismatch */
11607 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11609
11610 p += alpn_len;
11611 }
11612#endif /* MBEDTLS_SSL_ALPN */
11613
11614 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011615 * Forced fields from top-level ssl_context structure
11616 *
11617 * Most of them already set to the correct value by mbedtls_ssl_init() and
11618 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11619 */
11620 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11621
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011622#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011623 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011624#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11625#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011626 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011627#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011628
11629#if defined(MBEDTLS_SSL_PROTO_DTLS)
11630 ssl->in_epoch = 1;
11631#endif
11632
11633 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11634 * which we don't want - otherwise we'd end up freeing the wrong transform
11635 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11636 if( ssl->handshake != NULL )
11637 {
11638 mbedtls_ssl_handshake_free( ssl );
11639 mbedtls_free( ssl->handshake );
11640 ssl->handshake = NULL;
11641 }
11642
11643 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011644 * Done - should have consumed entire buffer
11645 */
11646 if( p != end )
11647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011648
11649 return( 0 );
11650}
11651
11652/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011653 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011654 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011655int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011656 const unsigned char *buf,
11657 size_t len )
11658{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011659 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011660
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011661 if( ret != 0 )
11662 mbedtls_ssl_free( context );
11663
11664 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011665}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011666#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011667
11668/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011669 * Free an SSL context
11670 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011671void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011672{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011673 if( ssl == NULL )
11674 return;
11675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011677
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011678 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011679 {
Angus Grattond8213d02016-05-25 20:56:48 +100011680 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011681 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011682 }
11683
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011684 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011685 {
Angus Grattond8213d02016-05-25 20:56:48 +100011686 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011687 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011688 }
11689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011690#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011691 if( ssl->compress_buf != NULL )
11692 {
Angus Grattond8213d02016-05-25 20:56:48 +100011693 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011694 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011695 }
11696#endif
11697
Paul Bakker48916f92012-09-16 19:57:18 +000011698 if( ssl->transform )
11699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011700 mbedtls_ssl_transform_free( ssl->transform );
11701 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011702 }
11703
11704 if( ssl->handshake )
11705 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011706 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011707 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11708 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011710 mbedtls_free( ssl->handshake );
11711 mbedtls_free( ssl->transform_negotiate );
11712 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011713 }
11714
Paul Bakkerc0463502013-02-14 11:19:38 +010011715 if( ssl->session )
11716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011717 mbedtls_ssl_session_free( ssl->session );
11718 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011719 }
11720
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011721#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011722 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011723 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011724 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011725 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011726 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011727#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011729#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11730 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11733 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011734 }
11735#endif
11736
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011737#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011738 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011739#endif
11740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011742
Paul Bakker86f04f42013-02-14 11:20:09 +010011743 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011744 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011745}
11746
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011747/*
11748 * Initialze mbedtls_ssl_config
11749 */
11750void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11751{
11752 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011753
11754#if !defined(MBEDTLS_SSL_PROTO_TLS)
11755 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11756#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011757}
11758
Simon Butcherc97b6972015-12-27 23:48:17 +000011759#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011760#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011761static int ssl_preset_default_hashes[] = {
11762#if defined(MBEDTLS_SHA512_C)
11763 MBEDTLS_MD_SHA512,
11764 MBEDTLS_MD_SHA384,
11765#endif
11766#if defined(MBEDTLS_SHA256_C)
11767 MBEDTLS_MD_SHA256,
11768 MBEDTLS_MD_SHA224,
11769#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011770#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011771 MBEDTLS_MD_SHA1,
11772#endif
11773 MBEDTLS_MD_NONE
11774};
Simon Butcherc97b6972015-12-27 23:48:17 +000011775#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011776#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011777
Hanno Becker73f4cb12019-06-27 13:51:07 +010011778#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011779static int ssl_preset_suiteb_ciphersuites[] = {
11780 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11781 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11782 0
11783};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011784#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011785
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011786#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011787#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011788static int ssl_preset_suiteb_hashes[] = {
11789 MBEDTLS_MD_SHA256,
11790 MBEDTLS_MD_SHA384,
11791 MBEDTLS_MD_NONE
11792};
11793#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011794#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011795
Hanno Beckerc1096e72019-06-19 12:30:41 +010011796#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011797static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011798#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011799 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011800#endif
11801#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011802 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011803#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011804 MBEDTLS_ECP_DP_NONE
11805};
11806#endif
11807
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011808/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011809 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011810 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011811int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011812 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011813{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011814#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011815 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011816#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011817
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011818 /* Use the functions here so that they are covered in tests,
11819 * but otherwise access member directly for efficiency */
11820 mbedtls_ssl_conf_endpoint( conf, endpoint );
11821 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011822
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011823 /*
11824 * Things that are common to all presets
11825 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011826#if defined(MBEDTLS_SSL_CLI_C)
11827 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11828 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011829#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011830 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011831#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011832#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11833 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11834#endif
11835 }
11836#endif
11837
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011838#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011839 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011840#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011841
11842#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11843 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11844#endif
11845
11846#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011847#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011848 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011849#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11850#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011851 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011852 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011853#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011854#endif
11855
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011856#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11857 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11858#endif
11859
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011860#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011861 conf->f_cookie_write = ssl_cookie_write_dummy;
11862 conf->f_cookie_check = ssl_cookie_check_dummy;
11863#endif
11864
Hanno Becker7f376f42019-06-12 16:20:48 +010011865#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11866 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011867 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11868#endif
11869
Janos Follath088ce432017-04-10 12:42:31 +010011870#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011871#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011872 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011873#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11874#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011875
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011877#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011878 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011879#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11880#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011881 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011882#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11883#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011884
11885#if defined(MBEDTLS_SSL_RENEGOTIATION)
11886 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011887 memset( conf->renego_period, 0x00, 2 );
11888 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011889#endif
11890
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011891#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11892 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11893 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011894 const unsigned char dhm_p[] =
11895 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11896 const unsigned char dhm_g[] =
11897 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11898
Hanno Beckera90658f2017-10-04 15:29:08 +010011899 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11900 dhm_p, sizeof( dhm_p ),
11901 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011902 {
11903 return( ret );
11904 }
11905 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011906#endif
11907
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011908 /*
11909 * Preset-specific defaults
11910 */
11911 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011912 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011913 /*
11914 * NSA Suite B
11915 */
11916 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011917#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011918 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011919#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11920#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011921 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011922#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11923#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011924 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011925#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11926#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011927 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011928#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011929
Hanno Becker73f4cb12019-06-27 13:51:07 +010011930#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011931 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11932 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11933 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11934 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11935 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011936#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011937
11938#if defined(MBEDTLS_X509_CRT_PARSE_C)
11939 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011940#endif
11941
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011942#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011943#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011944 conf->sig_hashes = ssl_preset_suiteb_hashes;
11945#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011946#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011947
11948#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011949#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011950 conf->curve_list = ssl_preset_suiteb_curves;
11951#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011952#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011953 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011954
11955 /*
11956 * Default
11957 */
11958 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011959#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011960 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11961 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11962 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11963 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011964#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11965#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011966 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11967 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11968 MBEDTLS_SSL_MIN_MINOR_VERSION :
11969 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011971 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011972 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11973#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011974#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11975#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11976 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11977#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11978#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11979 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11980#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011981
Hanno Becker73f4cb12019-06-27 13:51:07 +010011982#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011983 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11984 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11985 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11986 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11987 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010011988#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011989
11990#if defined(MBEDTLS_X509_CRT_PARSE_C)
11991 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11992#endif
11993
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011994#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011995#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011996 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011997#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011998#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011999
12000#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012001#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012002 conf->curve_list = mbedtls_ecp_grp_id_list();
12003#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012004#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012005
12006#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12007 conf->dhm_min_bitlen = 1024;
12008#endif
12009 }
12010
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012011 return( 0 );
12012}
12013
12014/*
12015 * Free mbedtls_ssl_config
12016 */
12017void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12018{
12019#if defined(MBEDTLS_DHM_C)
12020 mbedtls_mpi_free( &conf->dhm_P );
12021 mbedtls_mpi_free( &conf->dhm_G );
12022#endif
12023
12024#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12025 if( conf->psk != NULL )
12026 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012027 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012028 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012029 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012030 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012031 }
12032
12033 if( conf->psk_identity != NULL )
12034 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012035 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012036 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012037 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012038 conf->psk_identity_len = 0;
12039 }
12040#endif
12041
12042#if defined(MBEDTLS_X509_CRT_PARSE_C)
12043 ssl_key_cert_free( conf->key_cert );
12044#endif
12045
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012046 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012047}
12048
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012049#if defined(MBEDTLS_PK_C) && \
12050 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012051/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012052 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012054unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012056#if defined(MBEDTLS_RSA_C)
12057 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12058 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012060#if defined(MBEDTLS_ECDSA_C)
12061 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12062 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012064 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012065}
12066
Hanno Becker7e5437a2017-04-28 17:15:26 +010012067unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12068{
12069 switch( type ) {
12070 case MBEDTLS_PK_RSA:
12071 return( MBEDTLS_SSL_SIG_RSA );
12072 case MBEDTLS_PK_ECDSA:
12073 case MBEDTLS_PK_ECKEY:
12074 return( MBEDTLS_SSL_SIG_ECDSA );
12075 default:
12076 return( MBEDTLS_SSL_SIG_ANON );
12077 }
12078}
12079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012080mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012081{
12082 switch( sig )
12083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012084#if defined(MBEDTLS_RSA_C)
12085 case MBEDTLS_SSL_SIG_RSA:
12086 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012088#if defined(MBEDTLS_ECDSA_C)
12089 case MBEDTLS_SSL_SIG_ECDSA:
12090 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012091#endif
12092 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012093 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012094 }
12095}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012096#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012097
Hanno Becker7e5437a2017-04-28 17:15:26 +010012098#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12099 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12100
12101/* Find an entry in a signature-hash set matching a given hash algorithm. */
12102mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12103 mbedtls_pk_type_t sig_alg )
12104{
12105 switch( sig_alg )
12106 {
12107 case MBEDTLS_PK_RSA:
12108 return( set->rsa );
12109 case MBEDTLS_PK_ECDSA:
12110 return( set->ecdsa );
12111 default:
12112 return( MBEDTLS_MD_NONE );
12113 }
12114}
12115
12116/* Add a signature-hash-pair to a signature-hash set */
12117void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12118 mbedtls_pk_type_t sig_alg,
12119 mbedtls_md_type_t md_alg )
12120{
12121 switch( sig_alg )
12122 {
12123 case MBEDTLS_PK_RSA:
12124 if( set->rsa == MBEDTLS_MD_NONE )
12125 set->rsa = md_alg;
12126 break;
12127
12128 case MBEDTLS_PK_ECDSA:
12129 if( set->ecdsa == MBEDTLS_MD_NONE )
12130 set->ecdsa = md_alg;
12131 break;
12132
12133 default:
12134 break;
12135 }
12136}
12137
12138/* Allow exactly one hash algorithm for each signature. */
12139void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12140 mbedtls_md_type_t md_alg )
12141{
12142 set->rsa = md_alg;
12143 set->ecdsa = md_alg;
12144}
12145
12146#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12147 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12148
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012149/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012150 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012152mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012153{
12154 switch( hash )
12155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012156#if defined(MBEDTLS_MD5_C)
12157 case MBEDTLS_SSL_HASH_MD5:
12158 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012160#if defined(MBEDTLS_SHA1_C)
12161 case MBEDTLS_SSL_HASH_SHA1:
12162 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012164#if defined(MBEDTLS_SHA256_C)
12165 case MBEDTLS_SSL_HASH_SHA224:
12166 return( MBEDTLS_MD_SHA224 );
12167 case MBEDTLS_SSL_HASH_SHA256:
12168 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012170#if defined(MBEDTLS_SHA512_C)
12171 case MBEDTLS_SSL_HASH_SHA384:
12172 return( MBEDTLS_MD_SHA384 );
12173 case MBEDTLS_SSL_HASH_SHA512:
12174 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012175#endif
12176 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012177 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012178 }
12179}
12180
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012181/*
12182 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12183 */
12184unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12185{
12186 switch( md )
12187 {
12188#if defined(MBEDTLS_MD5_C)
12189 case MBEDTLS_MD_MD5:
12190 return( MBEDTLS_SSL_HASH_MD5 );
12191#endif
12192#if defined(MBEDTLS_SHA1_C)
12193 case MBEDTLS_MD_SHA1:
12194 return( MBEDTLS_SSL_HASH_SHA1 );
12195#endif
12196#if defined(MBEDTLS_SHA256_C)
12197 case MBEDTLS_MD_SHA224:
12198 return( MBEDTLS_SSL_HASH_SHA224 );
12199 case MBEDTLS_MD_SHA256:
12200 return( MBEDTLS_SSL_HASH_SHA256 );
12201#endif
12202#if defined(MBEDTLS_SHA512_C)
12203 case MBEDTLS_MD_SHA384:
12204 return( MBEDTLS_SSL_HASH_SHA384 );
12205 case MBEDTLS_MD_SHA512:
12206 return( MBEDTLS_SSL_HASH_SHA512 );
12207#endif
12208 default:
12209 return( MBEDTLS_SSL_HASH_NONE );
12210 }
12211}
12212
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012213#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012214/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012215 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012216 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012217 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012218int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012219{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012220 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12221 if( own_ec_id == grp_id )
12222 return( 0 );
12223 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012224
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012225 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012226}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012227#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012228
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012229#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012230/*
12231 * Check if a hash proposed by the peer is in our list.
12232 * Return 0 if we're willing to use it, -1 otherwise.
12233 */
12234int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12235 mbedtls_md_type_t md )
12236{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012237 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12238 if( md_alg == md )
12239 return( 0 );
12240 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012241
12242 return( -1 );
12243}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012244#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012246#if defined(MBEDTLS_X509_CRT_PARSE_C)
12247int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012248 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012249 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012250 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012251{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012252 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012253#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012254 int usage = 0;
12255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012256#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012257 const char *ext_oid;
12258 size_t ext_len;
12259#endif
12260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012261#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12262 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012263 ((void) cert);
12264 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012265 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012266#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012268#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12269 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012270 {
12271 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012272 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012274 case MBEDTLS_KEY_EXCHANGE_RSA:
12275 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012276 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012277 break;
12278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012279 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12280 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12281 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12282 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012283 break;
12284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012285 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12286 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012287 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012288 break;
12289
12290 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012291 case MBEDTLS_KEY_EXCHANGE_NONE:
12292 case MBEDTLS_KEY_EXCHANGE_PSK:
12293 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12294 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012295 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012296 usage = 0;
12297 }
12298 }
12299 else
12300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012301 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12302 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012303 }
12304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012305 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012306 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012307 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012308 ret = -1;
12309 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012310#else
12311 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012312#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012314#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12315 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012317 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12318 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012319 }
12320 else
12321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012322 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12323 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012324 }
12325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012326 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012327 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012328 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012329 ret = -1;
12330 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012331#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012332
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012333 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012335#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012336
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012337#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12338 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12339int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12340 unsigned char *output,
12341 unsigned char *data, size_t data_len )
12342{
12343 int ret = 0;
12344 mbedtls_md5_context mbedtls_md5;
12345 mbedtls_sha1_context mbedtls_sha1;
12346
12347 mbedtls_md5_init( &mbedtls_md5 );
12348 mbedtls_sha1_init( &mbedtls_sha1 );
12349
12350 /*
12351 * digitally-signed struct {
12352 * opaque md5_hash[16];
12353 * opaque sha_hash[20];
12354 * };
12355 *
12356 * md5_hash
12357 * MD5(ClientHello.random + ServerHello.random
12358 * + ServerParams);
12359 * sha_hash
12360 * SHA(ClientHello.random + ServerHello.random
12361 * + ServerParams);
12362 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012363 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012364 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012366 goto exit;
12367 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012368 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012369 ssl->handshake->randbytes, 64 ) ) != 0 )
12370 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012372 goto exit;
12373 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012374 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012375 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012377 goto exit;
12378 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012379 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012380 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012382 goto exit;
12383 }
12384
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012385 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012386 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012387 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012388 goto exit;
12389 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012390 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012391 ssl->handshake->randbytes, 64 ) ) != 0 )
12392 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012394 goto exit;
12395 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012396 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012397 data_len ) ) != 0 )
12398 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012400 goto exit;
12401 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012402 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012403 output + 16 ) ) != 0 )
12404 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012406 goto exit;
12407 }
12408
12409exit:
12410 mbedtls_md5_free( &mbedtls_md5 );
12411 mbedtls_sha1_free( &mbedtls_sha1 );
12412
12413 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012414 mbedtls_ssl_pend_fatal_alert( ssl,
12415 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012416
12417 return( ret );
12418
12419}
12420#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12421 MBEDTLS_SSL_PROTO_TLS1_1 */
12422
12423#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12424 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12425int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012426 unsigned char *hash, size_t *hashlen,
12427 unsigned char *data, size_t data_len,
12428 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012429{
12430 int ret = 0;
12431 mbedtls_md_context_t ctx;
12432 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012433 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012434
12435 mbedtls_md_init( &ctx );
12436
12437 /*
12438 * digitally-signed struct {
12439 * opaque client_random[32];
12440 * opaque server_random[32];
12441 * ServerDHParams params;
12442 * };
12443 */
12444 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12445 {
12446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12447 goto exit;
12448 }
12449 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12450 {
12451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12452 goto exit;
12453 }
12454 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12455 {
12456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12457 goto exit;
12458 }
12459 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12460 {
12461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12462 goto exit;
12463 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012464 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012465 {
12466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12467 goto exit;
12468 }
12469
12470exit:
12471 mbedtls_md_free( &ctx );
12472
12473 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012474 mbedtls_ssl_pend_fatal_alert( ssl,
12475 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012476
12477 return( ret );
12478}
12479#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12480 MBEDTLS_SSL_PROTO_TLS1_2 */
12481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012482#endif /* MBEDTLS_SSL_TLS_C */