blob: d4d51b5ef5c14822e16472bfcf86b107de253aeb [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)
Hanno Becker2793f742019-08-16 14:28:43 +0100592MBEDTLS_NO_INLINE static int ssl3_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200593 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)
Hanno Becker2793f742019-08-16 14:28:43 +0100653MBEDTLS_NO_INLINE static int tls1_prf( const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200654 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)
Hanno Beckerf6cc7422019-08-16 14:34:52 +0100751#if !( defined(MBEDTLS_SHA256_C) && defined(MBEDTLS_SHA512_C) )
752MBEDTLS_ALWAYS_INLINE static inline
753#else
754static
755#endif
756int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
760 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761{
762 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
766 const mbedtls_md_info_t *md_info;
767 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100768 int ret;
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
773 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100776
777 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000779
780 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100781 memcpy( tmp + md_len, label, nb );
782 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000783 nb += rlen;
784
785 /*
786 * Compute P_<hash>(secret, label + random)[0..dlen]
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100789 return( ret );
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
792 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
793 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100794
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_hmac_reset ( &md_ctx );
798 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
799 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_md_hmac_reset ( &md_ctx );
802 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
803 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000804
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100805 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806
807 for( j = 0; j < k; j++ )
808 dstbuf[i + j] = h_i[j];
809 }
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100812
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500813 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
814 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000815
816 return( 0 );
817}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100820MBEDTLS_NO_INLINE static int tls_prf_sha256(
821 const unsigned char *secret, size_t slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100822 const char *label,
823 const unsigned char *random, size_t rlen,
824 unsigned char *dstbuf, size_t dlen )
825{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100827 label, random, rlen, dstbuf, dlen ) );
828}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +0100832MBEDTLS_NO_INLINE static int tls_prf_sha384(
833 const unsigned char *secret, size_t slen,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200834 const char *label,
835 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000836 unsigned char *dstbuf, size_t dlen )
837{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100839 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000840}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#endif /* MBEDTLS_SHA512_C */
842#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100844/*
845 * Call the appropriate PRF function
846 */
Hanno Becker2793f742019-08-16 14:28:43 +0100847MBEDTLS_ALWAYS_INLINE static inline int ssl_prf( int minor_ver,
Hanno Becker39c7f7e2019-08-15 16:17:34 +0100848 mbedtls_md_type_t hash,
849 const unsigned char *secret, size_t slen,
850 const char *label,
851 const unsigned char *random, size_t rlen,
852 unsigned char *dstbuf, size_t dlen )
853{
854#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
855 (void) hash;
856#endif
857
858#if defined(MBEDTLS_SSL_PROTO_SSL3)
859 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
860 return( ssl3_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
864 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
865 return( tls1_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869#if defined(MBEDTLS_SHA512_C)
870 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
871 hash == MBEDTLS_MD_SHA384 )
872 {
873 return( tls_prf_sha384( secret, slen, label, random, rlen,
874 dstbuf, dlen ) );
875 }
876 else
877#endif
878#if defined(MBEDTLS_SHA256_C)
879 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
880 {
881 return( tls_prf_sha256( secret, slen, label, random, rlen,
882 dstbuf, dlen ) );
883 }
884#endif
885#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
886
887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
888}
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200889
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100890#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2793f742019-08-16 14:28:43 +0100891MBEDTLS_NO_INLINE static void ssl_calc_finished_ssl(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100892 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
893{
894 const char *sender;
895 mbedtls_md5_context md5;
896 mbedtls_sha1_context sha1;
897
898 unsigned char padbuf[48];
899 unsigned char md5sum[16];
900 unsigned char sha1sum[20];
901
902 mbedtls_ssl_session *session = ssl->session_negotiate;
903 if( !session )
904 session = ssl->session;
905
906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
907
908 mbedtls_md5_init( &md5 );
909 mbedtls_sha1_init( &sha1 );
910
911 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
912 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
913
914 /*
915 * SSLv3:
916 * hash =
917 * MD5( master + pad2 +
918 * MD5( handshake + sender + master + pad1 ) )
919 * + SHA1( master + pad2 +
920 * SHA1( handshake + sender + master + pad1 ) )
921 */
922
923#if !defined(MBEDTLS_MD5_ALT)
924 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
925 md5.state, sizeof( md5.state ) );
926#endif
927
928#if !defined(MBEDTLS_SHA1_ALT)
929 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
930 sha1.state, sizeof( sha1.state ) );
931#endif
932
933 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
934 : "SRVR";
935
936 memset( padbuf, 0x36, 48 );
937
938 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
939 mbedtls_md5_update_ret( &md5, session->master, 48 );
940 mbedtls_md5_update_ret( &md5, padbuf, 48 );
941 mbedtls_md5_finish_ret( &md5, md5sum );
942
943 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
944 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
945 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
946 mbedtls_sha1_finish_ret( &sha1, sha1sum );
947
948 memset( padbuf, 0x5C, 48 );
949
950 mbedtls_md5_starts_ret( &md5 );
951 mbedtls_md5_update_ret( &md5, session->master, 48 );
952 mbedtls_md5_update_ret( &md5, padbuf, 48 );
953 mbedtls_md5_update_ret( &md5, md5sum, 16 );
954 mbedtls_md5_finish_ret( &md5, buf );
955
956 mbedtls_sha1_starts_ret( &sha1 );
957 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
958 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
959 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
960 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
961
962 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
963
964 mbedtls_md5_free( &md5 );
965 mbedtls_sha1_free( &sha1 );
966
967 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
968 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
969 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
970
971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
972}
973#endif /* MBEDTLS_SSL_PROTO_SSL3 */
974
975#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker2793f742019-08-16 14:28:43 +0100976MBEDTLS_NO_INLINE static void ssl_calc_finished_tls(
Hanno Beckerfc7429e2019-08-16 10:12:21 +0100977 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
978{
979 int len = 12;
980 const char *sender;
981 mbedtls_md5_context md5;
982 mbedtls_sha1_context sha1;
983 unsigned char padbuf[36];
984
985 mbedtls_ssl_session *session = ssl->session_negotiate;
986 if( !session )
987 session = ssl->session;
988
989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
990
991 mbedtls_md5_init( &md5 );
992 mbedtls_sha1_init( &sha1 );
993
994 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
995 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
996
997 /*
998 * TLSv1:
999 * hash = PRF( master, finished_label,
1000 * MD5( handshake ) + SHA1( handshake ) )[0..11]
1001 */
1002
1003#if !defined(MBEDTLS_MD5_ALT)
1004 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
1005 md5.state, sizeof( md5.state ) );
1006#endif
1007
1008#if !defined(MBEDTLS_SHA1_ALT)
1009 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
1010 sha1.state, sizeof( sha1.state ) );
1011#endif
1012
1013 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1014 ? "client finished"
1015 : "server finished";
1016
1017 mbedtls_md5_finish_ret( &md5, padbuf );
1018 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
1019
1020 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1021 mbedtls_ssl_suite_get_mac(
1022 mbedtls_ssl_ciphersuite_from_id(
1023 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1024 session->master, 48, sender,
1025 padbuf, 36, buf, len );
1026
1027 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1028
1029 mbedtls_md5_free( &md5 );
1030 mbedtls_sha1_free( &sha1 );
1031
1032 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1033
1034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1035}
1036#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1037
1038#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1039#if defined(MBEDTLS_SHA256_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001040MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha256(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001041 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1042{
1043 int len = 12;
1044 const char *sender;
1045 mbedtls_sha256_context sha256;
1046 unsigned char padbuf[32];
1047
1048 mbedtls_ssl_session *session = ssl->session_negotiate;
1049 if( !session )
1050 session = ssl->session;
1051
1052 mbedtls_sha256_init( &sha256 );
1053
1054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
1055
1056 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1057
1058 /*
1059 * TLSv1.2:
1060 * hash = PRF( master, finished_label,
1061 * Hash( handshake ) )[0.11]
1062 */
1063
1064#if !defined(MBEDTLS_SHA256_ALT)
1065 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
1066 sha256.state, sizeof( sha256.state ) );
1067#endif
1068
1069 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1070 ? "client finished"
1071 : "server finished";
1072
1073 mbedtls_sha256_finish_ret( &sha256, padbuf );
1074
1075 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1076 mbedtls_ssl_suite_get_mac(
1077 mbedtls_ssl_ciphersuite_from_id(
1078 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1079 session->master, 48, sender,
1080 padbuf, 32, buf, len );
1081
1082 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1083
1084 mbedtls_sha256_free( &sha256 );
1085
1086 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1087
1088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1089}
1090#endif /* MBEDTLS_SHA256_C */
1091
1092#if defined(MBEDTLS_SHA512_C)
Hanno Becker2793f742019-08-16 14:28:43 +01001093MBEDTLS_NO_INLINE static void ssl_calc_finished_tls_sha384(
Hanno Beckerfc7429e2019-08-16 10:12:21 +01001094 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
1095{
1096 int len = 12;
1097 const char *sender;
1098 mbedtls_sha512_context sha512;
1099 unsigned char padbuf[48];
1100
1101 mbedtls_ssl_session *session = ssl->session_negotiate;
1102 if( !session )
1103 session = ssl->session;
1104
1105 mbedtls_sha512_init( &sha512 );
1106
1107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
1108
1109 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1110
1111 /*
1112 * TLSv1.2:
1113 * hash = PRF( master, finished_label,
1114 * Hash( handshake ) )[0.11]
1115 */
1116
1117#if !defined(MBEDTLS_SHA512_ALT)
1118 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
1119 sha512.state, sizeof( sha512.state ) );
1120#endif
1121
1122 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
1123 ? "client finished"
1124 : "server finished";
1125
1126 mbedtls_sha512_finish_ret( &sha512, padbuf );
1127
1128 ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1129 mbedtls_ssl_suite_get_mac(
1130 mbedtls_ssl_ciphersuite_from_id(
1131 mbedtls_ssl_session_get_ciphersuite( session ) ) ),
1132 session->master, 48, sender,
1133 padbuf, 48, buf, len );
1134
1135 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
1136
1137 mbedtls_sha512_free( &sha512 );
1138
1139 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
1140
1141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
1142}
1143#endif /* MBEDTLS_SHA512_C */
1144#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1145
Hanno Becker2793f742019-08-16 14:28:43 +01001146MBEDTLS_ALWAYS_INLINE static inline int ssl_calc_finished(
1147 int minor_ver,
Hanno Beckerc2fb7592019-08-15 16:31:23 +01001148 mbedtls_md_type_t hash,
1149 mbedtls_ssl_context *ssl,
1150 unsigned char *buf,
1151 int from )
1152{
1153#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1154 (void) hash;
1155#endif
1156
1157#if defined(MBEDTLS_SSL_PROTO_SSL3)
1158 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
1159 ssl_calc_finished_ssl( ssl, buf, from );
1160 else
1161#endif
1162#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1163 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
1164 ssl_calc_finished_tls( ssl, buf, from );
1165 else
1166#endif
1167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1168#if defined(MBEDTLS_SHA512_C)
1169 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1170 hash == MBEDTLS_MD_SHA384 )
1171 {
1172 ssl_calc_finished_tls_sha384( ssl, buf, from );
1173 }
1174 else
1175#endif
1176#if defined(MBEDTLS_SHA256_C)
1177 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
1178 ssl_calc_finished_tls_sha256( ssl, buf, from );
1179 else
1180#endif
1181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1183
1184 return( 0 );
1185}
1186
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001187/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001188 * Populate a transform structure with session keys and all the other
1189 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001190 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001191 * Parameters:
1192 * - [in/out]: transform: structure to populate
1193 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001194 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001195 * - [in] ciphersuite
1196 * - [in] master
1197 * - [in] encrypt_then_mac
1198 * - [in] trunc_hmac
1199 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001200 * - [in] tls_prf: pointer to PRF to use for key derivation
1201 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001202 * - [in] minor_ver: SSL/TLS minor version
1203 * - [in] endpoint: client or server
1204 * - [in] ssl: optionally used for:
1205 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1206 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1207 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001208 */
Hanno Becker298a4702019-08-16 10:21:32 +01001209/* Force compilers to inline this function if it's used only
1210 * from one place, because at least ARMC5 doesn't do that
1211 * automatically. */
1212#if !defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1213MBEDTLS_ALWAYS_INLINE static inline
1214#else
1215static
1216#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1217int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001218 int ciphersuite,
1219 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001220#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1222 int encrypt_then_mac,
1223#endif
1224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1225 int trunc_hmac,
1226#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001227#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001228#if defined(MBEDTLS_ZLIB_SUPPORT)
1229 int compression,
1230#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001231 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001232 int minor_ver,
1233 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001234 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001236 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 unsigned char keyblk[256];
1238 unsigned char *key1;
1239 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 unsigned char *mac_enc;
1241 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001242 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001243 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001244 unsigned keylen;
Hanno Becker473f98f2019-06-26 10:27:32 +01001245 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 const mbedtls_cipher_info_t *cipher_info;
1247 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001248
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001249#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1250 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1251 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001252 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001253 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +00001254#endif
Hanno Becker3307b532017-12-27 21:37:21 +00001255
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001256 /*
1257 * Some data just needs copying into the structure
1258 */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001259#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1260 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001261 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262#endif
Hanno Becker0a92b812019-06-24 15:46:40 +01001263
1264#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001265 transform->minor_ver = minor_ver;
Hanno Becker0a92b812019-06-24 15:46:40 +01001266#else
1267 ((void) minor_ver);
1268#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Manuel Pégourié-Gonnarda3024ee2019-07-09 12:54:17 +02001270#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1271 memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
1272#endif
1273
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001274 /*
1275 * Get various info structures
1276 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001277 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker473f98f2019-06-26 10:27:32 +01001278 if( ciphersuite_info == MBEDTLS_SSL_CIPHERSUITE_INVALID_HANDLE )
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001279 {
1280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001281 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1283 }
1284
Hanno Becker473f98f2019-06-26 10:27:32 +01001285 cipher_info = mbedtls_cipher_info_from_type(
1286 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001287 if( cipher_info == NULL )
1288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001290 mbedtls_ssl_suite_get_cipher( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001292 }
1293
Hanno Becker473f98f2019-06-26 10:27:32 +01001294 md_info = mbedtls_md_info_from_type(
1295 mbedtls_ssl_suite_get_mac( ciphersuite_info ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001296 if( md_info == NULL )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker473f98f2019-06-26 10:27:32 +01001299 mbedtls_ssl_suite_get_mac( ciphersuite_info ) ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001301 }
1302
Hanno Beckera5a2b082019-05-15 14:03:01 +01001303#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001304 /* Copy own and peer's CID if the use of the CID
1305 * extension has been negotiated. */
1306 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1307 {
1308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +01001309
Hanno Becker4932f9f2019-05-03 15:23:51 +01001310 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +01001311 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +01001312 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001313 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +01001314
1315 transform->out_cid_len = ssl->handshake->peer_cid_len;
1316 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1317 ssl->handshake->peer_cid_len );
1318 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1319 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001320 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001321#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +01001322
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001324 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +00001325 */
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001326 ret = ssl_prf( minor_ver,
1327 mbedtls_ssl_suite_get_mac( ciphersuite_info ),
1328 master, 48, "key expansion", randbytes, 64,
1329 keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001330 if( ret != 0 )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001333 return( ret );
1334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001337 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001338 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001339 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 /*
1343 * Determine the appropriate key, IV and MAC length.
1344 */
Paul Bakker68884e32013-01-07 18:20:04 +01001345
Hanno Beckere7f2df02017-12-27 08:17:40 +00001346 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001347
Hanno Beckerf1229442018-01-03 15:32:31 +00001348#if defined(MBEDTLS_GCM_C) || \
1349 defined(MBEDTLS_CCM_C) || \
1350 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001352 cipher_info->mode == MBEDTLS_MODE_CCM ||
1353 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001355 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001356 mac_key_len = 0;
Hanno Becker473f98f2019-06-26 10:27:32 +01001357 transform->taglen = mbedtls_ssl_suite_get_flags( ciphersuite_info ) &
1358 MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001359
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001360 /* All modes haves 96-bit IVs;
1361 * GCM and CCM has 4 implicit and 8 explicit bytes
1362 * ChachaPoly has all 12 bytes implicit
1363 */
Paul Bakker68884e32013-01-07 18:20:04 +01001364 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001365 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1366 transform->fixed_ivlen = 12;
1367 else
1368 transform->fixed_ivlen = 4;
Paul Bakker68884e32013-01-07 18:20:04 +01001369 }
1370 else
Hanno Beckerf1229442018-01-03 15:32:31 +00001371#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1372#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1373 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1374 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001375 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001376 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1378 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001381 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001383
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001384 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001385 mac_key_len = mbedtls_md_get_size( md_info );
1386 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001389 /*
1390 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1391 * (rfc 6066 page 13 or rfc 2104 section 4),
1392 * so we only need to adjust the length here.
1393 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001394 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001397
1398#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1399 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001400 * HMAC implementation which also truncates the key
1401 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001402 mac_key_len = transform->maclen;
1403#endif
1404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001406
1407 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001408 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001410 else
1411#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1412 {
1413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1415 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Hanno Beckera9d5c452019-07-25 16:47:12 +01001417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, ivlen: %u, maclen: %u",
Hanno Beckere7f2df02017-12-27 08:17:40 +00001418 (unsigned) keylen,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001419 (unsigned) transform->ivlen,
1420 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 /*
1423 * Finally setup the cipher contexts, IVs and MAC secrets.
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001426 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001428 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001429 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
Paul Bakker68884e32013-01-07 18:20:04 +01001431 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001432 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001434 /*
1435 * This is not used in TLS v1.1.
1436 */
Paul Bakker48916f92012-09-16 19:57:18 +00001437 iv_copy_len = ( transform->fixed_ivlen ) ?
1438 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001439 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1440 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001441 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 }
1443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_CLI_C */
1445#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001446 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001448 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001449 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Hanno Becker81c7b182017-11-09 18:39:33 +00001451 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001452 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001454 /*
1455 * This is not used in TLS v1.1.
1456 */
Paul Bakker48916f92012-09-16 19:57:18 +00001457 iv_copy_len = ( transform->fixed_ivlen ) ?
1458 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001459 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1460 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001469
Hanno Becker92231322018-01-03 15:32:51 +00001470#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001472 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001473 {
Hanno Becker92231322018-01-03 15:32:51 +00001474 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001478 }
1479
Hanno Becker81c7b182017-11-09 18:39:33 +00001480 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1481 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001482 }
1483 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1485#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1486 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001487 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001488 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001489 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1490 For AEAD-based ciphersuites, there is nothing to do here. */
1491 if( mac_key_len != 0 )
1492 {
1493 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1494 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1495 }
Paul Bakker68884e32013-01-07 18:20:04 +01001496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001497 else
1498#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001502 }
Hanno Becker92231322018-01-03 15:32:51 +00001503#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1506 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001507 {
1508 int ret = 0;
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001511
Hanno Beckere7f2df02017-12-27 08:17:40 +00001512 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001513 transform->iv_enc, transform->iv_dec,
1514 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001515 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001516 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1519 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001520 }
1521 }
Hanno Becker92231322018-01-03 15:32:51 +00001522#else
1523 ((void) mac_dec);
1524 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001526
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001527#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1528 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001529 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001530 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001531 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001532 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001533 iv_copy_len );
1534 }
1535#endif
1536
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001537 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001538 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 return( ret );
1542 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001543
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001544 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 cipher_info ) ) != 0 )
1546 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 return( ret );
1549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001552 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 return( ret );
1557 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001560 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 return( ret );
1565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_CIPHER_MODE_CBC)
1568 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1571 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 return( ret );
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001586 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001587
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001588 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001590 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593
Paul Bakker48916f92012-09-16 19:57:18 +00001594 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1595 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001597 if( deflateInit( &transform->ctx_deflate,
1598 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001599 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1602 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603 }
1604 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 return( 0 );
1608}
1609
Hanno Beckercf87c5e2019-08-16 10:11:21 +01001610#if defined(MBEDTLS_SSL_PROTO_SSL3)
1611static inline void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1612 unsigned char hash[36],
1613 size_t *hlen )
1614{
1615 mbedtls_md5_context md5;
1616 mbedtls_sha1_context sha1;
1617 unsigned char pad_1[48];
1618 unsigned char pad_2[48];
1619
1620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
1621
1622 mbedtls_md5_init( &md5 );
1623 mbedtls_sha1_init( &sha1 );
1624
1625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1627
1628 memset( pad_1, 0x36, 48 );
1629 memset( pad_2, 0x5C, 48 );
1630
1631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1633 mbedtls_md5_finish_ret( &md5, hash );
1634
1635 mbedtls_md5_starts_ret( &md5 );
1636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1638 mbedtls_md5_update_ret( &md5, hash, 16 );
1639 mbedtls_md5_finish_ret( &md5, hash );
1640
1641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1644
1645 mbedtls_sha1_starts_ret( &sha1 );
1646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1648 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1650
1651 *hlen = 36;
1652
1653 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1655
1656 mbedtls_md5_free( &md5 );
1657 mbedtls_sha1_free( &sha1 );
1658
1659 return;
1660}
1661#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1662
1663#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1664static inline void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1665 unsigned char hash[36],
1666 size_t *hlen )
1667{
1668 mbedtls_md5_context md5;
1669 mbedtls_sha1_context sha1;
1670
1671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
1672
1673 mbedtls_md5_init( &md5 );
1674 mbedtls_sha1_init( &sha1 );
1675
1676 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1677 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
1678
1679 mbedtls_md5_finish_ret( &md5, hash );
1680 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
1681
1682 *hlen = 36;
1683
1684 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1686
1687 mbedtls_md5_free( &md5 );
1688 mbedtls_sha1_free( &sha1 );
1689
1690 return;
1691}
1692#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1693
1694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1695#if defined(MBEDTLS_SHA256_C)
1696static inline void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1697 unsigned char hash[32],
1698 size_t *hlen )
1699{
1700 mbedtls_sha256_context sha256;
1701
1702 mbedtls_sha256_init( &sha256 );
1703
1704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
1705
1706 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
1707 mbedtls_sha256_finish_ret( &sha256, hash );
1708
1709 *hlen = 32;
1710
1711 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1713
1714 mbedtls_sha256_free( &sha256 );
1715
1716 return;
1717}
1718#endif /* MBEDTLS_SHA256_C */
1719
1720#if defined(MBEDTLS_SHA512_C)
1721static inline void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1722 unsigned char hash[48],
1723 size_t *hlen )
1724{
1725 mbedtls_sha512_context sha512;
1726
1727 mbedtls_sha512_init( &sha512 );
1728
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1730
1731 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
1732 mbedtls_sha512_finish_ret( &sha512, hash );
1733
1734 *hlen = 48;
1735
1736 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1738
1739 mbedtls_sha512_free( &sha512 );
1740
1741 return;
1742}
1743#endif /* MBEDTLS_SHA512_C */
1744#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1745
Hanno Becker2f41b242019-08-15 17:29:43 +01001746int mbedtls_ssl_calc_verify( int minor_ver,
1747 mbedtls_md_type_t hash,
1748 mbedtls_ssl_context const *ssl,
1749 unsigned char *dst,
1750 size_t *hlen )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001751{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001752#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1753 (void) hash;
1754#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001755
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001756#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001757 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001758 ssl_calc_verify_ssl( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001759 else
1760#endif
1761#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001762 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2f41b242019-08-15 17:29:43 +01001763 ssl_calc_verify_tls( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001764 else
1765#endif
1766#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1767#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001768 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1769 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001770 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001771 ssl_calc_verify_tls_sha384( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001772 }
1773 else
1774#endif
1775#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001776 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001777 {
Hanno Becker2f41b242019-08-15 17:29:43 +01001778 ssl_calc_verify_tls_sha256( ssl, dst, hlen );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001779 }
1780 else
1781#endif
1782#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1783 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1785 }
1786
1787 return( 0 );
1788}
1789
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001790/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001791 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001792 *
1793 * Parameters:
1794 * [in/out] handshake
1795 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1796 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001797 * [out] master
1798 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001799 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001800static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001801 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001802 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001803{
1804 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001805
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001806/* #if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) */
1807/* ssl = NULL; /\* make sure we don't use it except for debug and EMS *\/ */
1808/* (void) ssl; */
1809/* #endif */
1810
1811 mbedtls_ssl_ciphersuite_handle_t const ciphersuite =
1812 mbedtls_ssl_handshake_get_ciphersuite( handshake );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001813
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001814#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001815 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001816 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001817 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1818 return( 0 );
1819 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001820#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001821
1822 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1823 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001824
1825#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001826 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1827 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001828 {
1829 unsigned char session_hash[48];
1830 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001831
Hanno Becker2f41b242019-08-15 17:29:43 +01001832 mbedtls_ssl_calc_verify(
1833 mbedtls_ssl_get_minor_ver( ssl ),
1834 mbedtls_ssl_suite_get_mac( ciphersuite ),
1835 ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001836
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001837 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1838 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001839
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001840 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1841 mbedtls_ssl_suite_get_mac( ciphersuite ),
1842 handshake->premaster, handshake->pmslen,
1843 "extended master secret",
1844 session_hash, hash_len,
1845 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001846 }
1847 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001848#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001849 {
Hanno Becker39c7f7e2019-08-15 16:17:34 +01001850 ret = ssl_prf( mbedtls_ssl_get_minor_ver( ssl ),
1851 mbedtls_ssl_suite_get_mac( ciphersuite ),
1852 handshake->premaster, handshake->pmslen,
1853 "master secret",
1854 handshake->randbytes, 64,
1855 master, 48 );
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001856 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001857 if( ret != 0 )
1858 {
1859 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1860 return( ret );
1861 }
1862
1863 mbedtls_platform_zeroize( handshake->premaster,
1864 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001865
1866 return( 0 );
1867}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001868
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001869int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1870{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001871 int ret;
1872
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1874
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001875 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001876 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001877 ssl->session_negotiate->master,
1878 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001879 if( ret != 0 )
1880 {
1881 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1882 return( ret );
1883 }
1884
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001885 /* Swap the client and server random values:
1886 * - MS derivation wanted client+server (RFC 5246 8.1)
1887 * - key derivation wants server+client (RFC 5246 6.3) */
1888 {
1889 unsigned char tmp[64];
1890 memcpy( tmp, ssl->handshake->randbytes, 64 );
1891 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1892 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1893 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1894 }
1895
1896 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001897 ret = ssl_populate_transform( ssl->transform_negotiate,
Hanno Beckere02758c2019-06-26 15:31:31 +01001898 mbedtls_ssl_session_get_ciphersuite( ssl->session_negotiate ),
1899 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001900#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001901#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001902 ssl->session_negotiate->encrypt_then_mac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001903#endif
1904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckere02758c2019-06-26 15:31:31 +01001905 ssl->session_negotiate->trunc_hmac,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001906#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001907#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001908#if defined(MBEDTLS_ZLIB_SUPPORT)
Hanno Beckere02758c2019-06-26 15:31:31 +01001909 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001910#endif
Hanno Beckere02758c2019-06-26 15:31:31 +01001911 ssl->handshake->randbytes,
Hanno Becker2881d802019-05-22 14:44:53 +01001912 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Beckere02758c2019-06-26 15:31:31 +01001913 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
1914 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001915 if( ret != 0 )
1916 {
1917 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1918 return( ret );
1919 }
1920
1921 /* We no longer need Server/ClientHello.random values */
1922 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1923 sizeof( ssl->handshake->randbytes ) );
1924
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001925 /* Allocate compression buffer */
1926#if defined(MBEDTLS_ZLIB_SUPPORT)
1927 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1928 ssl->compress_buf == NULL )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1931 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1932 if( ssl->compress_buf == NULL )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001935 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001936 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1937 }
1938 }
1939#endif
1940
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1942
1943 return( 0 );
1944}
1945
Hanno Becker09d23642019-07-22 17:18:18 +01001946int mbedtls_ssl_build_pms( mbedtls_ssl_context *ssl )
1947{
1948 int ret;
1949
1950 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
1951 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
1952
Hanno Beckera3c2c172019-07-23 16:51:57 +01001953#if defined(MBEDTLS_USE_TINYCRYPT)
1954 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1955 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
1956 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1957 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA )
1958 {
1959 const struct uECC_Curve_t * uecc_curve = uECC_secp256r1();
Hanno Becker7a196332019-07-24 11:12:41 +01001960 ((void) ret);
Hanno Beckera3c2c172019-07-23 16:51:57 +01001961
1962 if( !uECC_shared_secret( ssl->handshake->ecdh_peerkey,
1963 ssl->handshake->ecdh_privkey,
1964 ssl->handshake->premaster,
1965 uecc_curve ) )
1966 {
1967 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1968 }
1969
1970 ssl->handshake->pmslen = NUM_ECC_BYTES;
1971 }
1972 else
1973#endif
Hanno Becker09d23642019-07-22 17:18:18 +01001974#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
1975 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1976 == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
1977 {
1978 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
1979 ssl->handshake->premaster,
1980 MBEDTLS_PREMASTER_SIZE,
1981 &ssl->handshake->pmslen,
1982 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01001983 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01001984 {
1985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
1986 return( ret );
1987 }
1988
1989 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1990 }
1991 else
1992#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
Hanno Becker29d16552019-07-24 11:11:45 +01001993#if defined(MBEDTLS_ECDH_C) && \
1994 ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1995 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
1996 defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1997 defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) )
Hanno Becker09d23642019-07-22 17:18:18 +01001998 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
1999 == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
2000 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2001 == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
2002 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2003 == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
2004 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2005 == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
2006 {
2007 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2008 &ssl->handshake->pmslen,
2009 ssl->handshake->premaster,
2010 MBEDTLS_MPI_MAX_SIZE,
2011 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002012 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002013 {
2014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
2015 return( ret );
2016 }
2017
2018 MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2019 }
2020 else
2021#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2022 MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2023 MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2024 MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2025#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2026 if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
2027 {
2028 if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
Manuel Pégourié-Gonnardfb02e962019-08-01 10:48:49 +02002029 mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ) ) != 0 )
Hanno Becker09d23642019-07-22 17:18:18 +01002030 {
2031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
2032 return( ret );
2033 }
2034 }
2035 else
2036#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
2037#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
2038 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
2039 MBEDTLS_KEY_EXCHANGE_ECJPAKE )
2040 {
2041 ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
2042 ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
2043 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002044 mbedtls_ssl_conf_get_prng( ssl->conf ) );
Hanno Becker09d23642019-07-22 17:18:18 +01002045 if( ret != 0 )
2046 {
2047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
2048 return( ret );
2049 }
2050 }
2051 else
2052#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2053#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
2054 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info )
2055 == MBEDTLS_KEY_EXCHANGE_RSA )
2056 {
2057 ((void) ret);
Manuel Pégourié-Gonnard8793fab2019-08-01 10:44:07 +02002058 /* The premaster secret has already been set by
Hanno Becker09d23642019-07-22 17:18:18 +01002059 * ssl_rsa_generate_partial_pms(). Only the
2060 * PMS length needs to be set. */
2061 ssl->handshake->pmslen = 48;
2062 }
2063 else
2064#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
2065 {
2066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2068 }
2069
2070 return( 0 );
2071}
2072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2074int 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 +02002075{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002076 unsigned char *p = ssl->handshake->premaster;
2077 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002078 const unsigned char *psk = ssl->conf->psk;
2079 size_t psk_len = ssl->conf->psk_len;
2080
2081 /* If the psk callback was called, use its result */
2082 if( ssl->handshake->psk != NULL )
2083 {
2084 psk = ssl->handshake->psk;
2085 psk_len = ssl->handshake->psk_len;
2086 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002087
2088 /*
2089 * PMS = struct {
2090 * opaque other_secret<0..2^16-1>;
2091 * opaque psk<0..2^16-1>;
2092 * };
2093 * with "other_secret" depending on the particular key exchange
2094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2096 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002097 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002098 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002100
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002101 *(p++) = (unsigned char)( psk_len >> 8 );
2102 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002103
2104 if( end < p || (size_t)( end - p ) < psk_len )
2105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2106
2107 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002108 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002109 }
2110 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2112#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2113 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002114 {
2115 /*
2116 * other_secret already set by the ClientKeyExchange message,
2117 * and is 48 bytes long
2118 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002119 if( end - p < 2 )
2120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2121
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002122 *p++ = 0;
2123 *p++ = 48;
2124 p += 48;
2125 }
2126 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2128#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2129 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002130 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002131 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002132 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002133
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002134 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002136 p + 2, end - ( p + 2 ), &len,
Hanno Beckerece325c2019-06-13 15:39:27 +01002137 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002138 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002141 return( ret );
2142 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002143 *(p++) = (unsigned char)( len >> 8 );
2144 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002145 p += len;
2146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002148 }
2149 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2151#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2152 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002153 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002154 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002155 size_t zlen;
2156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002158 p + 2, end - ( p + 2 ),
Hanno Beckerece325c2019-06-13 15:39:27 +01002159 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01002160 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002163 return( ret );
2164 }
2165
2166 *(p++) = (unsigned char)( zlen >> 8 );
2167 *(p++) = (unsigned char)( zlen );
2168 p += zlen;
2169
Janos Follath3fbdada2018-08-15 10:26:53 +01002170 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2171 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002172 }
2173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002178 }
2179
2180 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002181 if( end - p < 2 )
2182 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002183
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002184 *(p++) = (unsigned char)( psk_len >> 8 );
2185 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002186
2187 if( end < p || (size_t)( end - p ) < psk_len )
2188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2189
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002190 memcpy( p, psk, psk_len );
2191 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002192
2193 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2194
2195 return( 0 );
2196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002200/*
2201 * SSLv3.0 MAC functions
2202 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002203#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002204static void ssl_mac( mbedtls_md_context_t *md_ctx,
2205 const unsigned char *secret,
2206 const unsigned char *buf, size_t len,
2207 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002208 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002209{
2210 unsigned char header[11];
2211 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002212 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2214 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002215
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002216 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002218 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002219 else
Paul Bakker68884e32013-01-07 18:20:04 +01002220 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002221
2222 memcpy( header, ctr, 8 );
2223 header[ 8] = (unsigned char) type;
2224 header[ 9] = (unsigned char)( len >> 8 );
2225 header[10] = (unsigned char)( len );
2226
Paul Bakker68884e32013-01-07 18:20:04 +01002227 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 mbedtls_md_starts( md_ctx );
2229 mbedtls_md_update( md_ctx, secret, md_size );
2230 mbedtls_md_update( md_ctx, padding, padlen );
2231 mbedtls_md_update( md_ctx, header, 11 );
2232 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002233 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234
Paul Bakker68884e32013-01-07 18:20:04 +01002235 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236 mbedtls_md_starts( md_ctx );
2237 mbedtls_md_update( md_ctx, secret, md_size );
2238 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002239 mbedtls_md_update( md_ctx, out, md_size );
2240 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002243
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002244/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01002245 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002246#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002247 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2248 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2249 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2250/* This function makes sure every byte in the memory region is accessed
2251 * (in ascending addresses order) */
2252static void ssl_read_memory( unsigned char *p, size_t len )
2253{
2254 unsigned char acc = 0;
2255 volatile unsigned char force;
2256
2257 for( ; len != 0; p++, len-- )
2258 acc ^= *p;
2259
2260 force = acc;
2261 (void) force;
2262}
2263#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2264
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002265/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002267 */
Hanno Becker3307b532017-12-27 21:37:21 +00002268
Hanno Beckera5a2b082019-05-15 14:03:01 +01002269#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01002270/* This functions transforms a DTLS plaintext fragment and a record content
2271 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01002272 *
2273 * struct {
2274 * opaque content[DTLSPlaintext.length];
2275 * ContentType real_type;
2276 * uint8 zeros[length_of_padding];
2277 * } DTLSInnerPlaintext;
2278 *
2279 * Input:
2280 * - `content`: The beginning of the buffer holding the
2281 * plaintext to be wrapped.
2282 * - `*content_size`: The length of the plaintext in Bytes.
2283 * - `max_len`: The number of Bytes available starting from
2284 * `content`. This must be `>= *content_size`.
2285 * - `rec_type`: The desired record content type.
2286 *
2287 * Output:
2288 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2289 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2290 *
2291 * Returns:
2292 * - `0` on success.
2293 * - A negative error code if `max_len` didn't offer enough space
2294 * for the expansion.
2295 */
2296static int ssl_cid_build_inner_plaintext( unsigned char *content,
2297 size_t *content_size,
2298 size_t remaining,
2299 uint8_t rec_type )
2300{
2301 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01002302 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2303 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2304 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01002305
2306 /* Write real content type */
2307 if( remaining == 0 )
2308 return( -1 );
2309 content[ len ] = rec_type;
2310 len++;
2311 remaining--;
2312
2313 if( remaining < pad )
2314 return( -1 );
2315 memset( content + len, 0, pad );
2316 len += pad;
2317 remaining -= pad;
2318
2319 *content_size = len;
2320 return( 0 );
2321}
2322
Hanno Becker7dc25772019-05-20 15:08:01 +01002323/* This function parses a DTLSInnerPlaintext structure.
2324 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01002325static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2326 size_t *content_size,
2327 uint8_t *rec_type )
2328{
2329 size_t remaining = *content_size;
2330
2331 /* Determine length of padding by skipping zeroes from the back. */
2332 do
2333 {
2334 if( remaining == 0 )
2335 return( -1 );
2336 remaining--;
2337 } while( content[ remaining ] == 0 );
2338
2339 *content_size = remaining;
2340 *rec_type = content[ remaining ];
2341
2342 return( 0 );
2343}
Hanno Beckera5a2b082019-05-15 14:03:01 +01002344#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002345
Hanno Becker99abf512019-05-20 14:50:53 +01002346/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01002347 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00002348static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002349 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00002350 mbedtls_record *rec )
2351{
Hanno Becker99abf512019-05-20 14:50:53 +01002352 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01002353 *
2354 * additional_data = seq_num + TLSCompressed.type +
2355 * TLSCompressed.version + TLSCompressed.length;
2356 *
Hanno Becker99abf512019-05-20 14:50:53 +01002357 * For the CID extension, this is extended as follows
2358 * (quoting draft-ietf-tls-dtls-connection-id-05,
2359 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01002360 *
2361 * additional_data = seq_num + DTLSPlaintext.type +
2362 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01002363 * cid +
2364 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01002365 * length_of_DTLSInnerPlaintext;
2366 */
2367
Hanno Becker3307b532017-12-27 21:37:21 +00002368 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2369 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01002370 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01002371
Hanno Beckera5a2b082019-05-15 14:03:01 +01002372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01002373 if( rec->cid_len != 0 )
2374 {
2375 memcpy( add_data + 11, rec->cid, rec->cid_len );
2376 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2377 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2378 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2379 *add_data_len = 13 + 1 + rec->cid_len;
2380 }
2381 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01002382#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01002383 {
2384 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2385 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2386 *add_data_len = 13;
2387 }
Hanno Becker3307b532017-12-27 21:37:21 +00002388}
2389
Hanno Becker611a83b2018-01-03 14:27:32 +00002390int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2391 mbedtls_ssl_transform *transform,
2392 mbedtls_record *rec,
2393 int (*f_rng)(void *, unsigned char *, size_t),
2394 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002397 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00002398 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002399 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00002401 size_t post_avail;
2402
2403 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00002404#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002405 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00002406 ((void) ssl);
2407#endif
2408
2409 /* The PRNG is used for dynamic IV generation that's used
2410 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2411#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2412 ( defined(MBEDTLS_AES_C) || \
2413 defined(MBEDTLS_ARIA_C) || \
2414 defined(MBEDTLS_CAMELLIA_C) ) && \
2415 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2416 ((void) f_rng);
2417 ((void) p_rng);
2418#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Hanno Becker3307b532017-12-27 21:37:21 +00002422 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002423 {
Hanno Becker3307b532017-12-27 21:37:21 +00002424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2425 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2426 }
Hanno Becker505089d2019-05-01 09:45:57 +01002427 if( rec == NULL
2428 || rec->buf == NULL
2429 || rec->buf_len < rec->data_offset
2430 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01002431#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01002432 || rec->cid_len != 0
2433#endif
2434 )
Hanno Becker3307b532017-12-27 21:37:21 +00002435 {
2436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002438 }
2439
Hanno Becker3307b532017-12-27 21:37:21 +00002440 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01002441 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00002443 data, rec->data_len );
2444
2445 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2446
2447 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2448 {
2449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2450 (unsigned) rec->data_len,
2451 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2452 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2453 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002454
Hanno Beckera5a2b082019-05-15 14:03:01 +01002455#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002456 /*
2457 * Add CID information
2458 */
2459 rec->cid_len = transform->out_cid_len;
2460 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2461 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01002462
2463 if( rec->cid_len != 0 )
2464 {
2465 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01002466 * Wrap plaintext into DTLSInnerPlaintext structure.
2467 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01002468 *
Hanno Becker7dc25772019-05-20 15:08:01 +01002469 * Note that this changes `rec->data_len`, and hence
2470 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01002471 */
2472 if( ssl_cid_build_inner_plaintext( data,
2473 &rec->data_len,
2474 post_avail,
2475 rec->type ) != 0 )
2476 {
2477 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2478 }
2479
2480 rec->type = MBEDTLS_SSL_MSG_CID;
2481 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002482#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002483
Hanno Becker92c930f2019-04-29 17:31:37 +01002484 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2485
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002487 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002489#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 if( mode == MBEDTLS_MODE_STREAM ||
2491 ( mode == MBEDTLS_MODE_CBC
2492#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00002493 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002494#endif
2495 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 {
Hanno Becker3307b532017-12-27 21:37:21 +00002497 if( post_avail < transform->maclen )
2498 {
2499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2500 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2501 }
2502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01002504 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
2505 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002506 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002507 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00002508 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2509 data, rec->data_len, rec->ctr, rec->type, mac );
2510 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002511 }
2512 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2515 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01002516 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2517 MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002518 {
Hanno Becker992b6872017-11-09 18:57:39 +00002519 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2520
Hanno Beckere83efe62019-04-29 13:52:53 +01002521 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002522
Hanno Becker3307b532017-12-27 21:37:21 +00002523 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002524 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002525 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2526 data, rec->data_len );
2527 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2528 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2529
2530 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002531 }
2532 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002533#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002537 }
2538
Hanno Becker3307b532017-12-27 21:37:21 +00002539 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2540 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002541
Hanno Becker3307b532017-12-27 21:37:21 +00002542 rec->data_len += transform->maclen;
2543 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002544 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002545 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002546#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002548 /*
2549 * Encrypt
2550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2552 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002554 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002555 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002557 "including %d bytes of padding",
2558 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Hanno Becker3307b532017-12-27 21:37:21 +00002560 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2561 transform->iv_enc, transform->ivlen,
2562 data, rec->data_len,
2563 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002566 return( ret );
2567 }
2568
Hanno Becker3307b532017-12-27 21:37:21 +00002569 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002573 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
Paul Bakker68884e32013-01-07 18:20:04 +01002575 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002577
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002578#if defined(MBEDTLS_GCM_C) || \
2579 defined(MBEDTLS_CCM_C) || \
2580 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002582 mode == MBEDTLS_MODE_CCM ||
2583 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002584 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002585 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002586 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002587 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002588
Hanno Becker3307b532017-12-27 21:37:21 +00002589 /* Check that there's space for both the authentication tag
2590 * and the explicit IV before and after the record content. */
2591 if( post_avail < transform->taglen ||
2592 rec->data_offset < explicit_iv_len )
2593 {
2594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2595 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2596 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002597
Paul Bakker68884e32013-01-07 18:20:04 +01002598 /*
2599 * Generate IV
2600 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002601 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2602 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002603 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002604 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002605 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2606 explicit_iv_len );
2607 /* Prefix record content with explicit IV. */
2608 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 }
2610 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2611 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002612 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 unsigned char i;
2614
2615 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2616
2617 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002618 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002619 }
2620 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002621 {
2622 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002625 }
2626
Hanno Beckere83efe62019-04-29 13:52:53 +01002627 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002628
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2630 iv, transform->ivlen );
2631 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002632 data - explicit_iv_len, explicit_iv_len );
2633 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002634 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002636 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002637 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002638
Paul Bakker68884e32013-01-07 18:20:04 +01002639 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002640 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002641 */
Hanno Becker3307b532017-12-27 21:37:21 +00002642
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002643 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002644 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002645 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002646 data, rec->data_len, /* source */
2647 data, &rec->data_len, /* destination */
2648 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002651 return( ret );
2652 }
2653
Hanno Becker3307b532017-12-27 21:37:21 +00002654 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2655 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002656
Hanno Becker3307b532017-12-27 21:37:21 +00002657 rec->data_len += transform->taglen + explicit_iv_len;
2658 rec->data_offset -= explicit_iv_len;
2659 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002660 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2664#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002665 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002668 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002669 size_t padlen, i;
2670 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002671
Hanno Becker3307b532017-12-27 21:37:21 +00002672 /* Currently we're always using minimal padding
2673 * (up to 255 bytes would be allowed). */
2674 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2675 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 padlen = 0;
2677
Hanno Becker3307b532017-12-27 21:37:21 +00002678 /* Check there's enough space in the buffer for the padding. */
2679 if( post_avail < padlen + 1 )
2680 {
2681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2682 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2683 }
2684
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002686 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Hanno Becker3307b532017-12-27 21:37:21 +00002688 rec->data_len += padlen + 1;
2689 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002692 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002693 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2694 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002695 */
Hanno Becker0a92b812019-06-24 15:46:40 +01002696 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
2697 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002698 {
Hanno Becker3307b532017-12-27 21:37:21 +00002699 if( f_rng == NULL )
2700 {
2701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2703 }
2704
2705 if( rec->data_offset < transform->ivlen )
2706 {
2707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2708 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2709 }
2710
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002711 /*
2712 * Generate IV
2713 */
Hanno Becker3307b532017-12-27 21:37:21 +00002714 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002715 if( ret != 0 )
2716 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002717
Hanno Becker3307b532017-12-27 21:37:21 +00002718 memcpy( data - transform->ivlen, transform->iv_enc,
2719 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002720
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002721 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002725 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002726 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002727 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Hanno Becker3307b532017-12-27 21:37:21 +00002729 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2730 transform->iv_enc,
2731 transform->ivlen,
2732 data, rec->data_len,
2733 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002736 return( ret );
2737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002738
Hanno Becker3307b532017-12-27 21:37:21 +00002739 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2742 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002743 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01002746 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
2747 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002748 {
2749 /*
2750 * Save IV in SSL3 and TLS1
2751 */
Hanno Becker3307b532017-12-27 21:37:21 +00002752 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2753 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 }
Hanno Becker3307b532017-12-27 21:37:21 +00002755 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002756#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002757 {
2758 data -= transform->ivlen;
2759 rec->data_offset -= transform->ivlen;
2760 rec->data_len += transform->ivlen;
2761 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002763#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002764 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002765 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002766 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2767
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002768 /*
2769 * MAC(MAC_write_key, seq_num +
2770 * TLSCipherText.type +
2771 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002772 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002773 * IV + // except for TLS 1.0
2774 * ENC(content + padding + padding_length));
2775 */
Hanno Becker3307b532017-12-27 21:37:21 +00002776
2777 if( post_avail < transform->maclen)
2778 {
2779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2780 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2781 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782
Hanno Beckere83efe62019-04-29 13:52:53 +01002783 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002786 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002787 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002788
Hanno Becker3307b532017-12-27 21:37:21 +00002789 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002790 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002791 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2792 data, rec->data_len );
2793 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2794 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002795
Hanno Becker3307b532017-12-27 21:37:21 +00002796 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002797
Hanno Becker3307b532017-12-27 21:37:21 +00002798 rec->data_len += transform->maclen;
2799 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002800 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002801 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002804 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002805#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002806 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2809 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002812 /* Make extra sure authentication was performed, exactly once */
2813 if( auth_done != 1 )
2814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2816 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002817 }
2818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
2821 return( 0 );
2822}
2823
Hanno Becker40478be2019-07-12 08:23:59 +01002824int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Becker611a83b2018-01-03 14:27:32 +00002825 mbedtls_ssl_transform *transform,
2826 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002827{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002828 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002830 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002831#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002832 size_t padlen = 0, correct = 1;
2833#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002834 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002835 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002836 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002837
Hanno Becker611a83b2018-01-03 14:27:32 +00002838#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002839 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002840 ((void) ssl);
2841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002844 if( rec == NULL ||
2845 rec->buf == NULL ||
2846 rec->buf_len < rec->data_offset ||
2847 rec->buf_len - rec->data_offset < rec->data_len )
2848 {
2849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002851 }
2852
Hanno Becker4c6876b2017-12-27 21:28:58 +00002853 data = rec->buf + rec->data_offset;
2854 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Hanno Beckera5a2b082019-05-15 14:03:01 +01002856#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002857 /*
2858 * Match record's CID with incoming CID.
2859 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002860 if( rec->cid_len != transform->in_cid_len ||
2861 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2862 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002863 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002864 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002865#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2868 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002869 {
2870 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002871 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2872 transform->iv_dec,
2873 transform->ivlen,
2874 data, rec->data_len,
2875 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002878 return( ret );
2879 }
2880
Hanno Becker4c6876b2017-12-27 21:28:58 +00002881 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002885 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 }
Paul Bakker68884e32013-01-07 18:20:04 +01002887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002889#if defined(MBEDTLS_GCM_C) || \
2890 defined(MBEDTLS_CCM_C) || \
2891 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002893 mode == MBEDTLS_MODE_CCM ||
2894 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002895 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002896 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002897 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002898
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002899 /*
2900 * Prepare IV from explicit and implicit data.
2901 */
2902
2903 /* Check that there's enough space for the explicit IV
2904 * (at the beginning of the record) and the MAC (at the
2905 * end of the record). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002906 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002909 "+ taglen (%d)", rec->data_len,
2910 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002912 }
Paul Bakker68884e32013-01-07 18:20:04 +01002913
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002914#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002915 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2916 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002917 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002918
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002919 /* Fixed */
2920 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2921 /* Explicit */
2922 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002923 }
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002924 else
2925#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2926#if defined(MBEDTLS_CHACHAPOLY_C)
2927 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002928 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002929 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002930 unsigned char i;
2931
2932 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2933
2934 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002935 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002936 }
2937 else
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002938#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002939 {
2940 /* Reminder if we ever add an AEAD mode with a different size */
2941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2942 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2943 }
2944
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002945 /* Group changes to data, data_len, and add_data, because
2946 * add_data depends on data_len. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002947 data += explicit_iv_len;
2948 rec->data_offset += explicit_iv_len;
2949 rec->data_len -= explicit_iv_len + transform->taglen;
2950
Hanno Beckere83efe62019-04-29 13:52:53 +01002951 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002952 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002953 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002954
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002955 /* Because of the check above, we know that there are
2956 * explicit_iv_len Bytes preceeding data, and taglen
2957 * bytes following data + data_len. This justifies
Hanno Becker07d420d2019-07-10 11:44:13 +01002958 * the debug message and the invocation of
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002959 * mbedtls_cipher_auth_decrypt() below. */
2960
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002961 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002962 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002963 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002964
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002965 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002966 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002967 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002968 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2969 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002970 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002971 data, rec->data_len,
2972 data, &olen,
2973 data + rec->data_len,
2974 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2979 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002980
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002981 return( ret );
2982 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002983 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002984
Hanno Becker6d3db0f2019-07-10 13:55:25 +01002985 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002986 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002990 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002991 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2994#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002995 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002998 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002999
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 /*
Paul Bakker45829992013-01-03 14:52:21 +01003001 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00003002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003004 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3005 MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003006 {
3007 /* The ciphertext is prefixed with the CBC IV. */
3008 minlen += transform->ivlen;
3009 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003010#endif
Paul Bakker45829992013-01-03 14:52:21 +01003011
Hanno Becker4c6876b2017-12-27 21:28:58 +00003012 /* Size considerations:
3013 *
3014 * - The CBC cipher text must not be empty and hence
3015 * at least of size transform->ivlen.
3016 *
3017 * Together with the potential IV-prefix, this explains
3018 * the first of the two checks below.
3019 *
3020 * - The record must contain a MAC, either in plain or
3021 * encrypted, depending on whether Encrypt-then-MAC
3022 * is used or not.
3023 * - If it is, the message contains the IV-prefix,
3024 * the CBC ciphertext, and the MAC.
3025 * - If it is not, the padded plaintext, and hence
3026 * the CBC ciphertext, has at least length maclen + 1
3027 * because there is at least the padding length byte.
3028 *
3029 * As the CBC ciphertext is not empty, both cases give the
3030 * lower bound minlen + maclen + 1 on the record size, which
3031 * we test for in the second check below.
3032 */
3033 if( rec->data_len < minlen + transform->ivlen ||
3034 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01003035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003037 "+ 1 ) ( + expl IV )", rec->data_len,
3038 transform->ivlen,
3039 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01003041 }
3042
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003043 /*
3044 * Authenticate before decrypt if enabled
3045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003047 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003048 {
Hanno Becker992b6872017-11-09 18:57:39 +00003049 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003052
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003053 /* Update data_len in tandem with add_data.
3054 *
3055 * The subtraction is safe because of the previous check
3056 * data_len >= minlen + maclen + 1.
3057 *
3058 * Afterwards, we know that data + data_len is followed by at
3059 * least maclen Bytes, which justifies the call to
3060 * mbedtls_ssl_safer_memcmp() below.
3061 *
3062 * Further, we still know that data_len > minlen */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003063 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003064 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003065
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003066 /* Calculate expected MAC. */
Hanno Beckere83efe62019-04-29 13:52:53 +01003067 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
3068 add_data_len );
3069 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3070 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003071 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3072 data, rec->data_len );
3073 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3074 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003075
Hanno Becker4c6876b2017-12-27 21:28:58 +00003076 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3077 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003078 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00003079 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003080
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003081 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003082 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3083 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003087 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003088 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003091
3092 /*
3093 * Check length sanity
3094 */
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003095
3096 /* We know from above that data_len > minlen >= 0,
3097 * so the following check in particular implies that
3098 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003099 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003102 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003104 }
3105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003107 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003108 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003109 */
Hanno Becker0a92b812019-06-24 15:46:40 +01003110 if( mbedtls_ssl_transform_get_minor_ver( transform ) >=
3111 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003112 {
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003113 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003114 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003115
Hanno Becker4c6876b2017-12-27 21:28:58 +00003116 data += transform->ivlen;
3117 rec->data_offset += transform->ivlen;
3118 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003119 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003121
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003122 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3123
Hanno Becker4c6876b2017-12-27 21:28:58 +00003124 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3125 transform->iv_dec, transform->ivlen,
3126 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003129 return( ret );
3130 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003131
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003132 /* Double-check that length hasn't changed during decryption. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003133 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003137 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker0a92b812019-06-24 15:46:40 +01003140 if( mbedtls_ssl_transform_get_minor_ver( transform ) <
3141 MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003142 {
3143 /*
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003144 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3145 * records is equivalent to CBC decryption of the concatenation
3146 * of the records; in other words, IVs are maintained across
3147 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003148 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003149 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3150 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003153
Hanno Becker4c6876b2017-12-27 21:28:58 +00003154 /* Safe since data_len >= minlen + maclen + 1, so after having
3155 * subtracted at most minlen and maclen up to this point,
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003156 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3157 * >= ivlen ). */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003158 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003159
Hanno Becker4c6876b2017-12-27 21:28:58 +00003160 if( auth_done == 1 )
3161 {
3162 correct *= ( rec->data_len >= padlen + 1 );
3163 padlen *= ( rec->data_len >= padlen + 1 );
3164 }
3165 else
Paul Bakker45829992013-01-03 14:52:21 +01003166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003168 if( rec->data_len < transform->maclen + padlen + 1 )
3169 {
3170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3171 rec->data_len,
3172 transform->maclen,
3173 padlen + 1 ) );
3174 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003175#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00003176
3177 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3178 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003179 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003180
Hanno Becker4c6876b2017-12-27 21:28:58 +00003181 padlen++;
3182
3183 /* Regardless of the validity of the padding,
3184 * we have data_len >= padlen here. */
3185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003187 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3188 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003190 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if defined(MBEDTLS_SSL_DEBUG_ALL)
3193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00003194 "should be no more than %d",
3195 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003196#endif
Paul Bakker45829992013-01-03 14:52:21 +01003197 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 }
3199 }
3200 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3202#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3203 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003204 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3205 MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003207 /* The padding check involves a series of up to 256
3208 * consecutive memory reads at the end of the record
3209 * plaintext buffer. In order to hide the length and
3210 * validity of the padding, always perform exactly
3211 * `min(256,plaintext_len)` reads (but take into account
3212 * only the last `padlen` bytes for the padding check). */
3213 size_t pad_count = 0;
3214 size_t real_count = 0;
3215 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003216
Hanno Becker4c6876b2017-12-27 21:28:58 +00003217 /* Index of first padding byte; it has been ensured above
3218 * that the subtraction is safe. */
3219 size_t const padding_idx = rec->data_len - padlen;
3220 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3221 size_t const start_idx = rec->data_len - num_checks;
3222 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003223
Hanno Becker4c6876b2017-12-27 21:28:58 +00003224 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003225 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003226 real_count |= ( idx >= padding_idx );
3227 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003228 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00003229 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003232 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003234#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003235 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003237 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3239 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003243 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003244
Hanno Becker4c6876b2017-12-27 21:28:58 +00003245 /* If the padding was found to be invalid, padlen == 0
3246 * and the subtraction is safe. If the padding was found valid,
3247 * padlen hasn't been changed and the previous assertion
3248 * data_len >= padlen still holds. */
3249 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003251 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003253 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003257 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003258
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003259#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00003261 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003263
3264 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003265 * Authenticate if not done yet.
3266 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00003268#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003269 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003270 {
Hanno Becker992b6872017-11-09 18:57:39 +00003271 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003272
Hanno Becker4c6876b2017-12-27 21:28:58 +00003273 /* If the initial value of padlen was such that
3274 * data_len < maclen + padlen + 1, then padlen
3275 * got reset to 1, and the initial check
3276 * data_len >= minlen + maclen + 1
3277 * guarantees that at this point we still
3278 * have at least data_len >= maclen.
3279 *
3280 * If the initial value of padlen was such that
3281 * data_len >= maclen + padlen + 1, then we have
3282 * subtracted either padlen + 1 (if the padding was correct)
3283 * or 0 (if the padding was incorrect) since then,
3284 * hence data_len >= maclen in any case.
3285 */
3286 rec->data_len -= transform->maclen;
Hanno Beckere83efe62019-04-29 13:52:53 +01003287 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker0a92b812019-06-24 15:46:40 +01003290 if( mbedtls_ssl_transform_get_minor_ver( transform ) ==
3291 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003292 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00003293 ssl_mac( &transform->md_ctx_dec,
3294 transform->mac_dec,
3295 data, rec->data_len,
3296 rec->ctr, rec->type,
3297 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003298 }
3299 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3301#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3302 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker0a92b812019-06-24 15:46:40 +01003303 if( mbedtls_ssl_transform_get_minor_ver( transform ) >
3304 MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003305 {
3306 /*
3307 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003308 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003309 *
3310 * Known timing attacks:
3311 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3312 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003313 * To compensate for different timings for the MAC calculation
3314 * depending on how much padding was removed (which is determined
3315 * by padlen), process extra_run more blocks through the hash
3316 * function.
3317 *
3318 * The formula in the paper is
3319 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3320 * where L1 is the size of the header plus the decrypted message
3321 * plus CBC padding and L2 is the size of the header plus the
3322 * decrypted message. This is for an underlying hash function
3323 * with 64-byte blocks.
3324 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3325 * correctly. We round down instead of up, so -56 is the correct
3326 * value for our calculations instead of -55.
3327 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003328 * Repeat the formula rather than defining a block_size variable.
3329 * This avoids requiring division by a variable at runtime
3330 * (which would be marginally less efficient and would require
3331 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003332 */
3333 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00003334 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003335
3336 /*
3337 * The next two sizes are the minimum and maximum values of
3338 * in_msglen over all padlen values.
3339 *
3340 * They're independent of padlen, since we previously did
Hanno Becker6d3db0f2019-07-10 13:55:25 +01003341 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003342 *
3343 * Note that max_len + maclen is never more than the buffer
3344 * length, as we previously did in_msglen -= maclen too.
3345 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003346 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003347 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3348
Hanno Becker4c6876b2017-12-27 21:28:58 +00003349 memset( tmp, 0, sizeof( tmp ) );
3350
3351 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003352 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003353#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3354 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003355 case MBEDTLS_MD_MD5:
3356 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003357 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003358 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003359 extra_run =
3360 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3361 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003362 break;
3363#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003364#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003365 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003366 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01003367 extra_run =
3368 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3369 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003370 break;
3371#endif
3372 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003374 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3375 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003376
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003377 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003378
Hanno Beckere83efe62019-04-29 13:52:53 +01003379 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3380 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00003381 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3382 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003383 /* Make sure we access everything even when padlen > 0. This
3384 * makes the synchronisation requirements for just-in-time
3385 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003386 ssl_read_memory( data + rec->data_len, padlen );
3387 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003388
3389 /* Call mbedtls_md_process at least once due to cache attacks
3390 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003391 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00003392 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003393
Hanno Becker4c6876b2017-12-27 21:28:58 +00003394 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003395
3396 /* Make sure we access all the memory that could contain the MAC,
3397 * before we check it in the next code block. This makes the
3398 * synchronisation requirements for just-in-time Prime+Probe
3399 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00003400 ssl_read_memory( data + min_len,
3401 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003402 }
3403 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3405 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003410
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003411#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00003412 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3413 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003414#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003415
Hanno Becker4c6876b2017-12-27 21:28:58 +00003416 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3417 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#if defined(MBEDTLS_SSL_DEBUG_ALL)
3420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003421#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003422 correct = 0;
3423 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003424 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003425 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003426
3427 /*
3428 * Finally check the correct flag
3429 */
3430 if( correct == 0 )
3431 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00003432#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003433
3434 /* Make extra sure authentication was performed, exactly once */
3435 if( auth_done != 1 )
3436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3438 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003439 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003440
Hanno Beckera5a2b082019-05-15 14:03:01 +01003441#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01003442 if( rec->cid_len != 0 )
3443 {
3444 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3445 &rec->type );
3446 if( ret != 0 )
3447 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3448 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01003449#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01003450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003452
3453 return( 0 );
3454}
3455
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003456#undef MAC_NONE
3457#undef MAC_PLAINTEXT
3458#undef MAC_CIPHERTEXT
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461/*
3462 * Compression/decompression functions
3463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003465{
3466 int ret;
3467 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003468 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003469 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003470 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003473
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003474 if( len_pre == 0 )
3475 return( 0 );
3476
Paul Bakker2770fbd2012-07-03 13:30:23 +00003477 memcpy( msg_pre, ssl->out_msg, len_pre );
3478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003480 ssl->out_msglen ) );
3481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003483 ssl->out_msg, ssl->out_msglen );
3484
Paul Bakker48916f92012-09-16 19:57:18 +00003485 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3486 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3487 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003488 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003489
Paul Bakker48916f92012-09-16 19:57:18 +00003490 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003491 if( ret != Z_OK )
3492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3494 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003495 }
3496
Angus Grattond8213d02016-05-25 20:56:48 +10003497 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003498 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003501 ssl->out_msglen ) );
3502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003504 ssl->out_msg, ssl->out_msglen );
3505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003507
3508 return( 0 );
3509}
3510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003512{
3513 int ret;
3514 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003515 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003516 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003517 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003521 if( len_pre == 0 )
3522 return( 0 );
3523
Paul Bakker2770fbd2012-07-03 13:30:23 +00003524 memcpy( msg_pre, ssl->in_msg, len_pre );
3525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003527 ssl->in_msglen ) );
3528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003530 ssl->in_msg, ssl->in_msglen );
3531
Paul Bakker48916f92012-09-16 19:57:18 +00003532 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3533 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3534 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003535 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003536 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003537
Paul Bakker48916f92012-09-16 19:57:18 +00003538 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003539 if( ret != Z_OK )
3540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3542 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003543 }
3544
Angus Grattond8213d02016-05-25 20:56:48 +10003545 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003546 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003549 ssl->in_msglen ) );
3550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003551 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003552 ssl->in_msg, ssl->in_msglen );
3553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003555
3556 return( 0 );
3557}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3561static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563#if defined(MBEDTLS_SSL_PROTO_DTLS)
3564static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003565{
3566 /* If renegotiation is not enforced, retransmit until we would reach max
3567 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003568 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003569 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003570 uint32_t ratio =
3571 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3572 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003573 unsigned char doublings = 1;
3574
3575 while( ratio != 0 )
3576 {
3577 ++doublings;
3578 ratio >>= 1;
3579 }
3580
3581 if( ++ssl->renego_records_seen > doublings )
3582 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003584 return( 0 );
3585 }
3586 }
3587
3588 return( ssl_write_hello_request( ssl ) );
3589}
3590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003592
Paul Bakker5121ce52009-01-03 21:22:43 +00003593/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003594 * Fill the input message buffer by appending data to it.
3595 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003596 *
3597 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3598 * available (from this read and/or a previous one). Otherwise, an error code
3599 * is returned (possibly EOF or WANT_READ).
3600 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003601 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3602 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3603 * since we always read a whole datagram at once.
3604 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003605 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003606 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003609{
Paul Bakker23986e52011-04-24 08:57:21 +00003610 int ret;
3611 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003614
Hanno Beckera58a8962019-06-13 16:11:15 +01003615 if( mbedtls_ssl_get_recv( ssl ) == NULL &&
3616 mbedtls_ssl_get_recv_timeout( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003619 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003621 }
3622
Angus Grattond8213d02016-05-25 20:56:48 +10003623 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003627 }
3628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003630 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003632 uint32_t timeout;
3633
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003634 /* Just to be sure */
Hanno Becker0ae6b242019-06-13 16:45:36 +01003635 if( mbedtls_ssl_get_set_timer( ssl ) == NULL ||
3636 mbedtls_ssl_get_get_timer( ssl ) == NULL )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003637 {
3638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3639 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3641 }
3642
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003643 /*
3644 * The point is, we need to always read a full datagram at once, so we
3645 * sometimes read more then requested, and handle the additional data.
3646 * It could be the rest of the current record (while fetching the
3647 * header) and/or some other records in the same datagram.
3648 */
3649
3650 /*
3651 * Move to the next record in the already read datagram if applicable
3652 */
3653 if( ssl->next_record_offset != 0 )
3654 {
3655 if( ssl->in_left < ssl->next_record_offset )
3656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003659 }
3660
3661 ssl->in_left -= ssl->next_record_offset;
3662
3663 if( ssl->in_left != 0 )
3664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003666 ssl->next_record_offset ) );
3667 memmove( ssl->in_hdr,
3668 ssl->in_hdr + ssl->next_record_offset,
3669 ssl->in_left );
3670 }
3671
3672 ssl->next_record_offset = 0;
3673 }
3674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003677
3678 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003679 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003680 */
3681 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003684 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003685 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003686
3687 /*
Antonin Décimod5f47592019-01-23 15:24:37 +01003688 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003689 * are not at the beginning of a new record, the caller did something
3690 * wrong.
3691 */
3692 if( ssl->in_left != 0 )
3693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3695 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003696 }
3697
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003698 /*
3699 * Don't even try to read if time's out already.
3700 * This avoids by-passing the timer when repeatedly receiving messages
3701 * that will end up being dropped.
3702 */
3703 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003704 {
3705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003706 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003707 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003708 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003709 {
Angus Grattond8213d02016-05-25 20:56:48 +10003710 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003713 timeout = ssl->handshake->retransmit_timeout;
3714 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003715 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003718
Hanno Beckera58a8962019-06-13 16:11:15 +01003719 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
3720 {
3721 ret = mbedtls_ssl_get_recv_timeout( ssl )
3722 ( ssl->p_bio, ssl->in_hdr, len, timeout );
3723 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003724 else
Hanno Beckera58a8962019-06-13 16:11:15 +01003725 {
3726 ret = mbedtls_ssl_get_recv( ssl )
3727 ( ssl->p_bio, ssl->in_hdr, len );
3728 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003731
3732 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003733 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003734 }
3735
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003736 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003739 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003742 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003743 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003746 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003747 }
3748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003752 return( ret );
3753 }
3754
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003755 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003756 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +01003758 else if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
3759 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003761 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003762 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003763 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003765 return( ret );
3766 }
3767
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003768 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003769 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003771 }
3772
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 if( ret < 0 )
3774 return( ret );
3775
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003776 ssl->in_left = ret;
3777 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003778 MBEDTLS_SSL_TRANSPORT_ELSE
3779#endif /* MBEDTLS_SSL_PROTO_DTLS */
3780#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003783 ssl->in_left, nb_want ) );
3784
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003785 while( ssl->in_left < nb_want )
3786 {
3787 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003788
3789 if( ssl_check_timer( ssl ) != 0 )
3790 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3791 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003792 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003793 if( mbedtls_ssl_get_recv_timeout( ssl ) != NULL )
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003794 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003795 ret = mbedtls_ssl_get_recv_timeout( ssl )( ssl->p_bio,
3796 ssl->in_hdr + ssl->in_left, len,
3797 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003798 }
3799 else
3800 {
Hanno Beckera58a8962019-06-13 16:11:15 +01003801 ret = mbedtls_ssl_get_recv( ssl )( ssl->p_bio,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003802 ssl->in_hdr + ssl->in_left, len );
3803 }
3804 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003807 ssl->in_left, nb_want ) );
3808 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003809
3810 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003812
3813 if( ret < 0 )
3814 return( ret );
3815
mohammad160352aecb92018-03-28 23:41:40 -07003816 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003817 {
Darryl Green11999bb2018-03-13 15:22:58 +00003818 MBEDTLS_SSL_DEBUG_MSG( 1,
3819 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003820 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003821 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3822 }
3823
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003824 ssl->in_left += ret;
3825 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003826 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003827#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003830
3831 return( 0 );
3832}
3833
3834/*
3835 * Flush any data not yet written
3836 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003837int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003838{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003839 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003840 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003843
Hanno Beckera58a8962019-06-13 16:11:15 +01003844 if( mbedtls_ssl_get_send( ssl ) == NULL )
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003847 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003849 }
3850
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003851 /* Avoid incrementing counter if data is flushed */
3852 if( ssl->out_left == 0 )
3853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003855 return( 0 );
3856 }
3857
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 while( ssl->out_left > 0 )
3859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003861 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862
Hanno Becker2b1e3542018-08-06 11:19:13 +01003863 buf = ssl->out_hdr - ssl->out_left;
Hanno Beckera58a8962019-06-13 16:11:15 +01003864 ret = mbedtls_ssl_get_send( ssl )( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003867
3868 if( ret <= 0 )
3869 return( ret );
3870
mohammad160352aecb92018-03-28 23:41:40 -07003871 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003872 {
Darryl Green11999bb2018-03-13 15:22:58 +00003873 MBEDTLS_SSL_DEBUG_MSG( 1,
3874 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003875 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003876 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3877 }
3878
Paul Bakker5121ce52009-01-03 21:22:43 +00003879 ssl->out_left -= ret;
3880 }
3881
Hanno Becker2b1e3542018-08-06 11:19:13 +01003882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003883 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003884 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003885 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003886 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003887 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003888#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003889#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003890 {
3891 ssl->out_hdr = ssl->out_buf + 8;
3892 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003893#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003894 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003897
3898 return( 0 );
3899}
3900
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003901/*
3902 * Functions to handle the DTLS retransmission state machine
3903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003905/*
3906 * Append current handshake message to current outgoing flight
3907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003909{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3912 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3913 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003914
3915 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003916 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003917 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003919 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003920 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003921 }
3922
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003923 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003924 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003927 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928 }
3929
3930 /* Copy current handshake message with headers */
3931 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3932 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003933 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934 msg->next = NULL;
3935
3936 /* Append to the current flight */
3937 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003938 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003939 else
3940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003942 while( cur->next != NULL )
3943 cur = cur->next;
3944 cur->next = msg;
3945 }
3946
Hanno Becker3b235902018-08-06 09:54:53 +01003947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003948 return( 0 );
3949}
3950
3951/*
3952 * Free the current flight of handshake messages
3953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956 mbedtls_ssl_flight_item *cur = flight;
3957 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003958
3959 while( cur != NULL )
3960 {
3961 next = cur->next;
3962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963 mbedtls_free( cur->p );
3964 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003965
3966 cur = next;
3967 }
3968}
3969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3971static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003972#endif
3973
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003974/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003975 * Swap transform_out and out_ctr with the alternative ones
3976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003978{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003980 unsigned char tmp_out_ctr[8];
3981
3982 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003985 return;
3986 }
3987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003989
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003990 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003991 tmp_transform = ssl->transform_out;
3992 ssl->transform_out = ssl->handshake->alt_transform_out;
3993 ssl->handshake->alt_transform_out = tmp_transform;
3994
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003995 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003996 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3997 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003998 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003999
4000 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004001 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4004 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004008 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
4009 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004010 }
4011 }
4012#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004013}
4014
4015/*
4016 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004017 */
4018int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
4019{
4020 int ret = 0;
4021
4022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
4023
4024 ret = mbedtls_ssl_flight_transmit( ssl );
4025
4026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
4027
4028 return( ret );
4029}
4030
4031/*
4032 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004033 *
4034 * Need to remember the current message in case flush_output returns
4035 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004036 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004037 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004038int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004039{
Hanno Becker67bc7c32018-08-06 11:33:50 +01004040 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004044 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004046
4047 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004048 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004049 ssl_swap_epochs( ssl );
4050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004052 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004053
4054 while( ssl->handshake->cur_msg != NULL )
4055 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004056 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004057 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004058
Hanno Beckere1dcb032018-08-17 16:47:58 +01004059 int const is_finished =
4060 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
4061 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
4062
Hanno Becker04da1892018-08-14 13:22:10 +01004063 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
4064 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
4065
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004066 /* Swap epochs before sending Finished: we can't do it after
4067 * sending ChangeCipherSpec, in case write returns WANT_READ.
4068 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01004069 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004070 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02004072 ssl_swap_epochs( ssl );
4073 }
4074
Hanno Becker67bc7c32018-08-06 11:33:50 +01004075 ret = ssl_get_remaining_payload_in_datagram( ssl );
4076 if( ret < 0 )
4077 return( ret );
4078 max_frag_len = (size_t) ret;
4079
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004080 /* CCS is copied as is, while HS messages may need fragmentation */
4081 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4082 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004083 if( max_frag_len == 0 )
4084 {
4085 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4086 return( ret );
4087
4088 continue;
4089 }
4090
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004091 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004092 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004093 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004094
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004095 /* Update position inside current message */
4096 ssl->handshake->cur_msg_p += cur->len;
4097 }
4098 else
4099 {
4100 const unsigned char * const p = ssl->handshake->cur_msg_p;
4101 const size_t hs_len = cur->len - 12;
4102 const size_t frag_off = p - ( cur->p + 12 );
4103 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004104 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004105
Hanno Beckere1dcb032018-08-17 16:47:58 +01004106 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004107 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004108 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004109 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004110
Hanno Becker67bc7c32018-08-06 11:33:50 +01004111 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4112 return( ret );
4113
4114 continue;
4115 }
4116 max_hs_frag_len = max_frag_len - 12;
4117
4118 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4119 max_hs_frag_len : rem_len;
4120
4121 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004122 {
4123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004124 (unsigned) cur_hs_frag_len,
4125 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004126 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004127
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004128 /* Messages are stored with handshake headers as if not fragmented,
4129 * copy beginning of headers then fill fragmentation fields.
4130 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4131 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004132
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004133 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4134 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4135 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4136
Hanno Becker67bc7c32018-08-06 11:33:50 +01004137 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4138 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4139 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004140
4141 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4142
Hanno Becker3f7b9732018-08-28 09:53:25 +01004143 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004144 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4145 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004146 ssl->out_msgtype = cur->type;
4147
4148 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004149 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004150 }
4151
4152 /* If done with the current message move to the next one if any */
4153 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4154 {
4155 if( cur->next != NULL )
4156 {
4157 ssl->handshake->cur_msg = cur->next;
4158 ssl->handshake->cur_msg_p = cur->next->p + 12;
4159 }
4160 else
4161 {
4162 ssl->handshake->cur_msg = NULL;
4163 ssl->handshake->cur_msg_p = NULL;
4164 }
4165 }
4166
4167 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004168 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004171 return( ret );
4172 }
4173 }
4174
Hanno Becker67bc7c32018-08-06 11:33:50 +01004175 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4176 return( ret );
4177
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004178 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4180 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004181 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004184 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4185 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004186
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004188
4189 return( 0 );
4190}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004191
4192/*
4193 * To be called when the last message of an incoming flight is received.
4194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004195void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004196{
4197 /* We won't need to resend that one any more */
4198 ssl_flight_free( ssl->handshake->flight );
4199 ssl->handshake->flight = NULL;
4200 ssl->handshake->cur_msg = NULL;
4201
4202 /* The next incoming flight will start with this msg_seq */
4203 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4204
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004205 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004206 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004207
Hanno Becker0271f962018-08-16 13:23:47 +01004208 /* Clear future message buffering structure. */
4209 ssl_buffering_free( ssl );
4210
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004211 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004212 ssl_set_timer( ssl, 0 );
4213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4215 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004218 }
4219 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004221}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004222
4223/*
4224 * To be called when the last message of an outgoing flight is send.
4225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004227{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004228 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004229 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4232 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004235 }
4236 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004239#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004240
Paul Bakker5121ce52009-01-03 21:22:43 +00004241/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004242 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004244
4245/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004246 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004247 *
4248 * - fill in handshake headers
4249 * - update handshake checksum
4250 * - DTLS: save message for resending
4251 * - then pass to the record layer
4252 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004253 * DTLS: except for HelloRequest, messages are only queued, and will only be
4254 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004255 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004256 * Inputs:
4257 * - ssl->out_msglen: 4 + actual handshake message len
4258 * (4 is the size of handshake headers for TLS)
4259 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4260 * - ssl->out_msg + 4: the handshake message body
4261 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004262 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004263 * - ssl->out_msglen: the length of the record contents
4264 * (including handshake headers but excluding record headers)
4265 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004266 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004267int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004268{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004269 int ret;
4270 const size_t hs_len = ssl->out_msglen - 4;
4271 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4274
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004275 /*
4276 * Sanity checks
4277 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004278 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004279 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4280 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004281 /* In SSLv3, the client might send a NoCertificate alert. */
4282#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2881d802019-05-22 14:44:53 +01004283 if( ! ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004284 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01004285 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
4286 MBEDTLS_SSL_IS_CLIENT ) )
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004287#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4288 {
4289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4291 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004292 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004293
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004294 /* Whenever we send anything different from a
4295 * HelloRequest we should be in a handshake - double check. */
4296 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4297 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004298 ssl->handshake == NULL )
4299 {
4300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004305 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004306 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004307 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004308 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004311 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004312#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004313
Hanno Beckerb50a2532018-08-06 11:52:54 +01004314 /* Double-check that we did not exceed the bounds
4315 * of the outgoing record buffer.
4316 * This should never fail as the various message
4317 * writing functions must obey the bounds of the
4318 * outgoing record buffer, but better be safe.
4319 *
4320 * Note: We deliberately do not check for the MTU or MFL here.
4321 */
4322 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4323 {
4324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4325 "size %u, maximum %u",
4326 (unsigned) ssl->out_msglen,
4327 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4329 }
4330
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004331 /*
4332 * Fill handshake headers
4333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004334 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004335 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004336 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4337 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4338 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004339
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004340 /*
4341 * DTLS has additional fields in the Handshake layer,
4342 * between the length field and the actual payload:
4343 * uint16 message_seq;
4344 * uint24 fragment_offset;
4345 * uint24 fragment_length;
4346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004348 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004349 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004350 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004351 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004352 {
4353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4354 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004355 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004356 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4358 }
4359
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004360 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004361 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004362
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004363 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004364 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004365 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004366 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4367 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4368 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004369 }
4370 else
4371 {
4372 ssl->out_msg[4] = 0;
4373 ssl->out_msg[5] = 0;
4374 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004375
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004376 /* Handshake hashes are computed without fragmentation,
4377 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004378 memset( ssl->out_msg + 6, 0x00, 3 );
4379 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004380 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004382
Hanno Becker0207e532018-08-28 10:28:28 +01004383 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004384 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004385 mbedtls_ssl_update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 }
4387
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004388 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004390 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00004391 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4392 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004393 {
4394 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004397 return( ret );
4398 }
4399 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004400 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004401#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004402 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004403 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004404 {
4405 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4406 return( ret );
4407 }
4408 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004409
4410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4411
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004412 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004413}
4414
4415/*
4416 * Record layer functions
4417 */
4418
4419/*
4420 * Write current record.
4421 *
4422 * Uses:
4423 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4424 * - ssl->out_msglen: length of the record content (excl headers)
4425 * - ssl->out_msg: record content
4426 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004427int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004428{
4429 int ret, done = 0;
4430 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004431 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004432
4433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004435#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004436 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004438 {
4439 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004441 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004442 return( ret );
4443 }
4444
4445 len = ssl->out_msglen;
4446 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4450 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 ret = mbedtls_ssl_hw_record_write( ssl );
4455 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4458 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004459 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004460
4461 if( ret == 0 )
4462 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004465 if( !done )
4466 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004467 unsigned i;
4468 size_t protected_record_size;
4469
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004470 /* Skip writing the record content type to after the encryption,
4471 * as it may change when using the CID extension. */
4472
Hanno Becker2881d802019-05-22 14:44:53 +01004473 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4474 mbedtls_ssl_get_minor_ver( ssl ),
4475 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004476
Hanno Becker19859472018-08-06 09:40:20 +01004477 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004478 ssl->out_len[0] = (unsigned char)( len >> 8 );
4479 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004480
Paul Bakker48916f92012-09-16 19:57:18 +00004481 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004482 {
Hanno Becker3307b532017-12-27 21:37:21 +00004483 mbedtls_record rec;
4484
4485 rec.buf = ssl->out_iv;
4486 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4487 ( ssl->out_iv - ssl->out_buf );
4488 rec.data_len = ssl->out_msglen;
4489 rec.data_offset = ssl->out_msg - rec.buf;
4490
4491 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
Hanno Becker2881d802019-05-22 14:44:53 +01004492 mbedtls_ssl_write_version( mbedtls_ssl_get_major_ver( ssl ),
4493 mbedtls_ssl_get_minor_ver( ssl ),
Hanno Becker3307b532017-12-27 21:37:21 +00004494 ssl->conf->transport, rec.ver );
4495 rec.type = ssl->out_msgtype;
4496
Hanno Beckera5a2b082019-05-15 14:03:01 +01004497#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01004498 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01004499 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004500#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01004501
Hanno Becker611a83b2018-01-03 14:27:32 +00004502 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Beckerece325c2019-06-13 15:39:27 +01004503 mbedtls_ssl_conf_get_frng( ssl->conf ),
Hanno Becker9a122432019-07-23 13:24:02 +01004504 mbedtls_ssl_conf_get_prng( ssl->conf ) ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004507 return( ret );
4508 }
4509
Hanno Becker3307b532017-12-27 21:37:21 +00004510 if( rec.data_offset != 0 )
4511 {
4512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4514 }
4515
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004516 /* Update the record content type and CID. */
4517 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004518#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01004519 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004520#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00004521 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00004522 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4523 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004524 }
4525
Hanno Becker43395762019-05-03 14:46:38 +01004526 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004527
4528#if defined(MBEDTLS_SSL_PROTO_DTLS)
4529 /* In case of DTLS, double-check that we don't exceed
4530 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004531 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01004532 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004533 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004534 if( ret < 0 )
4535 return( ret );
4536
4537 if( protected_record_size > (size_t) ret )
4538 {
4539 /* Should never happen */
4540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4541 }
4542 }
4543#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004544
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004545 /* Now write the potentially updated record content type. */
4546 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004549 "version = [%d:%d], msglen = %d",
4550 ssl->out_hdr[0], ssl->out_hdr[1],
4551 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004553 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004554 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004555
4556 ssl->out_left += protected_record_size;
4557 ssl->out_hdr += protected_record_size;
4558 ssl_update_out_pointers( ssl, ssl->transform_out );
4559
Hanno Becker04484622018-08-06 09:49:38 +01004560 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4561 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4562 break;
4563
4564 /* The loop goes to its end iff the counter is wrapping */
4565 if( i == ssl_ep_len( ssl ) )
4566 {
4567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4568 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4569 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004570 }
4571
Hanno Becker67bc7c32018-08-06 11:33:50 +01004572#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004573 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004574 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004575 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004576 size_t remaining;
4577 ret = ssl_get_remaining_payload_in_datagram( ssl );
4578 if( ret < 0 )
4579 {
4580 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4581 ret );
4582 return( ret );
4583 }
4584
4585 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004586 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004587 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004588 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004589 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004590 else
4591 {
Hanno Becker513815a2018-08-20 11:56:09 +01004592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004593 }
4594 }
4595#endif /* MBEDTLS_SSL_PROTO_DTLS */
4596
4597 if( ( flush == SSL_FORCE_FLUSH ) &&
4598 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 return( ret );
4602 }
4603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004605
4606 return( 0 );
4607}
4608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004610
4611static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4612{
4613 if( ssl->in_msglen < ssl->in_hslen ||
4614 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4615 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4616 {
4617 return( 1 );
4618 }
4619 return( 0 );
4620}
Hanno Becker44650b72018-08-16 12:51:11 +01004621
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004622static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004623{
4624 return( ( ssl->in_msg[9] << 16 ) |
4625 ( ssl->in_msg[10] << 8 ) |
4626 ssl->in_msg[11] );
4627}
4628
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004629static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004630{
4631 return( ( ssl->in_msg[6] << 16 ) |
4632 ( ssl->in_msg[7] << 8 ) |
4633 ssl->in_msg[8] );
4634}
4635
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004636static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004637{
4638 uint32_t msg_len, frag_off, frag_len;
4639
4640 msg_len = ssl_get_hs_total_len( ssl );
4641 frag_off = ssl_get_hs_frag_off( ssl );
4642 frag_len = ssl_get_hs_frag_len( ssl );
4643
4644 if( frag_off > msg_len )
4645 return( -1 );
4646
4647 if( frag_len > msg_len - frag_off )
4648 return( -1 );
4649
4650 if( frag_len + 12 > ssl->in_msglen )
4651 return( -1 );
4652
4653 return( 0 );
4654}
4655
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004656/*
4657 * Mark bits in bitmask (used for DTLS HS reassembly)
4658 */
4659static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4660{
4661 unsigned int start_bits, end_bits;
4662
4663 start_bits = 8 - ( offset % 8 );
4664 if( start_bits != 8 )
4665 {
4666 size_t first_byte_idx = offset / 8;
4667
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004668 /* Special case */
4669 if( len <= start_bits )
4670 {
4671 for( ; len != 0; len-- )
4672 mask[first_byte_idx] |= 1 << ( start_bits - len );
4673
4674 /* Avoid potential issues with offset or len becoming invalid */
4675 return;
4676 }
4677
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004678 offset += start_bits; /* Now offset % 8 == 0 */
4679 len -= start_bits;
4680
4681 for( ; start_bits != 0; start_bits-- )
4682 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4683 }
4684
4685 end_bits = len % 8;
4686 if( end_bits != 0 )
4687 {
4688 size_t last_byte_idx = ( offset + len ) / 8;
4689
4690 len -= end_bits; /* Now len % 8 == 0 */
4691
4692 for( ; end_bits != 0; end_bits-- )
4693 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4694 }
4695
4696 memset( mask + offset / 8, 0xFF, len / 8 );
4697}
4698
4699/*
4700 * Check that bitmask is full
4701 */
4702static int ssl_bitmask_check( unsigned char *mask, size_t len )
4703{
4704 size_t i;
4705
4706 for( i = 0; i < len / 8; i++ )
4707 if( mask[i] != 0xFF )
4708 return( -1 );
4709
4710 for( i = 0; i < len % 8; i++ )
4711 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4712 return( -1 );
4713
4714 return( 0 );
4715}
4716
Hanno Becker56e205e2018-08-16 09:06:12 +01004717/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004718static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004719 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004720{
Hanno Becker56e205e2018-08-16 09:06:12 +01004721 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004722
Hanno Becker56e205e2018-08-16 09:06:12 +01004723 alloc_len = 12; /* Handshake header */
4724 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004725
Hanno Beckerd07df862018-08-16 09:14:58 +01004726 if( add_bitmap )
4727 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004728
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004729 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004730}
Hanno Becker56e205e2018-08-16 09:06:12 +01004731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004733
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004734static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004735{
4736 return( ( ssl->in_msg[1] << 16 ) |
4737 ( ssl->in_msg[2] << 8 ) |
4738 ssl->in_msg[3] );
4739}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004740
Simon Butcher99000142016-10-13 17:21:01 +01004741int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004742{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004746 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004747 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004748 }
4749
Hanno Becker12555c62018-08-16 12:47:53 +01004750 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004753 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004754 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004757 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004758 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004759 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004760 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004761
Hanno Becker44650b72018-08-16 12:51:11 +01004762 if( ssl_check_hs_header( ssl ) != 0 )
4763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4765 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4766 }
4767
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004768 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004769 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4770 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4771 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4772 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004773 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004774 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4775 {
4776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4777 recv_msg_seq,
4778 ssl->handshake->in_msg_seq ) );
4779 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4780 }
4781
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004782 /* Retransmit only on last message from previous flight, to avoid
4783 * too many retransmissions.
4784 * Besides, No sane server ever retransmits HelloVerifyRequest */
4785 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004786 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004789 "message_seq = %d, start_of_flight = %d",
4790 recv_msg_seq,
4791 ssl->handshake->in_flight_start_seq ) );
4792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004796 return( ret );
4797 }
4798 }
4799 else
4800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004802 "message_seq = %d, expected = %d",
4803 recv_msg_seq,
4804 ssl->handshake->in_msg_seq ) );
4805 }
4806
Hanno Becker90333da2017-10-10 11:27:13 +01004807 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004808 }
4809 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004810
Hanno Becker6d97ef52018-08-16 13:09:04 +01004811 /* Message reassembly is handled alongside buffering of future
4812 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004813 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004814 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004815 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004818 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004819 }
4820 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004821 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004823#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004824 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004825 /* With TLS we don't handle fragmentation (for now) */
4826 if( ssl->in_msglen < ssl->in_hslen )
4827 {
4828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4829 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4830 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004831 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004832#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004833
Simon Butcher99000142016-10-13 17:21:01 +01004834 return( 0 );
4835}
4836
4837void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4838{
Hanno Becker0271f962018-08-16 13:23:47 +01004839 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004840
Hanno Becker0271f962018-08-16 13:23:47 +01004841 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Hanno Becker8a4b5902019-08-15 17:04:57 +01004842 mbedtls_ssl_update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004843
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004844 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004846 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004847 ssl->handshake != NULL )
4848 {
Hanno Becker0271f962018-08-16 13:23:47 +01004849 unsigned offset;
4850 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004851
Hanno Becker0271f962018-08-16 13:23:47 +01004852 /* Increment handshake sequence number */
4853 hs->in_msg_seq++;
4854
4855 /*
4856 * Clear up handshake buffering and reassembly structure.
4857 */
4858
4859 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004860 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004861
4862 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004863 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4864 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004865 offset++, hs_buf++ )
4866 {
4867 *hs_buf = *(hs_buf + 1);
4868 }
4869
4870 /* Create a fresh last entry */
4871 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004872 }
4873#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004874}
4875
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004876/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004877 * DTLS anti-replay: RFC 6347 4.1.2.6
4878 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004879 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4880 * Bit n is set iff record number in_window_top - n has been seen.
4881 *
4882 * Usually, in_window_top is the last record number seen and the lsb of
4883 * in_window is set. The only exception is the initial state (record number 0
4884 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4887static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004888{
4889 ssl->in_window_top = 0;
4890 ssl->in_window = 0;
4891}
4892
4893static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4894{
4895 return( ( (uint64_t) buf[0] << 40 ) |
4896 ( (uint64_t) buf[1] << 32 ) |
4897 ( (uint64_t) buf[2] << 24 ) |
4898 ( (uint64_t) buf[3] << 16 ) |
4899 ( (uint64_t) buf[4] << 8 ) |
4900 ( (uint64_t) buf[5] ) );
4901}
4902
4903/*
4904 * Return 0 if sequence number is acceptable, -1 otherwise
4905 */
Hanno Beckerfc551722019-07-12 08:50:37 +01004906int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004907{
4908 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4909 uint64_t bit;
4910
Hanno Becker7f376f42019-06-12 16:20:48 +01004911 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4912 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4913 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004914 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004915 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004916
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004917 if( rec_seqnum > ssl->in_window_top )
4918 return( 0 );
4919
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004920 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004921
4922 if( bit >= 64 )
4923 return( -1 );
4924
4925 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4926 return( -1 );
4927
4928 return( 0 );
4929}
4930
4931/*
4932 * Update replay window on new validated record
4933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004935{
4936 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4937
Hanno Becker7f376f42019-06-12 16:20:48 +01004938 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4939 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4940 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004941 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004942 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004943
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004944 if( rec_seqnum > ssl->in_window_top )
4945 {
4946 /* Update window_top and the contents of the window */
4947 uint64_t shift = rec_seqnum - ssl->in_window_top;
4948
4949 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004950 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004951 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004952 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004953 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004954 ssl->in_window |= 1;
4955 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004956
4957 ssl->in_window_top = rec_seqnum;
4958 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004959 else
4960 {
4961 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004962 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004963
4964 if( bit < 64 ) /* Always true, but be extra sure */
4965 ssl->in_window |= (uint64_t) 1 << bit;
4966 }
4967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004969
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004970#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004971/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004972static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4973
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004974/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004975 * Without any SSL context, check if a datagram looks like a ClientHello with
4976 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004977 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004978 *
4979 * - if cookie is valid, return 0
4980 * - if ClientHello looks superficially valid but cookie is not,
4981 * fill obuf and set olen, then
4982 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4983 * - otherwise return a specific error code
4984 */
4985static int ssl_check_dtls_clihlo_cookie(
4986 mbedtls_ssl_cookie_write_t *f_cookie_write,
4987 mbedtls_ssl_cookie_check_t *f_cookie_check,
4988 void *p_cookie,
4989 const unsigned char *cli_id, size_t cli_id_len,
4990 const unsigned char *in, size_t in_len,
4991 unsigned char *obuf, size_t buf_len, size_t *olen )
4992{
4993 size_t sid_len, cookie_len;
4994 unsigned char *p;
4995
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004996 /*
4997 * Structure of ClientHello with record and handshake headers,
4998 * and expected values. We don't need to check a lot, more checks will be
4999 * done when actually parsing the ClientHello - skipping those checks
5000 * avoids code duplication and does not make cookie forging any easier.
5001 *
5002 * 0-0 ContentType type; copied, must be handshake
5003 * 1-2 ProtocolVersion version; copied
5004 * 3-4 uint16 epoch; copied, must be 0
5005 * 5-10 uint48 sequence_number; copied
5006 * 11-12 uint16 length; (ignored)
5007 *
5008 * 13-13 HandshakeType msg_type; (ignored)
5009 * 14-16 uint24 length; (ignored)
5010 * 17-18 uint16 message_seq; copied
5011 * 19-21 uint24 fragment_offset; copied, must be 0
5012 * 22-24 uint24 fragment_length; (ignored)
5013 *
5014 * 25-26 ProtocolVersion client_version; (ignored)
5015 * 27-58 Random random; (ignored)
5016 * 59-xx SessionID session_id; 1 byte len + sid_len content
5017 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
5018 * ...
5019 *
5020 * Minimum length is 61 bytes.
5021 */
5022 if( in_len < 61 ||
5023 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
5024 in[3] != 0 || in[4] != 0 ||
5025 in[19] != 0 || in[20] != 0 || in[21] != 0 )
5026 {
5027 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5028 }
5029
5030 sid_len = in[59];
5031 if( sid_len > in_len - 61 )
5032 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5033
5034 cookie_len = in[60 + sid_len];
5035 if( cookie_len > in_len - 60 )
5036 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
5037
5038 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
5039 cli_id, cli_id_len ) == 0 )
5040 {
5041 /* Valid cookie */
5042 return( 0 );
5043 }
5044
5045 /*
5046 * If we get here, we've got an invalid cookie, let's prepare HVR.
5047 *
5048 * 0-0 ContentType type; copied
5049 * 1-2 ProtocolVersion version; copied
5050 * 3-4 uint16 epoch; copied
5051 * 5-10 uint48 sequence_number; copied
5052 * 11-12 uint16 length; olen - 13
5053 *
5054 * 13-13 HandshakeType msg_type; hello_verify_request
5055 * 14-16 uint24 length; olen - 25
5056 * 17-18 uint16 message_seq; copied
5057 * 19-21 uint24 fragment_offset; copied
5058 * 22-24 uint24 fragment_length; olen - 25
5059 *
5060 * 25-26 ProtocolVersion server_version; 0xfe 0xff
5061 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
5062 *
5063 * Minimum length is 28.
5064 */
5065 if( buf_len < 28 )
5066 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
5067
5068 /* Copy most fields and adapt others */
5069 memcpy( obuf, in, 25 );
5070 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
5071 obuf[25] = 0xfe;
5072 obuf[26] = 0xff;
5073
5074 /* Generate and write actual cookie */
5075 p = obuf + 28;
5076 if( f_cookie_write( p_cookie,
5077 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
5078 {
5079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5080 }
5081
5082 *olen = p - obuf;
5083
5084 /* Go back and fill length fields */
5085 obuf[27] = (unsigned char)( *olen - 28 );
5086
5087 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
5088 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
5089 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
5090
5091 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
5092 obuf[12] = (unsigned char)( ( *olen - 13 ) );
5093
5094 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
5095}
5096
5097/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005098 * Handle possible client reconnect with the same UDP quadruplet
5099 * (RFC 6347 Section 4.2.8).
5100 *
5101 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5102 * that looks like a ClientHello.
5103 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005104 * - if the input looks like a ClientHello without cookies,
5105 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005106 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005107 * - if the input looks like a ClientHello with a valid cookie,
5108 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005109 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005110 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005111 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005112 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005113 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5114 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005115 */
5116static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5117{
5118 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005119 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005120
Hanno Becker87b56262019-07-10 14:37:41 +01005121 if( ssl->conf->f_cookie_write == NULL ||
5122 ssl->conf->f_cookie_check == NULL )
5123 {
5124 /* If we can't use cookies to verify reachability of the peer,
5125 * drop the record. */
5126 return( 0 );
5127 }
5128
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005129 ret = ssl_check_dtls_clihlo_cookie(
5130 ssl->conf->f_cookie_write,
5131 ssl->conf->f_cookie_check,
5132 ssl->conf->p_cookie,
5133 ssl->cli_id, ssl->cli_id_len,
5134 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005135 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005136
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005137 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5138
5139 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005140 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005141 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005142 * If the error is permanent we'll catch it later,
5143 * if it's not, then hopefully it'll work next time. */
Hanno Beckera58a8962019-06-13 16:11:15 +01005144 (void) mbedtls_ssl_get_send( ssl )( ssl->p_bio, ssl->out_buf, len );
Hanno Becker87b56262019-07-10 14:37:41 +01005145 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005146 }
5147
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005148 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005149 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005150 /* Got a valid cookie, partially reset context */
5151 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5152 {
5153 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5154 return( ret );
5155 }
5156
5157 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005158 }
5159
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005160 return( ret );
5161}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005162#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005163
Hanno Becker46483f12019-05-03 13:25:54 +01005164static int ssl_check_record_type( uint8_t record_type )
5165{
5166 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5167 record_type != MBEDTLS_SSL_MSG_ALERT &&
5168 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5169 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5170 {
5171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5172 }
5173
5174 return( 0 );
5175}
5176
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005177/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005178 * ContentType type;
5179 * ProtocolVersion version;
5180 * uint16 epoch; // DTLS only
5181 * uint48 sequence_number; // DTLS only
5182 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005183 *
5184 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005185 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005186 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5187 *
5188 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005189 * 1. proceed with the record if this function returns 0
5190 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5191 * 3. return CLIENT_RECONNECT if this function return that value
5192 * 4. drop the whole datagram if this function returns anything else.
5193 * Point 2 is needed when the peer is resending, and we have already received
5194 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005195 */
Hanno Becker21fc61c2019-07-12 11:10:16 +01005196static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005197 unsigned char *buf,
5198 size_t len,
5199 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005200{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005201 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005202
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005203 size_t const rec_hdr_type_offset = 0;
5204 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005205
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005206 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5207 rec_hdr_type_len;
5208 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005210 size_t const rec_hdr_ctr_len = 8;
5211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker61817612019-07-25 10:13:02 +01005212 uint32_t rec_epoch;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005213 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5214 rec_hdr_version_len;
5215
Hanno Beckera5a2b082019-05-15 14:03:01 +01005216#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005217 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5218 rec_hdr_ctr_len;
Hanno Becker61817612019-07-25 10:13:02 +01005219 size_t rec_hdr_cid_len = 0;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005220#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5221#endif /* MBEDTLS_SSL_PROTO_DTLS */
5222
5223 size_t rec_hdr_len_offset; /* To be determined */
5224 size_t const rec_hdr_len_len = 2;
5225
5226 /*
5227 * Check minimum lengths for record header.
5228 */
5229
5230#if defined(MBEDTLS_SSL_PROTO_DTLS)
5231 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5232 {
5233 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5234 }
5235 MBEDTLS_SSL_TRANSPORT_ELSE
5236#endif /* MBEDTLS_SSL_PROTO_DTLS */
5237#if defined(MBEDTLS_SSL_PROTO_TLS)
5238 {
5239 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5240 }
5241#endif /* MBEDTLS_SSL_PROTO_DTLS */
5242
5243 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5244 {
5245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5246 (unsigned) len,
5247 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5248 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5249 }
5250
5251 /*
5252 * Parse and validate record content type
5253 */
5254
5255 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005256
5257 /* Check record content type */
5258#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5259 rec->cid_len = 0;
5260
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005261 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005262 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 &&
5263 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Becker8b09b732019-05-08 12:03:28 +01005264 {
5265 /* Shift pointers to account for record header including CID
5266 * struct {
5267 * ContentType special_type = tls12_cid;
5268 * ProtocolVersion version;
5269 * uint16 epoch;
5270 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01005271 * opaque cid[cid_length]; // Additional field compared to
5272 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01005273 * uint16 length;
5274 * opaque enc_content[DTLSCiphertext.length];
5275 * } DTLSCiphertext;
5276 */
5277
5278 /* So far, we only support static CID lengths
5279 * fixed in the configuration. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005280 rec_hdr_cid_len = mbedtls_ssl_conf_get_cid_len( ssl->conf );
5281 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005282
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005283 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005284 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5286 (unsigned) len,
5287 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker29823462019-07-10 14:53:43 +01005288 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckerde7d6d32019-07-10 14:50:10 +01005289 }
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005290
Manuel Pégourié-Gonnardf3a15b32019-08-02 10:17:15 +02005291 /* configured CID len is guaranteed at most 255, see
5292 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5293 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005294 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Becker8b09b732019-05-08 12:03:28 +01005295 }
5296 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01005297#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005298 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005299 if( ssl_check_record_type( rec->type ) )
5300 {
Hanno Becker03e2db62019-07-12 14:40:00 +01005301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5302 (unsigned) rec->type ) );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005303 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5304 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005305 }
5306
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005307 /*
5308 * Parse and validate record version
5309 */
5310
Hanno Becker8061c6e2019-07-26 08:07:03 +01005311 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5312 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005313 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5314 ssl->conf->transport,
Hanno Becker8061c6e2019-07-26 08:07:03 +01005315 &rec->ver[0] );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005316
Hanno Becker2881d802019-05-22 14:44:53 +01005317 if( major_ver != mbedtls_ssl_get_major_ver( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5320 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 }
5322
Hanno Beckere965bd32019-06-12 14:04:34 +01005323 if( minor_ver > mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5326 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 }
5328
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005329 /*
5330 * Parse/Copy record sequence number.
5331 */
Hanno Becker8b09b732019-05-08 12:03:28 +01005332
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005333#if defined(MBEDTLS_SSL_PROTO_DTLS)
5334 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
5335 {
5336 /* Copy explicit record sequence number from input buffer. */
5337 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5338 rec_hdr_ctr_len );
5339 }
5340 MBEDTLS_SSL_TRANSPORT_ELSE
5341#endif /* MBEDTLS_SSL_PROTO_DTLS */
5342#if defined(MBEDTLS_SSL_PROTO_TLS)
5343 {
5344 /* Copy implicit record sequence number from SSL context structure. */
5345 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5346 }
5347#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker8b09b732019-05-08 12:03:28 +01005348
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005349 /*
5350 * Parse record length.
5351 */
5352
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005353 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker7b5ba842019-07-25 10:16:37 +01005354 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5355 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005356 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5357
Hanno Becker8b09b732019-05-08 12:03:28 +01005358 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01005359 "version = [%d:%d], msglen = %d",
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005360 rec->type,
5361 major_ver, minor_ver, rec->data_len ) );
5362
5363 rec->buf = buf;
5364 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Becker8b09b732019-05-08 12:03:28 +01005365
Hanno Beckerec014082019-07-26 08:20:27 +01005366 if( rec->data_len == 0 )
5367 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5368
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005369 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005370 * DTLS-related tests.
5371 * Check epoch before checking length constraint because
5372 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5373 * message gets duplicated before the corresponding Finished message,
5374 * the second ChangeCipherSpec should be discarded because it belongs
5375 * to an old epoch, but not because its length is shorter than
5376 * the minimum record length for packets using the new record transform.
5377 * Note that these two kinds of failures are handled differently,
5378 * as an unexpected record is silently skipped but an invalid
5379 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005380 */
5381#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005382 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005383 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005384 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005385
Hanno Beckere0452772019-07-10 17:12:07 +01005386 /* Check that the datagram is large enough to contain a record
5387 * of the advertised length. */
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005388 if( len < rec->data_offset + rec->data_len )
Hanno Beckere0452772019-07-10 17:12:07 +01005389 {
Hanno Beckerc6e7c572019-07-11 12:29:35 +01005390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5391 (unsigned) len,
5392 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Beckere0452772019-07-10 17:12:07 +01005393 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5394 }
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005395
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005396 /* Records from other, non-matching epochs are silently discarded.
5397 * (The case of same-port Client reconnects must be considered in
5398 * the caller). */
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005399 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005400 {
5401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5402 "expected %d, received %d",
5403 ssl->in_epoch, rec_epoch ) );
Hanno Beckerc1c173c2019-07-19 10:59:12 +01005404
5405 /* Records from the next epoch are considered for buffering
5406 * (concretely: early Finished messages). */
5407 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5408 {
5409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5410 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5411 }
5412
Hanno Becker87b56262019-07-10 14:37:41 +01005413 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005414 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005415#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker6c0e53c2019-07-10 17:20:01 +01005416 /* For records from the correct epoch, check whether their
5417 * sequence number has been seen before. */
Hanno Becker87b56262019-07-10 14:37:41 +01005418 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005419 {
5420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5421 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5422 }
5423#endif
5424 }
5425#endif /* MBEDTLS_SSL_PROTO_DTLS */
5426
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005427 return( 0 );
5428}
Paul Bakker5121ce52009-01-03 21:22:43 +00005429
Hanno Becker87b56262019-07-10 14:37:41 +01005430
5431#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5432static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5433{
5434 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5435
5436 /*
5437 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5438 * access the first byte of record content (handshake type), as we
5439 * have an active transform (possibly iv_len != 0), so use the
5440 * fact that the record header len is 13 instead.
5441 */
5442 if( rec_epoch == 0 &&
5443 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
5444 MBEDTLS_SSL_IS_SERVER &&
5445 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5446 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5447 ssl->in_left > 13 &&
5448 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5449 {
5450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5451 "from the same port" ) );
5452 return( ssl_handle_possible_reconnect( ssl ) );
5453 }
5454
5455 return( 0 );
5456}
5457#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5458
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459/*
5460 * If applicable, decrypt (and decompress) record content
5461 */
Hanno Beckera89610a2019-07-11 13:07:45 +01005462static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5463 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005464{
5465 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckera89610a2019-07-11 13:07:45 +01005468 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5471 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 ret = mbedtls_ssl_hw_record_read( ssl );
5476 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5479 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005480 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005481
5482 if( ret == 0 )
5483 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005486 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005487 {
Hanno Becker106f3da2019-07-12 09:35:58 +01005488 unsigned char const old_msg_type = rec->type;
5489
Hanno Becker611a83b2018-01-03 14:27:32 +00005490 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckera89610a2019-07-11 13:07:45 +01005491 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005494
Hanno Beckera5a2b082019-05-15 14:03:01 +01005495#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005496 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01005497 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
5498 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005499 {
Hanno Becker675c4d62019-05-24 10:11:06 +01005500 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01005501 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01005502 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005503#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01005504
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 return( ret );
5506 }
5507
Hanno Becker106f3da2019-07-12 09:35:58 +01005508 if( old_msg_type != rec->type )
Hanno Becker93012fe2018-08-07 14:30:18 +01005509 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005510 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker106f3da2019-07-12 09:35:58 +01005511 old_msg_type, rec->type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01005512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005513
Paul Bakker5121ce52009-01-03 21:22:43 +00005514 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker106f3da2019-07-12 09:35:58 +01005515 rec->buf + rec->data_offset, rec->data_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005516
Hanno Beckera5a2b082019-05-15 14:03:01 +01005517#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005518 /* We have already checked the record content type
5519 * in ssl_parse_record_header(), failing or silently
5520 * dropping the record in the case of an unknown type.
5521 *
5522 * Since with the use of CIDs, the record content type
5523 * might change during decryption, re-check the record
5524 * content type, but treat a failure as fatal this time. */
Hanno Becker106f3da2019-07-12 09:35:58 +01005525 if( ssl_check_record_type( rec->type ) )
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005526 {
5527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5528 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5529 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01005530#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01005531
Hanno Becker106f3da2019-07-12 09:35:58 +01005532 if( rec->data_len == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005533 {
5534#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01005535 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker106f3da2019-07-12 09:35:58 +01005536 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005537 {
5538 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5540 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5541 }
5542#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5543
5544 ssl->nb_zero++;
5545
5546 /*
5547 * Three or more empty messages may be a DoS attack
5548 * (excessive CPU consumption).
5549 */
5550 if( ssl->nb_zero > 3 )
5551 {
5552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01005553 "messages, possible DoS attack" ) );
5554 /* Treat the records as if they were not properly authenticated,
5555 * thereby failing the connection if we see more than allowed
5556 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005557 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5558 }
5559 }
5560 else
5561 ssl->nb_zero = 0;
5562
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005563 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
5564#if defined(MBEDTLS_SSL_PROTO_TLS)
5565 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005566 {
5567 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005568 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005569 if( ++ssl->in_ctr[i - 1] != 0 )
5570 break;
5571
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02005572 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005573 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00005574 {
5575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5576 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5577 }
5578 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005579#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00005580
Paul Bakker5121ce52009-01-03 21:22:43 +00005581 }
5582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005584 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005586 {
5587 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005590 return( ret );
5591 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005592 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005596 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005599 }
5600#endif
5601
Hanno Beckerf0242852019-07-09 17:30:02 +01005602 /* Check actual (decrypted) record content length against
5603 * configured maximum. */
5604 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5605 {
5606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5607 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5608 }
5609
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005610 return( 0 );
5611}
5612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005614
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005615/*
5616 * Read a record.
5617 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005618 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5619 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5620 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005621 */
Hanno Becker1097b342018-08-15 14:09:41 +01005622
5623/* Helper functions for mbedtls_ssl_read_record(). */
5624static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005625static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5626static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005627
Hanno Becker327c93b2018-08-15 13:56:18 +01005628int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005629 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005630{
5631 int ret;
5632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005634
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005635 if( ssl->keep_current_message == 0 )
5636 {
5637 do {
Simon Butcher99000142016-10-13 17:21:01 +01005638
Hanno Becker26994592018-08-15 14:14:59 +01005639 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005640 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005641 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005642
Hanno Beckere74d5562018-08-15 14:26:08 +01005643 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005644 {
Hanno Becker40f50842018-08-15 14:48:01 +01005645#if defined(MBEDTLS_SSL_PROTO_DTLS)
5646 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005647
Hanno Becker40f50842018-08-15 14:48:01 +01005648 /* We only check for buffered messages if the
5649 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005650 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005651 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005652 {
Hanno Becker40f50842018-08-15 14:48:01 +01005653 if( ssl_load_buffered_message( ssl ) == 0 )
5654 have_buffered = 1;
5655 }
5656
5657 if( have_buffered == 0 )
5658#endif /* MBEDTLS_SSL_PROTO_DTLS */
5659 {
5660 ret = ssl_get_next_record( ssl );
5661 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5662 continue;
5663
5664 if( ret != 0 )
5665 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005666 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker42a6b042019-07-26 07:25:20 +01005667 ssl_send_pending_fatal_alert( ssl );
Hanno Becker40f50842018-08-15 14:48:01 +01005668 return( ret );
5669 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005670 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005671 }
5672
5673 ret = mbedtls_ssl_handle_message_type( ssl );
5674
Hanno Becker40f50842018-08-15 14:48:01 +01005675#if defined(MBEDTLS_SSL_PROTO_DTLS)
5676 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5677 {
5678 /* Buffer future message */
5679 ret = ssl_buffer_message( ssl );
5680 if( ret != 0 )
5681 return( ret );
5682
5683 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5684 }
5685#endif /* MBEDTLS_SSL_PROTO_DTLS */
5686
Hanno Becker90333da2017-10-10 11:27:13 +01005687 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5688 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005689
5690 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005691 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005692 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005693 return( ret );
5694 }
5695
Hanno Becker327c93b2018-08-15 13:56:18 +01005696 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005697 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005698 {
5699 mbedtls_ssl_update_handshake_status( ssl );
5700 }
Simon Butcher99000142016-10-13 17:21:01 +01005701 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005702 else
Simon Butcher99000142016-10-13 17:21:01 +01005703 {
Hanno Becker02f59072018-08-15 14:00:24 +01005704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005705 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005706 }
5707
5708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5709
5710 return( 0 );
5711}
5712
Hanno Becker40f50842018-08-15 14:48:01 +01005713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005714static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005715{
Hanno Becker40f50842018-08-15 14:48:01 +01005716 if( ssl->in_left > ssl->next_record_offset )
5717 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005718
Hanno Becker40f50842018-08-15 14:48:01 +01005719 return( 0 );
5720}
5721
5722static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5723{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005724 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005725 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005726 int ret = 0;
5727
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005728 if( hs == NULL )
5729 return( -1 );
5730
Hanno Beckere00ae372018-08-20 09:39:42 +01005731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5732
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005733 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5734 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5735 {
5736 /* Check if we have seen a ChangeCipherSpec before.
5737 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005738 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005739 {
5740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5741 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005742 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005743 }
5744
Hanno Becker39b8bc92018-08-28 17:17:13 +01005745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005746 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5747 ssl->in_msglen = 1;
5748 ssl->in_msg[0] = 1;
5749
5750 /* As long as they are equal, the exact value doesn't matter. */
5751 ssl->in_left = 0;
5752 ssl->next_record_offset = 0;
5753
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005754 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005755 goto exit;
5756 }
Hanno Becker37f95322018-08-16 13:55:32 +01005757
Hanno Beckerb8f50142018-08-28 10:01:34 +01005758#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005759 /* Debug only */
5760 {
5761 unsigned offset;
5762 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5763 {
5764 hs_buf = &hs->buffering.hs[offset];
5765 if( hs_buf->is_valid == 1 )
5766 {
5767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5768 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005769 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005770 }
5771 }
5772 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005773#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005774
5775 /* Check if we have buffered and/or fully reassembled the
5776 * next handshake message. */
5777 hs_buf = &hs->buffering.hs[0];
5778 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5779 {
5780 /* Synthesize a record containing the buffered HS message. */
5781 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5782 ( hs_buf->data[2] << 8 ) |
5783 hs_buf->data[3];
5784
5785 /* Double-check that we haven't accidentally buffered
5786 * a message that doesn't fit into the input buffer. */
5787 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5788 {
5789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5790 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5791 }
5792
5793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5794 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5795 hs_buf->data, msg_len + 12 );
5796
5797 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5798 ssl->in_hslen = msg_len + 12;
5799 ssl->in_msglen = msg_len + 12;
5800 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5801
5802 ret = 0;
5803 goto exit;
5804 }
5805 else
5806 {
5807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5808 hs->in_msg_seq ) );
5809 }
5810
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005811 ret = -1;
5812
5813exit:
5814
5815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5816 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005817}
5818
Hanno Beckera02b0b42018-08-21 17:20:27 +01005819static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5820 size_t desired )
5821{
5822 int offset;
5823 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5825 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005826
Hanno Becker01315ea2018-08-21 17:22:17 +01005827 /* Get rid of future records epoch first, if such exist. */
5828 ssl_free_buffered_record( ssl );
5829
5830 /* Check if we have enough space available now. */
5831 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5832 hs->buffering.total_bytes_buffered ) )
5833 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005835 return( 0 );
5836 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005837
Hanno Becker4f432ad2018-08-28 10:02:32 +01005838 /* We don't have enough space to buffer the next expected handshake
5839 * message. Remove buffers used for future messages to gain space,
5840 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005841 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5842 offset >= 0; offset-- )
5843 {
5844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5845 offset ) );
5846
Hanno Beckerb309b922018-08-23 13:18:05 +01005847 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005848
5849 /* Check if we have enough space available now. */
5850 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5851 hs->buffering.total_bytes_buffered ) )
5852 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005853 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005854 return( 0 );
5855 }
5856 }
5857
5858 return( -1 );
5859}
5860
Hanno Becker40f50842018-08-15 14:48:01 +01005861static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5862{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863 int ret = 0;
5864 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5865
5866 if( hs == NULL )
5867 return( 0 );
5868
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5870
5871 switch( ssl->in_msgtype )
5872 {
5873 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005875
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005876 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005877 break;
5878
5879 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005880 {
5881 unsigned recv_msg_seq_offset;
5882 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5883 mbedtls_ssl_hs_buffer *hs_buf;
5884 size_t msg_len = ssl->in_hslen - 12;
5885
5886 /* We should never receive an old handshake
5887 * message - double-check nonetheless. */
5888 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5889 {
5890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5892 }
5893
5894 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5895 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5896 {
5897 /* Silently ignore -- message too far in the future */
5898 MBEDTLS_SSL_DEBUG_MSG( 2,
5899 ( "Ignore future HS message with sequence number %u, "
5900 "buffering window %u - %u",
5901 recv_msg_seq, ssl->handshake->in_msg_seq,
5902 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5903
5904 goto exit;
5905 }
5906
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5908 recv_msg_seq, recv_msg_seq_offset ) );
5909
5910 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5911
5912 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005913 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005914 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005915 size_t reassembly_buf_sz;
5916
Hanno Becker37f95322018-08-16 13:55:32 +01005917 hs_buf->is_fragmented =
5918 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5919
5920 /* We copy the message back into the input buffer
5921 * after reassembly, so check that it's not too large.
5922 * This is an implementation-specific limitation
5923 * and not one from the standard, hence it is not
5924 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005925 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005926 {
5927 /* Ignore message */
5928 goto exit;
5929 }
5930
Hanno Beckere0b150f2018-08-21 15:51:03 +01005931 /* Check if we have enough space to buffer the message. */
5932 if( hs->buffering.total_bytes_buffered >
5933 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5934 {
5935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5937 }
5938
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005939 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5940 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005941
5942 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5943 hs->buffering.total_bytes_buffered ) )
5944 {
5945 if( recv_msg_seq_offset > 0 )
5946 {
5947 /* If we can't buffer a future message because
5948 * of space limitations -- ignore. */
5949 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",
5950 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5951 (unsigned) hs->buffering.total_bytes_buffered ) );
5952 goto exit;
5953 }
Hanno Beckere1801392018-08-21 16:51:05 +01005954 else
5955 {
5956 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",
5957 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5958 (unsigned) hs->buffering.total_bytes_buffered ) );
5959 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005960
Hanno Beckera02b0b42018-08-21 17:20:27 +01005961 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005962 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005963 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",
5964 (unsigned) msg_len,
5965 (unsigned) reassembly_buf_sz,
5966 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005967 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005968 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5969 goto exit;
5970 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005971 }
5972
5973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5974 msg_len ) );
5975
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005976 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5977 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005978 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005979 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005980 goto exit;
5981 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005982 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005983
5984 /* Prepare final header: copy msg_type, length and message_seq,
5985 * then add standardised fragment_offset and fragment_length */
5986 memcpy( hs_buf->data, ssl->in_msg, 6 );
5987 memset( hs_buf->data + 6, 0, 3 );
5988 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5989
5990 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005991
5992 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005993 }
5994 else
5995 {
5996 /* Make sure msg_type and length are consistent */
5997 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5998 {
5999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
6000 /* Ignore */
6001 goto exit;
6002 }
6003 }
6004
Hanno Becker4422bbb2018-08-20 09:40:19 +01006005 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01006006 {
6007 size_t frag_len, frag_off;
6008 unsigned char * const msg = hs_buf->data + 12;
6009
6010 /*
6011 * Check and copy current fragment
6012 */
6013
6014 /* Validation of header fields already done in
6015 * mbedtls_ssl_prepare_handshake_record(). */
6016 frag_off = ssl_get_hs_frag_off( ssl );
6017 frag_len = ssl_get_hs_frag_len( ssl );
6018
6019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
6020 frag_off, frag_len ) );
6021 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
6022
6023 if( hs_buf->is_fragmented )
6024 {
6025 unsigned char * const bitmask = msg + msg_len;
6026 ssl_bitmask_set( bitmask, frag_off, frag_len );
6027 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
6028 msg_len ) == 0 );
6029 }
6030 else
6031 {
6032 hs_buf->is_complete = 1;
6033 }
6034
6035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
6036 hs_buf->is_complete ? "" : "not yet " ) );
6037 }
6038
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006039 break;
Hanno Becker37f95322018-08-16 13:55:32 +01006040 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006041
6042 default:
Hanno Becker360bef32018-08-28 10:04:33 +01006043 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006044 break;
6045 }
6046
6047exit:
6048
6049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
6050 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01006051}
6052#endif /* MBEDTLS_SSL_PROTO_DTLS */
6053
Hanno Becker1097b342018-08-15 14:09:41 +01006054static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006055{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006056 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01006057 * Consume last content-layer message and potentially
6058 * update in_msglen which keeps track of the contents'
6059 * consumption state.
6060 *
6061 * (1) Handshake messages:
6062 * Remove last handshake message, move content
6063 * and adapt in_msglen.
6064 *
6065 * (2) Alert messages:
6066 * Consume whole record content, in_msglen = 0.
6067 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01006068 * (3) Change cipher spec:
6069 * Consume whole record content, in_msglen = 0.
6070 *
6071 * (4) Application data:
6072 * Don't do anything - the record layer provides
6073 * the application data as a stream transport
6074 * and consumes through mbedtls_ssl_read only.
6075 *
6076 */
6077
6078 /* Case (1): Handshake messages */
6079 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006080 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006081 /* Hard assertion to be sure that no application data
6082 * is in flight, as corrupting ssl->in_msglen during
6083 * ssl->in_offt != NULL is fatal. */
6084 if( ssl->in_offt != NULL )
6085 {
6086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6088 }
6089
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006090 /*
6091 * Get next Handshake message in the current record
6092 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006093
Hanno Becker4a810fb2017-05-24 16:27:30 +01006094 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01006095 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01006096 * current handshake content: If DTLS handshake
6097 * fragmentation is used, that's the fragment
6098 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01006099 * size here is faulty and should be changed at
6100 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006101 * (2) While it doesn't seem to cause problems, one
6102 * has to be very careful not to assume that in_hslen
6103 * is always <= in_msglen in a sensible communication.
6104 * Again, it's wrong for DTLS handshake fragmentation.
6105 * The following check is therefore mandatory, and
6106 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006107 * Additionally, ssl->in_hslen might be arbitrarily out of
6108 * bounds after handling a DTLS message with an unexpected
6109 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006110 */
6111 if( ssl->in_hslen < ssl->in_msglen )
6112 {
6113 ssl->in_msglen -= ssl->in_hslen;
6114 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6115 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006116
Hanno Becker4a810fb2017-05-24 16:27:30 +01006117 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6118 ssl->in_msg, ssl->in_msglen );
6119 }
6120 else
6121 {
6122 ssl->in_msglen = 0;
6123 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006124
Hanno Becker4a810fb2017-05-24 16:27:30 +01006125 ssl->in_hslen = 0;
6126 }
6127 /* Case (4): Application data */
6128 else if( ssl->in_offt != NULL )
6129 {
6130 return( 0 );
6131 }
6132 /* Everything else (CCS & Alerts) */
6133 else
6134 {
6135 ssl->in_msglen = 0;
6136 }
6137
Hanno Becker1097b342018-08-15 14:09:41 +01006138 return( 0 );
6139}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006140
Hanno Beckere74d5562018-08-15 14:26:08 +01006141static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6142{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006143 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006144 return( 1 );
6145
6146 return( 0 );
6147}
6148
Hanno Becker5f066e72018-08-16 14:56:31 +01006149#if defined(MBEDTLS_SSL_PROTO_DTLS)
6150
6151static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6152{
6153 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6154 if( hs == NULL )
6155 return;
6156
Hanno Becker01315ea2018-08-21 17:22:17 +01006157 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006158 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006159 hs->buffering.total_bytes_buffered -=
6160 hs->buffering.future_record.len;
6161
6162 mbedtls_free( hs->buffering.future_record.data );
6163 hs->buffering.future_record.data = NULL;
6164 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006165}
6166
6167static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6168{
6169 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6170 unsigned char * rec;
6171 size_t rec_len;
6172 unsigned rec_epoch;
6173
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006174 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01006175 return( 0 );
6176
6177 if( hs == NULL )
6178 return( 0 );
6179
Hanno Becker5f066e72018-08-16 14:56:31 +01006180 rec = hs->buffering.future_record.data;
6181 rec_len = hs->buffering.future_record.len;
6182 rec_epoch = hs->buffering.future_record.epoch;
6183
6184 if( rec == NULL )
6185 return( 0 );
6186
Hanno Becker4cb782d2018-08-20 11:19:05 +01006187 /* Only consider loading future records if the
6188 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006189 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006190 return( 0 );
6191
Hanno Becker5f066e72018-08-16 14:56:31 +01006192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6193
6194 if( rec_epoch != ssl->in_epoch )
6195 {
6196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6197 goto exit;
6198 }
6199
6200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6201
6202 /* Double-check that the record is not too large */
6203 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6204 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6205 {
6206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6208 }
6209
6210 memcpy( ssl->in_hdr, rec, rec_len );
6211 ssl->in_left = rec_len;
6212 ssl->next_record_offset = 0;
6213
6214 ssl_free_buffered_record( ssl );
6215
6216exit:
6217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6218 return( 0 );
6219}
6220
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006221static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6222 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006223{
6224 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006225
6226 /* Don't buffer future records outside handshakes. */
6227 if( hs == NULL )
6228 return( 0 );
6229
6230 /* Only buffer handshake records (we are only interested
6231 * in Finished messages). */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006232 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006233 return( 0 );
6234
6235 /* Don't buffer more than one future epoch record. */
6236 if( hs->buffering.future_record.data != NULL )
6237 return( 0 );
6238
Hanno Becker01315ea2018-08-21 17:22:17 +01006239 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006240 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006241 hs->buffering.total_bytes_buffered ) )
6242 {
6243 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 +01006244 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006245 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006246 return( 0 );
6247 }
6248
Hanno Becker5f066e72018-08-16 14:56:31 +01006249 /* Buffer record */
6250 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6251 ssl->in_epoch + 1 ) );
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006252 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006253
6254 /* ssl_parse_record_header() only considers records
6255 * of the next epoch as candidates for buffering. */
6256 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006257 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006258
6259 hs->buffering.future_record.data =
6260 mbedtls_calloc( 1, hs->buffering.future_record.len );
6261 if( hs->buffering.future_record.data == NULL )
6262 {
6263 /* If we run out of RAM trying to buffer a
6264 * record from the next epoch, just ignore. */
6265 return( 0 );
6266 }
6267
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006268 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006269
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006270 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006271 return( 0 );
6272}
6273
6274#endif /* MBEDTLS_SSL_PROTO_DTLS */
6275
Hanno Beckere74d5562018-08-15 14:26:08 +01006276static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006277{
6278 int ret;
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006279 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006280
Hanno Becker5f066e72018-08-16 14:56:31 +01006281#if defined(MBEDTLS_SSL_PROTO_DTLS)
6282 /* We might have buffered a future record; if so,
6283 * and if the epoch matches now, load it.
6284 * On success, this call will set ssl->in_left to
6285 * the length of the buffered record, so that
6286 * the calls to ssl_fetch_input() below will
6287 * essentially be no-ops. */
6288 ret = ssl_load_buffered_record( ssl );
6289 if( ret != 0 )
6290 return( ret );
6291#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006292
Hanno Becker8b09b732019-05-08 12:03:28 +01006293 /* Ensure that we have enough space available for the default form
6294 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6295 * with no space for CIDs counted in). */
6296 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6297 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006300 return( ret );
6301 }
6302
Hanno Beckerc6e7c572019-07-11 12:29:35 +01006303 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6304 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker87b56262019-07-10 14:37:41 +01006307 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006308 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006309 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6310 {
Hanno Beckeraf5bcfc2019-07-11 12:43:20 +01006311 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006312 if( ret != 0 )
6313 return( ret );
6314
6315 /* Fall through to handling of unexpected records */
6316 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6317 }
6318
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006319 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6320 {
Hanno Becker87b56262019-07-10 14:37:41 +01006321#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker68379722019-07-12 09:23:47 +01006322 /* Reset in pointers to default state for TLS/DTLS records,
6323 * assuming no CID and no offset between record content and
6324 * record plaintext. */
6325 ssl_update_in_pointers( ssl );
6326
Hanno Becker69412452019-07-12 08:33:49 +01006327 /* Setup internal message pointers from record structure. */
6328 ssl->in_msgtype = rec.type;
6329#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6330 ssl->in_len = ssl->in_cid + rec.cid_len;
6331#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006332 ssl->in_msg = ssl->in_len + 2;
Hanno Becker69412452019-07-12 08:33:49 +01006333 ssl->in_msglen = rec.data_len;
6334
Hanno Becker87b56262019-07-10 14:37:41 +01006335 ret = ssl_check_client_reconnect( ssl );
6336 if( ret != 0 )
6337 return( ret );
6338#endif
6339
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006340 /* Skip unexpected record (but not whole datagram) */
Hanno Becker2528ee02019-07-11 12:48:53 +01006341 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006342
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6344 "(header)" ) );
6345 }
6346 else
6347 {
6348 /* Skip invalid record and the rest of the datagram */
6349 ssl->next_record_offset = 0;
6350 ssl->in_left = 0;
6351
6352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6353 "(header)" ) );
6354 }
6355
6356 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006357 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006358 }
Hanno Becker87b56262019-07-10 14:37:41 +01006359 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006360#endif
Hanno Becker87b56262019-07-10 14:37:41 +01006361 {
6362 return( ret );
6363 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006364 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006366#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006367 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01006368 {
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006369 /* Remember offset of next record within datagram. */
Hanno Becker2720f4c2019-07-11 12:50:10 +01006370 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006371 if( ssl->next_record_offset < ssl->in_left )
6372 {
6373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6374 }
6375 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006376 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006377#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006378#if defined(MBEDTLS_SSL_PROTO_TLS)
6379 {
Hanno Beckere0452772019-07-10 17:12:07 +01006380 /*
6381 * Fetch record contents from underlying transport.
6382 */
Hanno Becker9babbf72019-07-11 12:50:29 +01006383 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006384 if( ret != 0 )
6385 {
6386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6387 return( ret );
6388 }
6389
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006390 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006391 }
Hanno Beckerdc4d6272019-07-10 15:01:45 +01006392#endif /* MBEDTLS_SSL_PROTO_TLS */
6393
6394 /*
6395 * Decrypt record contents.
6396 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006397
Hanno Beckera89610a2019-07-11 13:07:45 +01006398 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006401 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006402 {
6403 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01006404 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006405 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006406 /* Except when waiting for Finished as a bad mac here
6407 * probably means something went wrong in the handshake
6408 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6409 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6410 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6411 {
6412#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6413 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6414 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006415 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006416 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6417 }
6418#endif
6419 return( ret );
6420 }
6421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01006423 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
6424 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6427 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006428 }
6429#endif
6430
Hanno Becker4a810fb2017-05-24 16:27:30 +01006431 /* As above, invalid records cause
6432 * dismissal of the whole datagram. */
6433
6434 ssl->next_record_offset = 0;
6435 ssl->in_left = 0;
6436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006438 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006439 }
6440
6441 return( ret );
6442 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006443 MBEDTLS_SSL_TRANSPORT_ELSE
6444#endif /* MBEDTLS_SSL_PROTO_DTLS */
6445#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006446 {
6447 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6449 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006450 {
Hanno Beckerde62da92019-07-24 13:23:50 +01006451 mbedtls_ssl_pend_fatal_alert( ssl,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006453 }
6454#endif
6455 return( ret );
6456 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006457#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006458 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006459
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006460
6461 /* Reset in pointers to default state for TLS/DTLS records,
6462 * assuming no CID and no offset between record content and
6463 * record plaintext. */
6464 ssl_update_in_pointers( ssl );
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006465#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6466 ssl->in_len = ssl->in_cid + rec.cid_len;
6467#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01006468 ssl->in_msg = ssl->in_len + 2;
Hanno Beckerbd70c8e2019-07-12 09:40:44 +01006469
Hanno Beckerbf256cd2019-07-12 09:37:30 +01006470 /* The record content type may change during decryption,
6471 * so re-read it. */
6472 ssl->in_msgtype = rec.type;
6473 /* Also update the input buffer, because unfortunately
6474 * the server-side ssl_parse_client_hello() reparses the
6475 * record header when receiving a ClientHello initiating
6476 * a renegotiation. */
6477 ssl->in_hdr[0] = rec.type;
6478 ssl->in_msg = rec.buf + rec.data_offset;
6479 ssl->in_msglen = rec.data_len;
6480 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6481 ssl->in_len[1] = (unsigned char)( rec.data_len );
6482
Simon Butcher99000142016-10-13 17:21:01 +01006483 return( 0 );
6484}
6485
6486int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6487{
6488 int ret;
6489
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006490 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006491 * Handle particular types of records
6492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 {
Simon Butcher99000142016-10-13 17:21:01 +01006495 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6496 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006497 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006499 }
6500
Hanno Beckere678eaa2018-08-21 14:57:46 +01006501 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006502 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006503 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006504 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6506 ssl->in_msglen ) );
6507 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006508 }
6509
Hanno Beckere678eaa2018-08-21 14:57:46 +01006510 if( ssl->in_msg[0] != 1 )
6511 {
6512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6513 ssl->in_msg[0] ) );
6514 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6515 }
6516
6517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006518 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01006519 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6520 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6521 {
6522 if( ssl->handshake == NULL )
6523 {
6524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6525 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6526 }
6527
6528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6529 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6530 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006531#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006532 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006536 if( ssl->in_msglen != 2 )
6537 {
6538 /* Note: Standard allows for more than one 2 byte alert
6539 to be packed in a single message, but Mbed TLS doesn't
6540 currently support this. */
6541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6542 ssl->in_msglen ) );
6543 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6544 }
6545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 ssl->in_msg[0], ssl->in_msg[1] ) );
6548
6549 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006550 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006555 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006556 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006557 }
6558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6560 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6563 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006564 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006565
6566#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6567 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6568 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6569 {
Hanno Becker90333da2017-10-10 11:27:13 +01006570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006571 /* Will be handled when trying to parse ServerHello */
6572 return( 0 );
6573 }
6574#endif
6575
6576#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2881d802019-05-22 14:44:53 +01006577 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006578 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6579 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006580 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6581 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6582 {
6583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6584 /* Will be handled in mbedtls_ssl_parse_certificate() */
6585 return( 0 );
6586 }
6587#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6588
6589 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006590 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 }
6592
Hanno Beckerc76c6192017-06-06 10:03:17 +01006593#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02006594 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006595 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01006596 /* Drop unexpected ApplicationData records,
6597 * except at the beginning of renegotiations */
6598 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6599 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6600#if defined(MBEDTLS_SSL_RENEGOTIATION)
6601 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6602 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006603#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01006604 )
6605 {
6606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6607 return( MBEDTLS_ERR_SSL_NON_FATAL );
6608 }
6609
6610 if( ssl->handshake != NULL &&
6611 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6612 {
6613 ssl_handshake_wrapup_free_hs_transform( ssl );
6614 }
6615 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01006616#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006617
Paul Bakker5121ce52009-01-03 21:22:43 +00006618 return( 0 );
6619}
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006622{
6623 int ret;
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6626 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6627 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006628 {
6629 return( ret );
6630 }
6631
6632 return( 0 );
6633}
6634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006635int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Hanno Becker1facd552019-07-03 13:57:23 +01006636 unsigned char level,
6637 unsigned char message )
Paul Bakker0a925182012-04-16 06:46:41 +00006638{
6639 int ret;
6640
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006641 if( ssl == NULL || ssl->conf == NULL )
6642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006645 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006648 ssl->out_msglen = 2;
6649 ssl->out_msg[0] = level;
6650 ssl->out_msg[1] = message;
6651
Hanno Becker67bc7c32018-08-06 11:33:50 +01006652 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006655 return( ret );
6656 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006658
6659 return( 0 );
6660}
6661
Hanno Becker17572472019-02-08 07:19:04 +00006662#if defined(MBEDTLS_X509_CRT_PARSE_C)
6663static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6664{
6665#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6666 if( session->peer_cert != NULL )
6667 {
6668 mbedtls_x509_crt_free( session->peer_cert );
6669 mbedtls_free( session->peer_cert );
6670 session->peer_cert = NULL;
6671 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006672#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006673 if( session->peer_cert_digest != NULL )
6674 {
6675 /* Zeroization is not necessary. */
6676 mbedtls_free( session->peer_cert_digest );
6677 session->peer_cert_digest = NULL;
6678 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6679 session->peer_cert_digest_len = 0;
6680 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006681#else
6682 ((void) session);
6683#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006684}
6685#endif /* MBEDTLS_X509_CRT_PARSE_C */
6686
Paul Bakker5121ce52009-01-03 21:22:43 +00006687/*
6688 * Handshake functions
6689 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006690#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006691/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006693{
Hanno Beckerdf645962019-06-26 13:02:22 +01006694 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6695 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker5121ce52009-01-03 21:22:43 +00006696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006698
Hanno Becker5097cba2019-02-05 13:36:46 +00006699 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006702 ssl->state++;
6703 return( 0 );
6704 }
6705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006708}
6709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006711{
Hanno Beckerdf645962019-06-26 13:02:22 +01006712 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6713 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006716
Hanno Becker5097cba2019-02-05 13:36:46 +00006717 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006720 ssl->state++;
6721 return( 0 );
6722 }
6723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006726}
Gilles Peskinef9828522017-05-03 12:28:43 +02006727
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006728#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006729/* Some certificate support -> implement write and parse */
6730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006734 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006735 const mbedtls_x509_crt *crt;
Hanno Beckerdf645962019-06-26 13:02:22 +01006736 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
6737 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006740
Hanno Becker5097cba2019-02-05 13:36:46 +00006741 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006744 ssl->state++;
6745 return( 0 );
6746 }
6747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006749 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6750 MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006751 {
6752 if( ssl->client_auth == 0 )
6753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 ssl->state++;
6756 return( 0 );
6757 }
6758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006760 /*
6761 * If using SSLv3 and got no cert, send an Alert message
6762 * (otherwise an empty Certificate message will be sent).
6763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006764 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01006765 mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006766 {
6767 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6769 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6770 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006773 goto write_msg;
6774 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#endif /* MBEDTLS_SSL_CLI_C */
6778#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01006779 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006781 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6784 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006785 }
6786 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006787#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006790
6791 /*
6792 * 0 . 0 handshake type
6793 * 1 . 3 handshake length
6794 * 4 . 6 length of all certs
6795 * 7 . 9 length of cert. 1
6796 * 10 . n-1 peer certificate
6797 * n . n+2 length of cert. 2
6798 * n+3 . ... upper level cert, etc.
6799 */
6800 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006802
Paul Bakker29087132010-03-21 21:03:34 +00006803 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 {
6805 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006806 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006809 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 }
6812
6813 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6814 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6815 ssl->out_msg[i + 2] = (unsigned char)( n );
6816
6817 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6818 i += n; crt = crt->next;
6819 }
6820
6821 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6822 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6823 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6824
6825 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6827 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006828
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006829#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006830write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006831#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
6833 ssl->state++;
6834
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006835 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006836 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006838 return( ret );
6839 }
6840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Paul Bakkered27a042013-04-18 22:46:23 +02006843 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006844}
6845
Hanno Becker285ff0c2019-01-31 07:44:03 +00006846#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006847
6848#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006849static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6850 unsigned char *crt_buf,
6851 size_t crt_buf_len )
6852{
6853 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6854
6855 if( peer_crt == NULL )
6856 return( -1 );
6857
6858 if( peer_crt->raw.len != crt_buf_len )
6859 return( -1 );
6860
Hanno Becker68b856d2019-02-08 14:00:04 +00006861 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006862}
Hanno Becker5882dd02019-06-06 16:25:57 +01006863#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006864static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6865 unsigned char *crt_buf,
6866 size_t crt_buf_len )
6867{
6868 int ret;
6869 unsigned char const * const peer_cert_digest =
6870 ssl->session->peer_cert_digest;
6871 mbedtls_md_type_t const peer_cert_digest_type =
6872 ssl->session->peer_cert_digest_type;
6873 mbedtls_md_info_t const * const digest_info =
6874 mbedtls_md_info_from_type( peer_cert_digest_type );
6875 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6876 size_t digest_len;
6877
6878 if( peer_cert_digest == NULL || digest_info == NULL )
6879 return( -1 );
6880
6881 digest_len = mbedtls_md_get_size( digest_info );
6882 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6883 return( -1 );
6884
6885 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6886 if( ret != 0 )
6887 return( -1 );
6888
6889 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6890}
Hanno Becker5882dd02019-06-06 16:25:57 +01006891#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006892#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006893
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006894/*
6895 * Once the certificate message is read, parse it into a cert chain and
6896 * perform basic checks, but leave actual verification to the caller
6897 */
Hanno Becker35e41772019-02-05 15:37:23 +00006898static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6899 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006900{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006901 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006902#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6903 int crt_cnt=0;
6904#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006905 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006906 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006911 mbedtls_ssl_pend_fatal_alert( ssl,
6912 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006914 }
6915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6917 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006920 mbedtls_ssl_pend_fatal_alert( ssl,
6921 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006923 }
6924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006926
Paul Bakker5121ce52009-01-03 21:22:43 +00006927 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006929 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006930 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006931
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006932 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006936 mbedtls_ssl_pend_fatal_alert( ssl,
6937 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006939 }
6940
Hanno Becker33c3dc82019-01-30 14:46:46 +00006941 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6942 i += 3;
6943
Hanno Becker33c3dc82019-01-30 14:46:46 +00006944 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006945 while( i < ssl->in_hslen )
6946 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006947 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006948 if ( i + 3 > ssl->in_hslen ) {
6949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006950 mbedtls_ssl_pend_fatal_alert( ssl,
6951 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006952 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6953 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006954 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6955 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006956 if( ssl->in_msg[i] != 0 )
6957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006959 mbedtls_ssl_pend_fatal_alert( ssl,
6960 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006962 }
6963
Hanno Becker33c3dc82019-01-30 14:46:46 +00006964 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006965 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6966 | (unsigned int) ssl->in_msg[i + 2];
6967 i += 3;
6968
6969 if( n < 128 || i + n > ssl->in_hslen )
6970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006972 mbedtls_ssl_pend_fatal_alert( ssl,
6973 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006975 }
6976
Hanno Becker33c3dc82019-01-30 14:46:46 +00006977 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006978#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6979 if( crt_cnt++ == 0 &&
Hanno Becker2d9623f2019-06-13 12:07:05 +01006980 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
6981 MBEDTLS_SSL_IS_CLIENT &&
Hanno Becker35e41772019-02-05 15:37:23 +00006982 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006983 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006984 /* During client-side renegotiation, check that the server's
6985 * end-CRTs hasn't changed compared to the initial handshake,
6986 * mitigating the triple handshake attack. On success, reuse
6987 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006988 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6989 if( ssl_check_peer_crt_unchanged( ssl,
6990 &ssl->in_msg[i],
6991 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006992 {
Hanno Becker35e41772019-02-05 15:37:23 +00006993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01006994 mbedtls_ssl_pend_fatal_alert( ssl,
6995 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker35e41772019-02-05 15:37:23 +00006996 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006997 }
Hanno Becker35e41772019-02-05 15:37:23 +00006998
6999 /* Now we can safely free the original chain. */
7000 ssl_clear_peer_cert( ssl->session );
7001 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00007002#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7003
Hanno Becker33c3dc82019-01-30 14:46:46 +00007004 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00007005#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00007006 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00007007#else
Hanno Becker42de8f82019-02-26 11:51:34 +00007008 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00007009 * it in-place from the input buffer instead of making a copy. */
7010 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
7011#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007012 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00007013 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00007014 case 0: /*ok*/
7015 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7016 /* Ignore certificate with an unknown algorithm: maybe a
7017 prior certificate was already trusted. */
7018 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007019
Hanno Becker33c3dc82019-01-30 14:46:46 +00007020 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7021 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7022 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007023
Hanno Becker33c3dc82019-01-30 14:46:46 +00007024 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7025 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7026 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007027
Hanno Becker33c3dc82019-01-30 14:46:46 +00007028 default:
7029 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7030 crt_parse_der_failed:
Hanno Beckerde62da92019-07-24 13:23:50 +01007031 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker33c3dc82019-01-30 14:46:46 +00007032 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
7033 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007034 }
7035
7036 i += n;
7037 }
7038
Hanno Becker35e41772019-02-05 15:37:23 +00007039 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007040 return( 0 );
7041}
7042
Hanno Beckerb8a08572019-02-05 12:49:06 +00007043#if defined(MBEDTLS_SSL_SRV_C)
7044static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
7045{
Hanno Becker2d9623f2019-06-13 12:07:05 +01007046 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007047 return( -1 );
7048
7049#if defined(MBEDTLS_SSL_PROTO_SSL3)
7050 /*
7051 * Check if the client sent an empty certificate
7052 */
Hanno Becker2881d802019-05-22 14:44:53 +01007053 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Hanno Beckerb8a08572019-02-05 12:49:06 +00007054 {
7055 if( ssl->in_msglen == 2 &&
7056 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
7057 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
7058 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
7059 {
7060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
7061 return( 0 );
7062 }
7063
7064 return( -1 );
7065 }
7066#endif /* MBEDTLS_SSL_PROTO_SSL3 */
7067
7068#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7069 defined(MBEDTLS_SSL_PROTO_TLS1_2)
7070 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
7071 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7072 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
7073 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
7074 {
7075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
7076 return( 0 );
7077 }
7078
Hanno Beckerb8a08572019-02-05 12:49:06 +00007079#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7080 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01007081
7082 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00007083}
7084#endif /* MBEDTLS_SSL_SRV_C */
7085
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007086/* Check if a certificate message is expected.
7087 * Return either
7088 * - SSL_CERTIFICATE_EXPECTED, or
7089 * - SSL_CERTIFICATE_SKIP
7090 * indicating whether a Certificate message is expected or not.
7091 */
7092#define SSL_CERTIFICATE_EXPECTED 0
7093#define SSL_CERTIFICATE_SKIP 1
7094static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
7095 int authmode )
7096{
Hanno Becker473f98f2019-06-26 10:27:32 +01007097 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007098 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007099
7100 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
7101 return( SSL_CERTIFICATE_SKIP );
7102
7103#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007104 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007105 {
Hanno Becker473f98f2019-06-26 10:27:32 +01007106 if( mbedtls_ssl_suite_get_key_exchange( ciphersuite_info ) ==
7107 MBEDTLS_KEY_EXCHANGE_RSA_PSK )
7108 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007109 return( SSL_CERTIFICATE_SKIP );
Hanno Becker473f98f2019-06-26 10:27:32 +01007110 }
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007111
7112 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7113 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007114 ssl->session_negotiate->verify_result =
7115 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7116 return( SSL_CERTIFICATE_SKIP );
7117 }
7118 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00007119#else
7120 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007121#endif /* MBEDTLS_SSL_SRV_C */
7122
7123 return( SSL_CERTIFICATE_EXPECTED );
7124}
7125
Hanno Becker3cf50612019-02-05 14:36:34 +00007126static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7127 int authmode,
7128 mbedtls_x509_crt *chain,
7129 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007130{
Hanno Becker8c13ee62019-02-26 16:48:17 +00007131 int verify_ret;
Hanno Becker473f98f2019-06-26 10:27:32 +01007132 mbedtls_ssl_ciphersuite_handle_t ciphersuite_info =
Hanno Beckerdf645962019-06-26 13:02:22 +01007133 mbedtls_ssl_handshake_get_ciphersuite( ssl->handshake );
Hanno Becker3cf50612019-02-05 14:36:34 +00007134 mbedtls_x509_crt *ca_chain;
7135 mbedtls_x509_crl *ca_crl;
7136
7137 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7138 return( 0 );
7139
7140#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7141 if( ssl->handshake->sni_ca_chain != NULL )
7142 {
7143 ca_chain = ssl->handshake->sni_ca_chain;
7144 ca_crl = ssl->handshake->sni_ca_crl;
7145 }
7146 else
7147#endif
7148 {
7149 ca_chain = ssl->conf->ca_chain;
7150 ca_crl = ssl->conf->ca_crl;
7151 }
7152
7153 /*
7154 * Main check: verify certificate
7155 */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007156 verify_ret = mbedtls_x509_crt_verify_restartable(
Hanno Becker3cf50612019-02-05 14:36:34 +00007157 chain,
7158 ca_chain, ca_crl,
7159 ssl->conf->cert_profile,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007160#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Hanno Becker3cf50612019-02-05 14:36:34 +00007161 ssl->hostname,
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03007162#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Hanno Becker3cf50612019-02-05 14:36:34 +00007163 &ssl->session_negotiate->verify_result,
7164 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
7165
Hanno Becker8c13ee62019-02-26 16:48:17 +00007166 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007167 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007168 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007169 }
7170
7171#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Hanno Becker8c13ee62019-02-26 16:48:17 +00007172 if( verify_ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Hanno Becker3cf50612019-02-05 14:36:34 +00007173 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7174#endif
7175
7176 /*
7177 * Secondary checks: always done, but change 'ret' only if it was 0
7178 */
7179
7180#if defined(MBEDTLS_ECP_C)
7181 {
Manuel Pégourié-Gonnardb3917662019-07-03 11:19:30 +02007182 int ret;
Hanno Becker8c13ee62019-02-26 16:48:17 +00007183 mbedtls_pk_context *pk;
7184 ret = mbedtls_x509_crt_pk_acquire( chain, &pk );
7185 if( ret != 0 )
Hanno Becker2224ccf2019-06-28 10:52:45 +01007186 {
7187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_x509_crt_pk_acquire", ret );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007188 return( ret );
Hanno Becker2224ccf2019-06-28 10:52:45 +01007189 }
Hanno Becker3cf50612019-02-05 14:36:34 +00007190
7191 /* If certificate uses an EC key, make sure the curve is OK */
Hanno Becker8c13ee62019-02-26 16:48:17 +00007192 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) )
7193 ret = mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id );
7194
Hanno Beckerc6d1c3e2019-03-05 13:50:56 +00007195 mbedtls_x509_crt_pk_release( chain );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007196
7197 if( ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007198 {
7199 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7200
7201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007202 if( verify_ret == 0 )
7203 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007204 }
7205 }
7206#endif /* MBEDTLS_ECP_C */
7207
7208 if( mbedtls_ssl_check_cert_usage( chain,
7209 ciphersuite_info,
Hanno Becker2d9623f2019-06-13 12:07:05 +01007210 ( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7211 MBEDTLS_SSL_IS_CLIENT ),
Hanno Becker3cf50612019-02-05 14:36:34 +00007212 &ssl->session_negotiate->verify_result ) != 0 )
7213 {
7214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007215 if( verify_ret == 0 )
7216 verify_ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Becker3cf50612019-02-05 14:36:34 +00007217 }
7218
7219 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7220 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7221 * with details encoded in the verification flags. All other kinds
7222 * of error codes, including those from the user provided f_vrfy
7223 * functions, are treated as fatal and lead to a failure of
7224 * ssl_parse_certificate even if verification was optional. */
7225 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
Hanno Becker8c13ee62019-02-26 16:48:17 +00007226 ( verify_ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7227 verify_ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
Hanno Becker3cf50612019-02-05 14:36:34 +00007228 {
Hanno Becker8c13ee62019-02-26 16:48:17 +00007229 verify_ret = 0;
Hanno Becker3cf50612019-02-05 14:36:34 +00007230 }
7231
7232 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
7233 {
7234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Hanno Becker8c13ee62019-02-26 16:48:17 +00007235 verify_ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
Hanno Becker3cf50612019-02-05 14:36:34 +00007236 }
7237
Hanno Becker8c13ee62019-02-26 16:48:17 +00007238 if( verify_ret != 0 )
Hanno Becker3cf50612019-02-05 14:36:34 +00007239 {
7240 uint8_t alert;
7241
7242 /* The certificate may have been rejected for several reasons.
7243 Pick one and send the corresponding alert. Which alert to send
7244 may be a subject of debate in some cases. */
7245 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7246 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7247 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7248 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7249 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7250 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7251 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7252 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7253 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7254 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7255 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7256 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7257 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7258 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7259 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7260 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7261 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7262 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7263 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7264 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7265 else
7266 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Hanno Beckerde62da92019-07-24 13:23:50 +01007267 mbedtls_ssl_pend_fatal_alert( ssl, alert );
Hanno Becker3cf50612019-02-05 14:36:34 +00007268 }
7269
7270#if defined(MBEDTLS_DEBUG_C)
7271 if( ssl->session_negotiate->verify_result != 0 )
7272 {
7273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7274 ssl->session_negotiate->verify_result ) );
7275 }
7276 else
7277 {
7278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7279 }
7280#endif /* MBEDTLS_DEBUG_C */
7281
Hanno Becker8c13ee62019-02-26 16:48:17 +00007282 return( verify_ret );
Hanno Becker3cf50612019-02-05 14:36:34 +00007283}
7284
Hanno Becker34106f62019-02-08 14:59:05 +00007285#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01007286
7287#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007288static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7289 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007290{
7291 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00007292 /* Remember digest of the peer's end-CRT. */
7293 ssl->session_negotiate->peer_cert_digest =
7294 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7295 if( ssl->session_negotiate->peer_cert_digest == NULL )
7296 {
7297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7298 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007299 mbedtls_ssl_pend_fatal_alert( ssl,
7300 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker34106f62019-02-08 14:59:05 +00007301
7302 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7303 }
7304
7305 ret = mbedtls_md( mbedtls_md_info_from_type(
7306 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7307 start, len,
7308 ssl->session_negotiate->peer_cert_digest );
7309
7310 ssl->session_negotiate->peer_cert_digest_type =
7311 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7312 ssl->session_negotiate->peer_cert_digest_len =
7313 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7314
7315 return( ret );
7316}
Hanno Becker5882dd02019-06-06 16:25:57 +01007317#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00007318
7319static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7320 unsigned char *start, size_t len )
7321{
7322 unsigned char *end = start + len;
7323 int ret;
7324
7325 /* Make a copy of the peer's raw public key. */
7326 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7327 ret = mbedtls_pk_parse_subpubkey( &start, end,
7328 &ssl->handshake->peer_pubkey );
7329 if( ret != 0 )
7330 {
7331 /* We should have parsed the public key before. */
7332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7333 }
7334
7335 return( 0 );
7336}
7337#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7338
Hanno Becker3cf50612019-02-05 14:36:34 +00007339int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7340{
7341 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007342 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007343#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7344 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7345 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007346 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007347#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01007348 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007349#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007350 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00007351 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007352
7353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7354
Hanno Becker6b9a6f32019-02-07 10:11:07 +00007355 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7356 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007357 {
7358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00007359 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007360 }
7361
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007362#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7363 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007364 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007365 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00007366 chain = ssl->handshake->ecrs_peer_cert;
7367 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007368 goto crt_verify;
7369 }
7370#endif
7371
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007372 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007373 {
7374 /* mbedtls_ssl_read_record may have sent an alert already. We
7375 let it decide whether to alert. */
7376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007377 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007378 }
7379
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007380#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00007381 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7382 {
7383 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007384
Hanno Beckerb8a08572019-02-05 12:49:06 +00007385 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00007386 ret = 0;
7387 else
7388 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00007389
Hanno Becker613d4902019-02-05 13:11:17 +00007390 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007391 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00007392#endif /* MBEDTLS_SSL_SRV_C */
7393
Hanno Becker35e41772019-02-05 15:37:23 +00007394 /* Clear existing peer CRT structure in case we tried to
7395 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00007396 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00007397
7398 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7399 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00007400 {
7401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7402 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007403 mbedtls_ssl_pend_fatal_alert( ssl,
7404 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00007405
Hanno Beckere4aeb762019-02-05 17:19:52 +00007406 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7407 goto exit;
7408 }
7409 mbedtls_x509_crt_init( chain );
7410
7411 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00007412 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007413 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007414
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007415#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7416 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007417 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007418
7419crt_verify:
7420 if( ssl->handshake->ecrs_enabled)
7421 rs_ctx = &ssl->handshake->ecrs_ctx;
7422#endif
7423
Hanno Becker3cf50612019-02-05 14:36:34 +00007424 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00007425 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00007426 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00007427 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007428
Hanno Becker3008d282019-02-05 17:02:28 +00007429#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00007430 {
Hanno Becker5882dd02019-06-06 16:25:57 +01007431 size_t pk_len;
7432 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00007433
Hanno Becker34106f62019-02-08 14:59:05 +00007434 /* We parse the CRT chain without copying, so
7435 * these pointers point into the input buffer,
7436 * and are hence still valid after freeing the
7437 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007438
Hanno Becker5882dd02019-06-06 16:25:57 +01007439#if defined(MBEDTLS_SSL_RENEGOTIATION)
7440 unsigned char *crt_start;
7441 size_t crt_len;
7442
Hanno Becker34106f62019-02-08 14:59:05 +00007443 crt_start = chain->raw.p;
7444 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01007445#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007446
Hanno Becker8c13ee62019-02-26 16:48:17 +00007447 pk_start = chain->cache->pk_raw.p;
7448 pk_len = chain->cache->pk_raw.len;
Hanno Becker34106f62019-02-08 14:59:05 +00007449
7450 /* Free the CRT structures before computing
7451 * digest and copying the peer's public key. */
7452 mbedtls_x509_crt_free( chain );
7453 mbedtls_free( chain );
7454 chain = NULL;
7455
Hanno Becker5882dd02019-06-06 16:25:57 +01007456#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00007457 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00007459 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01007460#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007461
Hanno Becker34106f62019-02-08 14:59:05 +00007462 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00007463 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00007464 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007465 }
Hanno Becker34106f62019-02-08 14:59:05 +00007466#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7467 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00007468 ssl->session_negotiate->peer_cert = chain;
7469 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00007470#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007473
Hanno Becker613d4902019-02-05 13:11:17 +00007474exit:
7475
Hanno Beckere4aeb762019-02-05 17:19:52 +00007476 if( ret == 0 )
7477 ssl->state++;
7478
7479#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7480 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7481 {
7482 ssl->handshake->ecrs_peer_cert = chain;
7483 chain = NULL;
7484 }
7485#endif
7486
7487 if( chain != NULL )
7488 {
7489 mbedtls_x509_crt_free( chain );
7490 mbedtls_free( chain );
7491 }
7492
Paul Bakker5121ce52009-01-03 21:22:43 +00007493 return( ret );
7494}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00007495#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007498{
7499 int ret;
7500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007504 ssl->out_msglen = 1;
7505 ssl->out_msg[0] = 1;
7506
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 ssl->state++;
7508
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007509 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007510 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007512 return( ret );
7513 }
7514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007516
7517 return( 0 );
7518}
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007521{
7522 int ret;
7523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007525
Hanno Becker327c93b2018-08-15 13:56:18 +01007526 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007529 return( ret );
7530 }
7531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007535 mbedtls_ssl_pend_fatal_alert( ssl,
7536 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007538 }
7539
Hanno Beckere678eaa2018-08-21 14:57:46 +01007540 /* CCS records are only accepted if they have length 1 and content '1',
7541 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007542
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007543 /*
7544 * Switch to our negotiated transform and session parameters for inbound
7545 * data.
7546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007548 ssl->transform_in = ssl->transform_negotiate;
7549 ssl->session_in = ssl->session_negotiate;
7550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007552 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007555 ssl_dtls_replay_reset( ssl );
7556#endif
7557
7558 /* Increment epoch */
7559 if( ++ssl->in_epoch == 0 )
7560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007562 /* This is highly unlikely to happen for legitimate reasons, so
7563 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007565 }
7566 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007567 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007569#if defined(MBEDTLS_SSL_PROTO_TLS)
7570 {
7571 memset( ssl->in_ctr, 0, 8 );
7572 }
7573#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007574
Hanno Beckerf5970a02019-05-08 09:38:41 +01007575 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7578 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Hanno Beckerde62da92019-07-24 13:23:50 +01007583 mbedtls_ssl_pend_fatal_alert( ssl,
7584 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007586 }
7587 }
7588#endif
7589
Paul Bakker5121ce52009-01-03 21:22:43 +00007590 ssl->state++;
7591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007593
7594 return( 0 );
7595}
7596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7600 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Hanno Becker533f5b12019-08-15 16:56:35 +01007601 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007602 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007603#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7605#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007606 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007609 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007610#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007612}
7613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007617
7618 /*
7619 * Free our handshake params
7620 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007621 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007623 ssl->handshake = NULL;
7624
7625 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007626 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007627 */
7628 if( ssl->transform )
7629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_transform_free( ssl->transform );
7631 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007632 }
7633 ssl->transform = ssl->transform_negotiate;
7634 ssl->transform_negotiate = NULL;
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637}
7638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643#if defined(MBEDTLS_SSL_RENEGOTIATION)
7644 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007647 ssl->renego_records_seen = 0;
7648 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007649#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007650
7651 /*
7652 * Free the previous session and switch in the current one
7653 */
Paul Bakker0a597072012-09-25 21:55:46 +00007654 if( ssl->session )
7655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007657 /* RFC 7366 3.1: keep the EtM state */
7658 ssl->session_negotiate->encrypt_then_mac =
7659 ssl->session->encrypt_then_mac;
7660#endif
7661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 mbedtls_ssl_session_free( ssl->session );
7663 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007664 }
7665 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007666 ssl->session_negotiate = NULL;
7667
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007668#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007669 /*
7670 * Add cache entry
7671 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007672 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007673 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007674 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007675 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007676 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007678 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007679#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007682 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007683 ssl->handshake->flight != NULL )
7684 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007685 /* Cancel handshake timer */
7686 ssl_set_timer( ssl, 0 );
7687
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007688 /* Keep last flight around in case we need to resend it:
7689 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007691 }
7692 else
7693#endif
7694 ssl_handshake_wrapup_free_hs_transform( ssl );
7695
Paul Bakker48916f92012-09-16 19:57:18 +00007696 ssl->state++;
7697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007699}
7700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007702{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007703 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007706
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007707 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007708
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007709 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7710 mbedtls_ssl_suite_get_mac(
7711 mbedtls_ssl_ciphersuite_from_id(
7712 mbedtls_ssl_session_get_ciphersuite(
7713 ssl->session_negotiate ) ) ),
7714 ssl, ssl->out_msg + 4,
7715 mbedtls_ssl_conf_get_endpoint( ssl->conf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007716
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007717 /*
7718 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7719 * may define some other value. Currently (early 2016), no defined
7720 * ciphersuite does this (and this is unlikely to change as activity has
7721 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7722 */
Hanno Becker2881d802019-05-22 14:44:53 +01007723 hash_len = ( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007726 ssl->verify_data_len = hash_len;
7727 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007728#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007729
Paul Bakker5121ce52009-01-03 21:22:43 +00007730 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7732 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007733
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007734#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 /*
7736 * In case of session resuming, invert the client and server
7737 * ChangeCipherSpec messages order.
7738 */
Paul Bakker0a597072012-09-25 21:55:46 +00007739 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007742 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7743 MBEDTLS_SSL_IS_CLIENT )
7744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007746 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007748#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007749 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
7750 MBEDTLS_SSL_IS_SERVER )
7751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Hanno Becker2d9623f2019-06-13 12:07:05 +01007753 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007754#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007755 }
7756 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007757#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007758 ssl->state++;
7759
Paul Bakker48916f92012-09-16 19:57:18 +00007760 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007761 * Switch to our negotiated transform and session parameters for outbound
7762 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007767 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007768 {
7769 unsigned char i;
7770
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007771 /* Remember current epoch settings for resending */
7772 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007773 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007774
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007775 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007776 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007777
7778 /* Increment epoch */
7779 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007780 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007781 break;
7782
7783 /* The loop goes to its end iff the counter is wrapping */
7784 if( i == 0 )
7785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7787 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007788 }
7789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007790 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007792#if defined(MBEDTLS_SSL_PROTO_TLS)
7793 {
7794 memset( ssl->cur_out_ctr, 0, 8 );
7795 }
7796#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007797
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007798 ssl->transform_out = ssl->transform_negotiate;
7799 ssl->session_out = ssl->session_negotiate;
7800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7802 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7807 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007808 }
7809 }
7810#endif
7811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007813 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007815#endif
7816
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007817 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007818 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007820 return( ret );
7821 }
7822
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007823#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007824 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007825 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7826 {
7827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7828 return( ret );
7829 }
7830#endif
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007833
7834 return( 0 );
7835}
7836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007838#define SSL_MAX_HASH_LEN 36
7839#else
7840#define SSL_MAX_HASH_LEN 12
7841#endif
7842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007844{
Paul Bakker23986e52011-04-24 08:57:21 +00007845 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007846 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007847 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007850
Hanno Beckerc2fb7592019-08-15 16:31:23 +01007851 ssl_calc_finished( mbedtls_ssl_get_minor_ver( ssl ),
7852 mbedtls_ssl_suite_get_mac(
7853 mbedtls_ssl_ciphersuite_from_id(
7854 mbedtls_ssl_session_get_ciphersuite(
7855 ssl->session_negotiate ) ) ),
7856 ssl, buf,
7857 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007858
Hanno Becker327c93b2018-08-15 13:56:18 +01007859 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007862 return( ret );
7863 }
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007868 mbedtls_ssl_pend_fatal_alert( ssl,
7869 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007871 }
7872
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007873 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +01007875 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007876 hash_len = 36;
7877 else
7878#endif
7879 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7882 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007885 mbedtls_ssl_pend_fatal_alert( ssl,
7886 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007888 }
7889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007891 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Hanno Beckerde62da92019-07-24 13:23:50 +01007894 mbedtls_ssl_pend_fatal_alert( ssl,
7895 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007897 }
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007900 ssl->verify_data_len = hash_len;
7901 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007902#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007903
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007904#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007905 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007908 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +01007912 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007914#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007915 }
7916 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007917#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007918 ssl->state++;
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007921 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007923#endif
7924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007926
7927 return( 0 );
7928}
7929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007931{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7935 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7936 mbedtls_md5_init( &handshake->fin_md5 );
7937 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007938 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7939 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007940#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7942#if defined(MBEDTLS_SHA256_C)
7943 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007944 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SHA512_C)
7947 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007948 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007949#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007951
Hanno Becker7e5437a2017-04-28 17:15:26 +01007952#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7953 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7954 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7955#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957#if defined(MBEDTLS_DHM_C)
7958 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_ECDH_C)
7961 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007962#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007963#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007964 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007965#if defined(MBEDTLS_SSL_CLI_C)
7966 handshake->ecjpake_cache = NULL;
7967 handshake->ecjpake_cache_len = 0;
7968#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007969#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007970
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007971#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007972 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007973#endif
7974
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007975#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7976 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7977#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007978
7979#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7980 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7981 mbedtls_pk_init( &handshake->peer_pubkey );
7982#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007983}
7984
Hanno Becker611a83b2018-01-03 14:27:32 +00007985void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007987 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7990 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007991
Hanno Becker92231322018-01-03 15:32:51 +00007992#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 mbedtls_md_init( &transform->md_ctx_enc );
7994 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007995#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996}
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007999{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008001}
8002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008004{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008005 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008006 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008008 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008010 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008011 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008012
8013 /*
8014 * Either the pointers are now NULL or cleared properly and can be freed.
8015 * Now allocate missing structures.
8016 */
8017 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008018 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008019 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008020 }
Paul Bakker48916f92012-09-16 19:57:18 +00008021
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008022 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008023 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008024 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008025 }
Paul Bakker48916f92012-09-16 19:57:18 +00008026
Paul Bakker82788fb2014-10-20 13:59:19 +02008027 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008028 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008029 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008030 }
Paul Bakker48916f92012-09-16 19:57:18 +00008031
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008032 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008033 if( ssl->handshake == NULL ||
8034 ssl->transform_negotiate == NULL ||
8035 ssl->session_negotiate == NULL )
8036 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 mbedtls_free( ssl->handshake );
8040 mbedtls_free( ssl->transform_negotiate );
8041 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008042
8043 ssl->handshake = NULL;
8044 ssl->transform_negotiate = NULL;
8045 ssl->session_negotiate = NULL;
8046
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008047 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008048 }
8049
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008050 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00008052 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008053 ssl_handshake_params_init( ssl->handshake );
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008056 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008057 {
8058 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008059
Hanno Becker2d9623f2019-06-13 12:07:05 +01008060 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008061 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8062 else
8063 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
8064 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008065#endif
8066
Paul Bakker48916f92012-09-16 19:57:18 +00008067 return( 0 );
8068}
8069
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008070#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008071/* Dummy cookie callbacks for defaults */
8072static int ssl_cookie_write_dummy( void *ctx,
8073 unsigned char **p, unsigned char *end,
8074 const unsigned char *cli_id, size_t cli_id_len )
8075{
8076 ((void) ctx);
8077 ((void) p);
8078 ((void) end);
8079 ((void) cli_id);
8080 ((void) cli_id_len);
8081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008082 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008083}
8084
8085static int ssl_cookie_check_dummy( void *ctx,
8086 const unsigned char *cookie, size_t cookie_len,
8087 const unsigned char *cli_id, size_t cli_id_len )
8088{
8089 ((void) ctx);
8090 ((void) cookie);
8091 ((void) cookie_len);
8092 ((void) cli_id);
8093 ((void) cli_id_len);
8094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008096}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008097#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008098
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008099/* Once ssl->out_hdr as the address of the beginning of the
8100 * next outgoing record is set, deduce the other pointers.
8101 *
8102 * Note: For TLS, we save the implicit record sequence number
8103 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8104 * and the caller has to make sure there's space for this.
8105 */
8106
8107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8108 mbedtls_ssl_transform *transform )
8109{
8110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008111 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008112 {
8113 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008114#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008115 ssl->out_cid = ssl->out_ctr + 8;
8116 ssl->out_len = ssl->out_cid;
8117 if( transform != NULL )
8118 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008119#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008120 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008121#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008122 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008123 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008124 MBEDTLS_SSL_TRANSPORT_ELSE
8125#endif /* MBEDTLS_SSL_PROTO_DTLS */
8126#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008127 {
8128 ssl->out_ctr = ssl->out_hdr - 8;
8129 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008130#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008131 ssl->out_cid = ssl->out_len;
8132#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008133 ssl->out_iv = ssl->out_hdr + 5;
8134 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008135#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008136
8137 /* Adjust out_msg to make space for explicit IV, if used. */
8138 if( transform != NULL &&
Hanno Becker2881d802019-05-22 14:44:53 +01008139 mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008140 {
8141 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8142 }
8143 else
8144 ssl->out_msg = ssl->out_iv;
8145}
8146
8147/* Once ssl->in_hdr as the address of the beginning of the
8148 * next incoming record is set, deduce the other pointers.
8149 *
8150 * Note: For TLS, we save the implicit record sequence number
8151 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8152 * and the caller has to make sure there's space for this.
8153 */
8154
Hanno Beckerf5970a02019-05-08 09:38:41 +01008155static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008156{
Hanno Beckerf5970a02019-05-08 09:38:41 +01008157 /* This function sets the pointers to match the case
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008158 * of unprotected TLS/DTLS records, with ssl->in_msg
8159 * pointing to the beginning of the record content.
Hanno Beckerf5970a02019-05-08 09:38:41 +01008160 *
8161 * When decrypting a protected record, ssl->in_msg
8162 * will be shifted to point to the beginning of the
8163 * record plaintext.
8164 */
8165
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008166#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008167 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168 {
Hanno Becker70e79282019-05-03 14:34:53 +01008169 /* This sets the header pointers to match records
8170 * without CID. When we receive a record containing
8171 * a CID, the fields are shifted accordingly in
8172 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008173 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008174#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01008175 ssl->in_cid = ssl->in_ctr + 8;
8176 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01008177#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01008178 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008179#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008180 ssl->in_msg = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008181 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008182 MBEDTLS_SSL_TRANSPORT_ELSE
8183#endif /* MBEDTLS_SSL_PROTO_DTLS */
8184#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008185 {
8186 ssl->in_ctr = ssl->in_hdr - 8;
8187 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008188#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01008189 ssl->in_cid = ssl->in_len;
8190#endif
Hanno Beckerc360dcc2019-07-12 10:00:45 +01008191 ssl->in_msg = ssl->in_hdr + 5;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008192 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008193#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008194}
8195
Paul Bakker5121ce52009-01-03 21:22:43 +00008196/*
8197 * Initialize an SSL context
8198 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008199void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8200{
8201 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8202}
8203
8204/*
8205 * Setup an SSL context
8206 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008207
8208static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8209{
8210 /* Set the incoming and outgoing record pointers. */
8211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008212 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008213 {
8214 ssl->out_hdr = ssl->out_buf;
8215 ssl->in_hdr = ssl->in_buf;
8216 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008217 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008218#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008219#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008220 {
8221 ssl->out_hdr = ssl->out_buf + 8;
8222 ssl->in_hdr = ssl->in_buf + 8;
8223 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02008224#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008225
8226 /* Derive other internal pointers. */
8227 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01008228 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008229}
8230
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008231int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008232 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008233{
Paul Bakker48916f92012-09-16 19:57:18 +00008234 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008235
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008236 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008237
Hanno Beckeref982d52019-07-23 15:56:18 +01008238#if defined(MBEDTLS_USE_TINYCRYPT)
8239 uECC_set_rng( &uecc_rng_wrapper );
8240#endif
8241
Paul Bakker62f2dee2012-09-28 07:31:51 +00008242 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008243 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008244 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008245
8246 /* Set to NULL in case of an error condition */
8247 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008248
Angus Grattond8213d02016-05-25 20:56:48 +10008249 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8250 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008251 {
Angus Grattond8213d02016-05-25 20:56:48 +10008252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008253 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008254 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008255 }
8256
8257 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8258 if( ssl->out_buf == NULL )
8259 {
8260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008261 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008262 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008263 }
8264
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008265 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008266
Paul Bakker48916f92012-09-16 19:57:18 +00008267 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008268 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008269
Hanno Beckerc8f52992019-07-25 11:15:08 +01008270 ssl->pending_fatal_alert_msg = MBEDTLS_SSL_ALERT_MSG_NONE;
Hanno Beckerf46e1ce2019-07-03 13:56:59 +01008271
Paul Bakker5121ce52009-01-03 21:22:43 +00008272 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008273
8274error:
8275 mbedtls_free( ssl->in_buf );
8276 mbedtls_free( ssl->out_buf );
8277
8278 ssl->conf = NULL;
8279
8280 ssl->in_buf = NULL;
8281 ssl->out_buf = NULL;
8282
8283 ssl->in_hdr = NULL;
8284 ssl->in_ctr = NULL;
8285 ssl->in_len = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008286 ssl->in_msg = NULL;
8287
8288 ssl->out_hdr = NULL;
8289 ssl->out_ctr = NULL;
8290 ssl->out_len = NULL;
8291 ssl->out_iv = NULL;
8292 ssl->out_msg = NULL;
8293
k-stachowiak9f7798e2018-07-31 16:52:32 +02008294 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008295}
8296
8297/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008298 * Reset an initialized and used SSL context for re-use while retaining
8299 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008300 *
8301 * If partial is non-zero, keep data in the input buffer and client ID.
8302 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008303 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008304static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008305{
Paul Bakker48916f92012-09-16 19:57:18 +00008306 int ret;
8307
Hanno Becker7e772132018-08-10 12:38:21 +01008308#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8309 !defined(MBEDTLS_SSL_SRV_C)
8310 ((void) partial);
8311#endif
8312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008314
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008315 /* Cancel any possibly running timer */
8316 ssl_set_timer( ssl, 0 );
8317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318#if defined(MBEDTLS_SSL_RENEGOTIATION)
8319 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008320 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008321
8322 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8324 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008327
Paul Bakker7eb013f2011-10-06 12:37:39 +00008328 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008329 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008330
8331 ssl->in_msgtype = 0;
8332 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008333#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008334 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008335 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008338 ssl_dtls_replay_reset( ssl );
8339#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008340
8341 ssl->in_hslen = 0;
8342 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008343
8344 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008345
8346 ssl->out_msgtype = 0;
8347 ssl->out_msglen = 0;
8348 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8350 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008351 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008352#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008353
Hanno Becker19859472018-08-06 09:40:20 +01008354 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8355
Paul Bakker48916f92012-09-16 19:57:18 +00008356 ssl->transform_in = NULL;
8357 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008358
Hanno Becker78640902018-08-13 16:35:15 +01008359 ssl->session_in = NULL;
8360 ssl->session_out = NULL;
8361
Angus Grattond8213d02016-05-25 20:56:48 +10008362 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008363
8364#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008365 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008366#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8367 {
8368 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008369 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008370 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8373 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8376 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8379 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008380 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008381 }
8382#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008383
Paul Bakker48916f92012-09-16 19:57:18 +00008384 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386 mbedtls_ssl_transform_free( ssl->transform );
8387 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008388 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008389 }
Paul Bakker48916f92012-09-16 19:57:18 +00008390
Paul Bakkerc0463502013-02-14 11:19:38 +01008391 if( ssl->session )
8392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393 mbedtls_ssl_session_free( ssl->session );
8394 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008395 ssl->session = NULL;
8396 }
8397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008399 ssl->alpn_chosen = NULL;
8400#endif
8401
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008402#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008403#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008404 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008405#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008406 {
8407 mbedtls_free( ssl->cli_id );
8408 ssl->cli_id = NULL;
8409 ssl->cli_id_len = 0;
8410 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008411#endif
8412
Paul Bakker48916f92012-09-16 19:57:18 +00008413 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8414 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008415
8416 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008417}
8418
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008419/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008420 * Reset an initialized and used SSL context for re-use while retaining
8421 * all application-set variables, function pointers and data.
8422 */
8423int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8424{
8425 return( ssl_session_reset_int( ssl, 0 ) );
8426}
8427
8428/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008429 * SSL set accessors
8430 */
Hanno Becker2d9623f2019-06-13 12:07:05 +01008431#if !defined(MBEDTLS_SSL_CONF_ENDPOINT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008432void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008434 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008435}
Hanno Becker2d9623f2019-06-13 12:07:05 +01008436#endif /* MBEDTLS_SSL_CONF_ENDPOINT */
Paul Bakker5121ce52009-01-03 21:22:43 +00008437
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008438void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008439{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008440 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008441}
8442
Hanno Becker7f376f42019-06-12 16:20:48 +01008443#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8444 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008445void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008446{
Hanno Becker7f376f42019-06-12 16:20:48 +01008447 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008448}
Hanno Becker7f376f42019-06-12 16:20:48 +01008449#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008450
Hanno Beckerde671542019-06-12 16:30:46 +01008451#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8452 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8453void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8454 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008455{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008456 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008457}
Hanno Beckerde671542019-06-12 16:30:46 +01008458#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008461
Hanno Becker1841b0a2018-08-24 11:13:57 +01008462void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8463 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008464{
8465 ssl->disable_datagram_packing = !allow_packing;
8466}
8467
Hanno Becker1f835fa2019-06-13 10:14:59 +01008468#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8469 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008470void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8471 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008472{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008473 conf->hs_timeout_min = min;
8474 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008475}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008476#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8477 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8478void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8479 uint32_t min, uint32_t max )
8480{
8481 ((void) conf);
8482 ((void) min);
8483 ((void) max);
8484}
8485#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8486 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8487
8488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008489
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008490void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008491{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008492#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8493 conf->authmode = authmode;
8494#else
8495 ((void) conf);
8496 ((void) authmode);
8497#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008498}
8499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008501void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008502 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008503 void *p_vrfy )
8504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008505 conf->f_vrfy = f_vrfy;
8506 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008509
Hanno Beckerece325c2019-06-13 15:39:27 +01008510#if !defined(MBEDTLS_SSL_CONF_RNG)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008511void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008512 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008513 void *p_rng )
8514{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008515 conf->f_rng = f_rng;
8516 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008517}
Hanno Beckerece325c2019-06-13 15:39:27 +01008518#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008519
Hanno Becker14a4a442019-07-02 17:00:34 +01008520#if defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008521void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008522 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008523 void *p_dbg )
8524{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008525 conf->f_dbg = f_dbg;
8526 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008527}
Hanno Becker14a4a442019-07-02 17:00:34 +01008528#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008529
Hanno Beckera58a8962019-06-13 16:11:15 +01008530#if !defined(MBEDTLS_SSL_CONF_RECV) && \
8531 !defined(MBEDTLS_SSL_CONF_SEND) && \
8532 !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008534 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008535 mbedtls_ssl_send_t *f_send,
8536 mbedtls_ssl_recv_t *f_recv,
8537 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008538{
Hanno Beckera58a8962019-06-13 16:11:15 +01008539 ssl->p_bio = p_bio;
8540 ssl->f_send = f_send;
8541 ssl->f_recv = f_recv;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008542 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008543}
Hanno Beckera58a8962019-06-13 16:11:15 +01008544#else
8545void mbedtls_ssl_set_bio_ctx( mbedtls_ssl_context *ssl,
8546 void *p_bio )
8547{
8548 ssl->p_bio = p_bio;
8549}
8550#endif
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008551
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008552#if defined(MBEDTLS_SSL_PROTO_DTLS)
8553void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8554{
8555 ssl->mtu = mtu;
8556}
8557#endif
8558
Hanno Becker1f835fa2019-06-13 10:14:59 +01008559#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008560void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008561{
8562 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008563}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008564#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008565
Hanno Becker0ae6b242019-06-13 16:45:36 +01008566#if !defined(MBEDTLS_SSL_CONF_SET_TIMER) && \
8567 !defined(MBEDTLS_SSL_CONF_GET_TIMER)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008568void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8569 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008570 mbedtls_ssl_set_timer_t *f_set_timer,
8571 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008572{
8573 ssl->p_timer = p_timer;
8574 ssl->f_set_timer = f_set_timer;
8575 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008576 /* Make sure we start with no timer running */
8577 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008578}
Hanno Becker0ae6b242019-06-13 16:45:36 +01008579#else
8580void mbedtls_ssl_set_timer_cb_ctx( mbedtls_ssl_context *ssl,
8581 void *p_timer )
8582{
8583 ssl->p_timer = p_timer;
8584 /* Make sure we start with no timer running */
8585 ssl_set_timer( ssl, 0 );
8586}
8587#endif
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008588
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008589#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008590void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008591 void *p_cache,
8592 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8593 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008594{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008595 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008596 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008597 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008598}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008599#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008600
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008601#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008602int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008603{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008604 int ret;
8605
8606 if( ssl == NULL ||
8607 session == NULL ||
8608 ssl->session_negotiate == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01008609 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008612 }
8613
Hanno Becker58fccf22019-02-06 14:30:46 +00008614 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8615 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008616 return( ret );
8617
Paul Bakker0a597072012-09-25 21:55:46 +00008618 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008619
8620 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008621}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008622#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008623
Hanno Becker73f4cb12019-06-27 13:51:07 +01008624#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008625void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008626 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008628 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8629 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8630 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8631 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008632}
8633
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008634void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008635 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008636 int major, int minor )
8637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008639 return;
8640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008642 return;
8643
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008644 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008645}
Hanno Becker73f4cb12019-06-27 13:51:07 +01008646#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008649void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008650 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008651{
8652 conf->cert_profile = profile;
8653}
8654
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008655/* Append a new keycert entry to a (possibly empty) list */
8656static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8657 mbedtls_x509_crt *cert,
8658 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008659{
niisato8ee24222018-06-25 19:05:48 +09008660 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008661
niisato8ee24222018-06-25 19:05:48 +09008662 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8663 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008664 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008665
niisato8ee24222018-06-25 19:05:48 +09008666 new_cert->cert = cert;
8667 new_cert->key = key;
8668 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008669
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008670 /* Update head is the list was null, else add to the end */
8671 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008672 {
niisato8ee24222018-06-25 19:05:48 +09008673 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008674 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008675 else
8676 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008677 mbedtls_ssl_key_cert *cur = *head;
8678 while( cur->next != NULL )
8679 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008680 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008681 }
8682
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008683 return( 0 );
8684}
8685
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008686int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008687 mbedtls_x509_crt *own_cert,
8688 mbedtls_pk_context *pk_key )
8689{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008690 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008691}
8692
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008693void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008694 mbedtls_x509_crt *ca_chain,
8695 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008696{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008697 conf->ca_chain = ca_chain;
8698 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008701
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008702#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8703int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8704 mbedtls_x509_crt *own_cert,
8705 mbedtls_pk_context *pk_key )
8706{
8707 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8708 own_cert, pk_key ) );
8709}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008710
8711void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8712 mbedtls_x509_crt *ca_chain,
8713 mbedtls_x509_crl *ca_crl )
8714{
8715 ssl->handshake->sni_ca_chain = ca_chain;
8716 ssl->handshake->sni_ca_crl = ca_crl;
8717}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008718
8719void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8720 int authmode )
8721{
8722 ssl->handshake->sni_authmode = authmode;
8723}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008724#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8725
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008726#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008727/*
8728 * Set EC J-PAKE password for current handshake
8729 */
8730int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8731 const unsigned char *pw,
8732 size_t pw_len )
8733{
8734 mbedtls_ecjpake_role role;
8735
Janos Follath8eb64132016-06-03 15:40:57 +01008736 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8738
Hanno Becker2d9623f2019-06-13 12:07:05 +01008739 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008740 role = MBEDTLS_ECJPAKE_SERVER;
8741 else
8742 role = MBEDTLS_ECJPAKE_CLIENT;
8743
8744 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8745 role,
8746 MBEDTLS_MD_SHA256,
8747 MBEDTLS_ECP_DP_SECP256R1,
8748 pw, pw_len ) );
8749}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008750#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008753int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008754 const unsigned char *psk, size_t psk_len,
8755 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008756{
Paul Bakker6db455e2013-09-18 17:29:31 +02008757 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008760 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008762
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008763 /* Identity len will be encoded on two bytes */
8764 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008765 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008766 {
8767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8768 }
8769
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008770 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008771 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008772 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008773
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008774 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008775 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008776 conf->psk_len = 0;
8777 }
8778 if( conf->psk_identity != NULL )
8779 {
8780 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008781 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008782 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008783 }
8784
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008785 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8786 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008787 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008788 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008789 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008790 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008791 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008792 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008793 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008794
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008795 conf->psk_len = psk_len;
8796 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008797
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008798 memcpy( conf->psk, psk, conf->psk_len );
8799 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008800
8801 return( 0 );
8802}
8803
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008804int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8805 const unsigned char *psk, size_t psk_len )
8806{
8807 if( psk == NULL || ssl->handshake == NULL )
8808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8809
8810 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8812
8813 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008814 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008815 mbedtls_platform_zeroize( ssl->handshake->psk,
8816 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008817 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008818 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008819 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008820
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008821 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008822 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008823
8824 ssl->handshake->psk_len = psk_len;
8825 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8826
8827 return( 0 );
8828}
8829
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008830void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008832 size_t),
8833 void *p_psk )
8834{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008835 conf->f_psk = f_psk;
8836 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008839
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008840#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008841
8842#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008843int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008844{
8845 int ret;
8846
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008847 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8848 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8849 {
8850 mbedtls_mpi_free( &conf->dhm_P );
8851 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008852 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008854
8855 return( 0 );
8856}
Hanno Becker470a8c42017-10-04 15:28:46 +01008857#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008858
Hanno Beckera90658f2017-10-04 15:29:08 +01008859int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8860 const unsigned char *dhm_P, size_t P_len,
8861 const unsigned char *dhm_G, size_t G_len )
8862{
8863 int ret;
8864
8865 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8866 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8867 {
8868 mbedtls_mpi_free( &conf->dhm_P );
8869 mbedtls_mpi_free( &conf->dhm_G );
8870 return( ret );
8871 }
8872
8873 return( 0 );
8874}
Paul Bakker5121ce52009-01-03 21:22:43 +00008875
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008876int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008877{
8878 int ret;
8879
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008880 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8881 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8882 {
8883 mbedtls_mpi_free( &conf->dhm_P );
8884 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008885 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008886 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008887
8888 return( 0 );
8889}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008890#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008891
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008892#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8893/*
8894 * Set the minimum length for Diffie-Hellman parameters
8895 */
8896void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8897 unsigned int bitlen )
8898{
8899 conf->dhm_min_bitlen = bitlen;
8900}
8901#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8902
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008903#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008904/*
8905 * Set allowed/preferred hashes for handshake signatures
8906 */
8907void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8908 const int *hashes )
8909{
Hanno Becker56595f42019-06-19 16:31:38 +01008910#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008911 conf->sig_hashes = hashes;
Hanno Becker56595f42019-06-19 16:31:38 +01008912#else
8913 ((void) conf);
8914 ((void) hashes);
8915#endif /* MBEDTLS_SSL_CONF_SINGLE_SIG_HASH */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008916}
Hanno Becker947194e2017-04-07 13:25:49 +01008917#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008918
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008919#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +01008920#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008921/*
8922 * Set the allowed elliptic curves
8923 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008924void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008925 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008926{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008927 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008928}
Hanno Beckerc1096e72019-06-19 12:30:41 +01008929#endif /* MBEDTLS_SSL_CONF_SINGLE_EC */
Hanno Becker947194e2017-04-07 13:25:49 +01008930#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008931
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008932#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008933int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008934{
Hanno Becker947194e2017-04-07 13:25:49 +01008935 /* Initialize to suppress unnecessary compiler warning */
8936 size_t hostname_len = 0;
8937
8938 /* Check if new hostname is valid before
8939 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008940 if( hostname != NULL )
8941 {
8942 hostname_len = strlen( hostname );
8943
8944 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8945 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8946 }
8947
8948 /* Now it's clear that we will overwrite the old hostname,
8949 * so we can free it safely */
8950
8951 if( ssl->hostname != NULL )
8952 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008953 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008954 mbedtls_free( ssl->hostname );
8955 }
8956
8957 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008958
Paul Bakker5121ce52009-01-03 21:22:43 +00008959 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008960 {
8961 ssl->hostname = NULL;
8962 }
8963 else
8964 {
8965 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008966 if( ssl->hostname == NULL )
8967 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008968
Hanno Becker947194e2017-04-07 13:25:49 +01008969 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008970
Hanno Becker947194e2017-04-07 13:25:49 +01008971 ssl->hostname[hostname_len] = '\0';
8972 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008973
8974 return( 0 );
8975}
Teppo Järvelin4009d8f2019-08-19 14:48:09 +03008976#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008977
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008978#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008979void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008981 const unsigned char *, size_t),
8982 void *p_sni )
8983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008984 conf->f_sni = f_sni;
8985 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008989#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008990int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008991{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008992 size_t cur_len, tot_len;
8993 const char **p;
8994
8995 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008996 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8997 * MUST NOT be truncated."
8998 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008999 */
9000 tot_len = 0;
9001 for( p = protos; *p != NULL; p++ )
9002 {
9003 cur_len = strlen( *p );
9004 tot_len += cur_len;
9005
9006 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009008 }
9009
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009010 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009011
9012 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009013}
9014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009016{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009017 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009018}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009020
Hanno Becker33b9b252019-07-05 11:23:25 +01009021#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER) || \
9022 !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
9023void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf,
9024 int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009025{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009026 conf->max_major_ver = major;
9027 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009028}
Hanno Becker33b9b252019-07-05 11:23:25 +01009029#endif /* MBEDTLS_SSL_CONF_MAX_MINOR_VER ||
9030 MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
Paul Bakker490ecc82011-10-06 13:04:09 +00009031
Hanno Becker33b9b252019-07-05 11:23:25 +01009032#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER) || \
9033 !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
9034void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf,
9035 int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009036{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009037 conf->min_major_ver = major;
9038 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009039}
Hanno Becker33b9b252019-07-05 11:23:25 +01009040#endif /* MBEDTLS_SSL_CONF_MIN_MINOR_VER ||
9041 MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
Paul Bakker1d29fb52012-09-28 13:28:45 +00009042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009044void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009045{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009046 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009047}
9048#endif
9049
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +01009050#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +01009051void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9052 char cert_req_ca_list )
9053{
9054 conf->cert_req_ca_list = cert_req_ca_list;
9055}
9056#endif
9057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009058#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009059void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009060{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009061 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009062}
9063#endif
9064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01009066#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009067void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009068{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009069 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009070}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009071#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
9072#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009073void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03009074 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03009075{
9076 conf->enforce_extended_master_secret = ems_enf;
9077}
Hanno Beckerf765ce62019-06-21 13:17:14 +01009078#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01009079#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009080
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009081#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009082void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009083{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009084 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009085}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009086#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009089int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009092 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009095 }
9096
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009097 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009098
9099 return( 0 );
9100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009104void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009105{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009106 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009111void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009112{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009113 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009114}
9115#endif
9116
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009117#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009118void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009120 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009121}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01009122#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009125void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009127 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009128}
9129
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009130void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009132 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009133}
9134
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009135void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009136 const unsigned char period[8] )
9137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009138 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009143#if defined(MBEDTLS_SSL_CLI_C)
9144void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009145{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009146 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009147}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009148#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009149
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009150#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009151void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9152 mbedtls_ssl_ticket_write_t *f_ticket_write,
9153 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9154 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009155{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009156 conf->f_ticket_write = f_ticket_write;
9157 conf->f_ticket_parse = f_ticket_parse;
9158 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009159}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009162
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009163#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9164void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9165 mbedtls_ssl_export_keys_t *f_export_keys,
9166 void *p_export_keys )
9167{
9168 conf->f_export_keys = f_export_keys;
9169 conf->p_export_keys = p_export_keys;
9170}
9171#endif
9172
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009173#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009174void mbedtls_ssl_conf_async_private_cb(
9175 mbedtls_ssl_config *conf,
9176 mbedtls_ssl_async_sign_t *f_async_sign,
9177 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9178 mbedtls_ssl_async_resume_t *f_async_resume,
9179 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009180 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009181{
9182 conf->f_async_sign_start = f_async_sign;
9183 conf->f_async_decrypt_start = f_async_decrypt;
9184 conf->f_async_resume = f_async_resume;
9185 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009186 conf->p_async_config_data = async_config_data;
9187}
9188
Gilles Peskine8f97af72018-04-26 11:46:10 +02009189void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9190{
9191 return( conf->p_async_config_data );
9192}
9193
Gilles Peskine1febfef2018-04-30 11:54:39 +02009194void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009195{
9196 if( ssl->handshake == NULL )
9197 return( NULL );
9198 else
9199 return( ssl->handshake->user_async_ctx );
9200}
9201
Gilles Peskine1febfef2018-04-30 11:54:39 +02009202void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009203 void *ctx )
9204{
9205 if( ssl->handshake != NULL )
9206 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009207}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009208#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009209
Paul Bakker5121ce52009-01-03 21:22:43 +00009210/*
9211 * SSL get accessors
9212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009213size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009214{
9215 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9216}
9217
Hanno Becker8b170a02017-10-10 11:51:19 +01009218int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9219{
9220 /*
9221 * Case A: We're currently holding back
9222 * a message for further processing.
9223 */
9224
9225 if( ssl->keep_current_message == 1 )
9226 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009228 return( 1 );
9229 }
9230
9231 /*
9232 * Case B: Further records are pending in the current datagram.
9233 */
9234
9235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009236 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01009237 ssl->in_left > ssl->next_record_offset )
9238 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009240 return( 1 );
9241 }
9242#endif /* MBEDTLS_SSL_PROTO_DTLS */
9243
9244 /*
9245 * Case C: A handshake message is being processed.
9246 */
9247
Hanno Becker8b170a02017-10-10 11:51:19 +01009248 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9249 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009251 return( 1 );
9252 }
9253
9254 /*
9255 * Case D: An application data message is being processed
9256 */
9257 if( ssl->in_offt != NULL )
9258 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009260 return( 1 );
9261 }
9262
9263 /*
9264 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009265 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009266 * we implement support for multiple alerts in single records.
9267 */
9268
9269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9270 return( 0 );
9271}
9272
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009273uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009274{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009275 if( ssl->session != NULL )
9276 return( ssl->session->verify_result );
9277
9278 if( ssl->session_negotiate != NULL )
9279 return( ssl->session_negotiate->verify_result );
9280
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009281 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009282}
9283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009285{
Hanno Beckere02758c2019-06-26 15:31:31 +01009286 int suite;
9287
Paul Bakker926c8e42013-03-06 10:23:34 +01009288 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009289 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009290
Hanno Beckere02758c2019-06-26 15:31:31 +01009291 suite = mbedtls_ssl_session_get_ciphersuite( ssl->session );
9292 return( mbedtls_ssl_get_ciphersuite_name( suite ) );
Paul Bakker72f62662011-01-16 21:27:44 +00009293}
9294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009298 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009299 {
Hanno Becker2881d802019-05-22 14:44:53 +01009300 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009303 return( "DTLSv1.0" );
9304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009306 return( "DTLSv1.2" );
9307
9308 default:
9309 return( "unknown (DTLS)" );
9310 }
9311 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009312 MBEDTLS_SSL_TRANSPORT_ELSE
9313#endif /* MBEDTLS_SSL_PROTO_DTLS */
9314#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00009315 {
Hanno Becker2881d802019-05-22 14:44:53 +01009316 switch( mbedtls_ssl_get_minor_ver( ssl ) )
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009317 {
9318 case MBEDTLS_SSL_MINOR_VERSION_0:
9319 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009320
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009321 case MBEDTLS_SSL_MINOR_VERSION_1:
9322 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009323
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009324 case MBEDTLS_SSL_MINOR_VERSION_2:
9325 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009326
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009327 case MBEDTLS_SSL_MINOR_VERSION_3:
9328 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00009329
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009330 default:
9331 return( "unknown" );
9332 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009333 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009334#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00009335}
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009338{
Hanno Becker3136ede2018-08-17 15:28:19 +01009339 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 const mbedtls_ssl_transform *transform = ssl->transform_out;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009341
Hanno Becker43395762019-05-03 14:46:38 +01009342 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9343
Hanno Becker78640902018-08-13 16:35:15 +01009344 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01009345 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347#if defined(MBEDTLS_ZLIB_SUPPORT)
9348 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9349 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009350#endif
9351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009353 {
Hanno Beckera9d5c452019-07-25 16:47:12 +01009354#if defined(MBEDTLS_GCM_C) || \
9355 defined(MBEDTLS_CCM_C) || \
9356 defined(MBEDTLS_CHACHAPOLY_C)
9357#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 case MBEDTLS_MODE_GCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009359#endif
9360#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 case MBEDTLS_MODE_CCM:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009362#endif
9363#if defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker5b559ac2018-08-03 09:40:07 +01009364 case MBEDTLS_MODE_CHACHAPOLY:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009365#endif
9366 transform_expansion =
9367 transform->ivlen - transform->fixed_ivlen + transform->taglen;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009368 break;
9369
Hanno Beckera9d5c452019-07-25 16:47:12 +01009370#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C ||
9371 MBEDTLS_CHACHAPOLY_C */
9372
9373#if defined(MBEDTLS_CIPHER_MODE_STREAM)
9374 case MBEDTLS_MODE_STREAM:
9375 transform_expansion = transform->maclen;
9376 break;
9377#endif /* MBEDTLS_CIPHER_MODE_STREAM */
9378
9379#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380 case MBEDTLS_MODE_CBC:
Hanno Beckera9d5c452019-07-25 16:47:12 +01009381 {
9382 size_t block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009383
9384 block_size = mbedtls_cipher_get_block_size(
9385 &transform->cipher_ctx_enc );
9386
Hanno Becker3136ede2018-08-17 15:28:19 +01009387 /* Expansion due to the addition of the MAC. */
9388 transform_expansion += transform->maclen;
9389
9390 /* Expansion due to the addition of CBC padding;
9391 * Theoretically up to 256 bytes, but we never use
9392 * more than the block size of the underlying cipher. */
9393 transform_expansion += block_size;
9394
9395 /* For TLS 1.1 or higher, an explicit IV is added
9396 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009397#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +01009398 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009399 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009400#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009401
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009402 break;
Hanno Beckera9d5c452019-07-25 16:47:12 +01009403 }
9404#endif /* MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009405
9406 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009409 }
9410
Hanno Beckera5a2b082019-05-15 14:03:01 +01009411#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01009412 if( transform->out_cid_len != 0 )
9413 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01009414#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01009415
Hanno Becker43395762019-05-03 14:46:38 +01009416 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009417}
9418
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009419#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9420size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9421{
9422 size_t max_len;
9423
9424 /*
9425 * Assume mfl_code is correct since it was checked when set
9426 */
Angus Grattond8213d02016-05-25 20:56:48 +10009427 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009428
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009429 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009430 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009431 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009432 {
Angus Grattond8213d02016-05-25 20:56:48 +10009433 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009434 }
9435
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009436 /* During a handshake, use the value being negotiated */
9437 if( ssl->session_negotiate != NULL &&
9438 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9439 {
9440 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9441 }
9442
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009443 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009444}
9445#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9446
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009447#if defined(MBEDTLS_SSL_PROTO_DTLS)
9448static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9449{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009450 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Hanno Becker2d9623f2019-06-13 12:07:05 +01009451 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT &&
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009452 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9453 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9454 return ( 0 );
9455
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009456 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9457 return( ssl->mtu );
9458
9459 if( ssl->mtu == 0 )
9460 return( ssl->handshake->mtu );
9461
9462 return( ssl->mtu < ssl->handshake->mtu ?
9463 ssl->mtu : ssl->handshake->mtu );
9464}
9465#endif /* MBEDTLS_SSL_PROTO_DTLS */
9466
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009467int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9468{
9469 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9470
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009471#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9472 !defined(MBEDTLS_SSL_PROTO_DTLS)
9473 (void) ssl;
9474#endif
9475
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009476#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9477 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9478
9479 if( max_len > mfl )
9480 max_len = mfl;
9481#endif
9482
9483#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009484 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009485 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009486 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009487 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9488 const size_t overhead = (size_t) ret;
9489
9490 if( ret < 0 )
9491 return( ret );
9492
9493 if( mtu <= overhead )
9494 {
9495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9496 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9497 }
9498
9499 if( max_len > mtu - overhead )
9500 max_len = mtu - overhead;
9501 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009502#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009503
Hanno Becker0defedb2018-08-10 12:35:02 +01009504#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9505 !defined(MBEDTLS_SSL_PROTO_DTLS)
9506 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009507#endif
9508
9509 return( (int) max_len );
9510}
9511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#if defined(MBEDTLS_X509_CRT_PARSE_C)
9513const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009514{
9515 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009516 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009517
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009518#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009519 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009520#else
9521 return( NULL );
9522#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009527int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9528 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009529{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009530 if( ssl == NULL ||
9531 dst == NULL ||
9532 ssl->session == NULL ||
Hanno Becker2d9623f2019-06-13 12:07:05 +01009533 mbedtls_ssl_conf_get_endpoint( ssl->conf ) != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009536 }
9537
Hanno Becker58fccf22019-02-06 14:30:46 +00009538 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009541
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009542const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9543{
9544 if( ssl == NULL )
9545 return( NULL );
9546
9547 return( ssl->session );
9548}
9549
Paul Bakker5121ce52009-01-03 21:22:43 +00009550/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009551 * Define ticket header determining Mbed TLS version
9552 * and structure of the ticket.
9553 */
9554
Hanno Becker41527622019-05-16 12:50:45 +01009555/*
Hanno Becker26829e92019-05-28 14:30:45 +01009556 * Define bitflag determining compile-time settings influencing
9557 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009558 */
9559
Hanno Becker26829e92019-05-28 14:30:45 +01009560#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009561#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009562#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009563#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009564#endif /* MBEDTLS_HAVE_TIME */
9565
9566#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009567#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009568#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009569#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009570#endif /* MBEDTLS_X509_CRT_PARSE_C */
9571
9572#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009573#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009574#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009575#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009576#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9577
9578#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009579#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009580#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009581#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009582#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9583
9584#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009585#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009586#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009587#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009588#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9589
9590#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009591#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009592#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009593#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009594#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9595
Hanno Becker41527622019-05-16 12:50:45 +01009596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9597#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9598#else
9599#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9600#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9601
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009602#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9603#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9604#else
9605#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9606#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9607
Hanno Becker88440552019-07-03 14:16:13 +01009608#if defined(MBEDTLS_ZLIB_SUPPORT)
9609#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 1
9610#else
9611#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION 0
9612#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9613
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009614#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9615#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9616#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9617#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9618#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9619#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9620#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009621#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Becker88440552019-07-03 14:16:13 +01009622#define SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT 8
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009623
Hanno Becker26829e92019-05-28 14:30:45 +01009624#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009625 ( (uint16_t) ( \
9626 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9627 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9628 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9629 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9630 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9631 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009632 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
Hanno Becker88440552019-07-03 14:16:13 +01009633 ( SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION << SSL_SERIALIZED_SESSION_CONFIG_COMPRESSION_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009634 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009635
Hanno Becker557fe9f2019-05-16 12:41:07 +01009636static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009637 MBEDTLS_VERSION_MAJOR,
9638 MBEDTLS_VERSION_MINOR,
9639 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009640 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9641 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009642};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009643
9644/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009645 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009646 * (in the presentation language of TLS, RFC 8446 section 3)
9647 *
Hanno Becker26829e92019-05-28 14:30:45 +01009648 * opaque mbedtls_version[3]; // major, minor, patch
9649 * opaque session_format[2]; // version-specific 16-bit field determining
9650 * // the format of the remaining
9651 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009652 *
9653 * Note: When updating the format, remember to keep
9654 * these version+format bytes.
9655 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009656 * // In this version, `session_format` determines
9657 * // the setting of those compile-time
9658 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009659 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009660 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009661 * uint8 ciphersuite[2]; // defined by the standard
9662 * uint8 compression; // 0 or 1
9663 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009664 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009665 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009666 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009667 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9668 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9669 * case disabled: uint8_t peer_cert_digest_type;
9670 * opaque peer_cert_digest<0..2^8-1>;
9671 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009672 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009673 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009674 * uint8 mfl_code; // up to 255 according to standard
9675 * uint8 trunc_hmac; // 0 or 1
9676 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009677 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009678 * The order is the same as in the definition of the structure, except
9679 * verify_result is put before peer_cert so that all mandatory fields come
9680 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009681 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009682static int ssl_session_save( const mbedtls_ssl_session *session,
9683 unsigned char omit_header,
9684 unsigned char *buf,
9685 size_t buf_len,
9686 size_t *olen )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009687{
9688 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009689 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009690#if defined(MBEDTLS_HAVE_TIME)
9691 uint64_t start;
9692#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009693#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009694#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009695 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009696#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009697#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009698
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009699 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009700 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009701 /*
9702 * Add version identifier
9703 */
9704
9705 used += sizeof( ssl_serialized_session_header );
9706
9707 if( used <= buf_len )
9708 {
9709 memcpy( p, ssl_serialized_session_header,
9710 sizeof( ssl_serialized_session_header ) );
9711 p += sizeof( ssl_serialized_session_header );
9712 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009713 }
9714
9715 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009716 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009717 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009718#if defined(MBEDTLS_HAVE_TIME)
9719 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009720
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009721 if( used <= buf_len )
9722 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009723 start = (uint64_t) session->start;
9724
9725 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9726 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9727 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9728 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9729 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9730 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9731 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9732 *p++ = (unsigned char)( ( start ) & 0xFF );
9733 }
9734#endif /* MBEDTLS_HAVE_TIME */
9735
9736 /*
9737 * Basic mandatory fields
9738 */
Hanno Becker88440552019-07-03 14:16:13 +01009739 {
9740 size_t const ciphersuite_len = 2;
9741#if defined(MBEDTLS_ZLIB_SUPPORT)
9742 size_t const compression_len = 1;
9743#else
9744 size_t const compression_len = 0;
9745#endif
9746 size_t const id_len_len = 1;
9747 size_t const id_len = 32;
9748 size_t const master_len = 48;
9749 size_t const verif_result_len = 4;
9750
9751 size_t const basic_len =
9752 ciphersuite_len +
9753 compression_len +
9754 id_len_len +
9755 id_len +
9756 master_len +
9757 verif_result_len;
9758
9759 used += basic_len;
9760 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009761
9762 if( used <= buf_len )
9763 {
Hanno Beckere02758c2019-06-26 15:31:31 +01009764 const int ciphersuite =
9765 mbedtls_ssl_session_get_ciphersuite( session );
9766 *p++ = (unsigned char)( ( ciphersuite >> 8 ) & 0xFF );
9767 *p++ = (unsigned char)( ( ciphersuite ) & 0xFF );
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009768
Hanno Becker88440552019-07-03 14:16:13 +01009769#if defined(MBEDTLS_ZLIB_SUPPORT)
9770 *p++ = (unsigned char)(
9771 mbedtls_ssl_session_get_compression( session ) );
9772#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009773
9774 *p++ = (unsigned char)( session->id_len & 0xFF );
9775 memcpy( p, session->id, 32 );
9776 p += 32;
9777
9778 memcpy( p, session->master, 48 );
9779 p += 48;
9780
9781 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9782 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9783 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9784 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009785 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009786
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009787 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009788 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009789 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009790#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009791#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009792 if( session->peer_cert == NULL )
9793 cert_len = 0;
9794 else
9795 cert_len = session->peer_cert->raw.len;
9796
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009797 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009798
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009799 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009800 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009801 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9802 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9803 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9804
9805 if( session->peer_cert != NULL )
9806 {
9807 memcpy( p, session->peer_cert->raw.p, cert_len );
9808 p += cert_len;
9809 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009810 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009811
Hanno Becker5882dd02019-06-06 16:25:57 +01009812#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009813 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009814 if( session->peer_cert_digest != NULL )
9815 {
9816 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9817 if( used <= buf_len )
9818 {
9819 *p++ = (unsigned char) session->peer_cert_digest_type;
9820 *p++ = (unsigned char) session->peer_cert_digest_len;
9821 memcpy( p, session->peer_cert_digest,
9822 session->peer_cert_digest_len );
9823 p += session->peer_cert_digest_len;
9824 }
9825 }
9826 else
9827 {
9828 used += 2;
9829 if( used <= buf_len )
9830 {
9831 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9832 *p++ = 0;
9833 }
9834 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009835#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009836#endif /* MBEDTLS_X509_CRT_PARSE_C */
9837
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009838 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009839 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009840 */
9841#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009842 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009843
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009844 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009845 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009846 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9847 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9848 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9849
9850 if( session->ticket != NULL )
9851 {
9852 memcpy( p, session->ticket, session->ticket_len );
9853 p += session->ticket_len;
9854 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009855
9856 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9857 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9858 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9859 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009860 }
9861#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9862
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009863 /*
9864 * Misc extension-related info
9865 */
9866#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9867 used += 1;
9868
9869 if( used <= buf_len )
9870 *p++ = session->mfl_code;
9871#endif
9872
9873#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9874 used += 1;
9875
9876 if( used <= buf_len )
9877 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9878#endif
9879
9880#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9881 used += 1;
9882
9883 if( used <= buf_len )
9884 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9885#endif
9886
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009887 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009888 *olen = used;
9889
9890 if( used > buf_len )
9891 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009892
9893 return( 0 );
9894}
9895
9896/*
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009897 * Public wrapper for ssl_session_save()
9898 */
9899int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9900 unsigned char *buf,
9901 size_t buf_len,
9902 size_t *olen )
9903{
9904 return( ssl_session_save( session, 0, buf, buf_len, olen ) );
9905}
9906
9907/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +02009908 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009909 *
9910 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009911 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009912 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009913static int ssl_session_load( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009914 unsigned char omit_header,
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009915 const unsigned char *buf,
9916 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009917{
9918 const unsigned char *p = buf;
9919 const unsigned char * const end = buf + len;
Hanno Beckere02758c2019-06-26 15:31:31 +01009920 int ciphersuite;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009921#if defined(MBEDTLS_HAVE_TIME)
9922 uint64_t start;
9923#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009924#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009925#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009926 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009927#endif
9928#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009929
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009930 if( !omit_header )
Hanno Beckerb5352f02019-05-16 12:39:07 +01009931 {
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +02009932 /*
9933 * Check version identifier
9934 */
9935
9936 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
9937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9938
9939 if( memcmp( p, ssl_serialized_session_header,
9940 sizeof( ssl_serialized_session_header ) ) != 0 )
9941 {
9942 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
9943 }
9944 p += sizeof( ssl_serialized_session_header );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009945 }
Hanno Beckerb5352f02019-05-16 12:39:07 +01009946
9947 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009948 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009949 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009950#if defined(MBEDTLS_HAVE_TIME)
9951 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009952 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9953
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009954 start = ( (uint64_t) p[0] << 56 ) |
9955 ( (uint64_t) p[1] << 48 ) |
9956 ( (uint64_t) p[2] << 40 ) |
9957 ( (uint64_t) p[3] << 32 ) |
9958 ( (uint64_t) p[4] << 24 ) |
9959 ( (uint64_t) p[5] << 16 ) |
9960 ( (uint64_t) p[6] << 8 ) |
9961 ( (uint64_t) p[7] );
9962 p += 8;
9963
9964 session->start = (time_t) start;
9965#endif /* MBEDTLS_HAVE_TIME */
9966
9967 /*
9968 * Basic mandatory fields
9969 */
Hanno Becker88440552019-07-03 14:16:13 +01009970 {
9971 size_t const ciphersuite_len = 2;
9972#if defined(MBEDTLS_ZLIB_SUPPORT)
9973 size_t const compression_len = 1;
9974#else
9975 size_t const compression_len = 0;
9976#endif
9977 size_t const id_len_len = 1;
9978 size_t const id_len = 32;
9979 size_t const master_len = 48;
9980 size_t const verif_result_len = 4;
Hanno Beckere02758c2019-06-26 15:31:31 +01009981
Hanno Becker88440552019-07-03 14:16:13 +01009982 size_t const basic_len =
9983 ciphersuite_len +
9984 compression_len +
9985 id_len_len +
9986 id_len +
9987 master_len +
9988 verif_result_len;
9989
9990 if( basic_len > (size_t)( end - p ) )
9991 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9992 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009993
Hanno Beckere02758c2019-06-26 15:31:31 +01009994 ciphersuite = ( p[0] << 8 ) | p[1];
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009995 p += 2;
9996
Hanno Becker73f4cb12019-06-27 13:51:07 +01009997#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Hanno Beckere02758c2019-06-26 15:31:31 +01009998 session->ciphersuite = ciphersuite;
9999#else
10000 if( ciphersuite !=
Hanno Becker73f4cb12019-06-27 13:51:07 +010010001 MBEDTLS_SSL_SUITE_ID( MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE ) )
Hanno Beckere02758c2019-06-26 15:31:31 +010010002 {
10003 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
10004 }
10005#endif
10006
Hanno Becker88440552019-07-03 14:16:13 +010010007#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010008 session->compression = *p++;
Hanno Becker88440552019-07-03 14:16:13 +010010009#endif
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010010
10011 session->id_len = *p++;
10012 memcpy( session->id, p, 32 );
10013 p += 32;
10014
10015 memcpy( session->master, p, 48 );
10016 p += 48;
10017
10018 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10019 ( (uint32_t) p[1] << 16 ) |
10020 ( (uint32_t) p[2] << 8 ) |
10021 ( (uint32_t) p[3] );
10022 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010023
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010024 /* Immediately clear invalid pointer values that have been read, in case
10025 * we exit early before we replaced them with valid ones. */
10026#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010027#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010028 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010029#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010030 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +010010031#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010032#endif
10033#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10034 session->ticket = NULL;
10035#endif
10036
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010037 /*
10038 * Peer certificate
10039 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +000010041#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010042 if( 3 > (size_t)( end - p ) )
10043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10044
10045 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10046 p += 3;
10047
10048 if( cert_len == 0 )
10049 {
10050 session->peer_cert = NULL;
10051 }
10052 else
10053 {
10054 int ret;
10055
10056 if( cert_len > (size_t)( end - p ) )
10057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10058
10059 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10060
10061 if( session->peer_cert == NULL )
10062 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10063
10064 mbedtls_x509_crt_init( session->peer_cert );
10065
10066 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10067 p, cert_len ) ) != 0 )
10068 {
10069 mbedtls_x509_crt_free( session->peer_cert );
10070 mbedtls_free( session->peer_cert );
10071 session->peer_cert = NULL;
10072 return( ret );
10073 }
10074
10075 p += cert_len;
10076 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010077#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010078 /* Deserialize CRT digest from the end of the ticket. */
10079 if( 2 > (size_t)( end - p ) )
10080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10081
10082 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10083 session->peer_cert_digest_len = (size_t) *p++;
10084
10085 if( session->peer_cert_digest_len != 0 )
10086 {
Hanno Becker2326d202019-06-06 14:54:55 +010010087 const mbedtls_md_info_t *md_info =
10088 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10089 if( md_info == NULL )
10090 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10091 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10093
Hanno Becker4a2f8e52019-02-06 15:23:38 +000010094 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10096
10097 session->peer_cert_digest =
10098 mbedtls_calloc( 1, session->peer_cert_digest_len );
10099 if( session->peer_cert_digest == NULL )
10100 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10101
10102 memcpy( session->peer_cert_digest, p,
10103 session->peer_cert_digest_len );
10104 p += session->peer_cert_digest_len;
10105 }
Hanno Becker5882dd02019-06-06 16:25:57 +010010106#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010107#endif /* MBEDTLS_X509_CRT_PARSE_C */
10108
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010109 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010110 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010111 */
10112#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10113 if( 3 > (size_t)( end - p ) )
10114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10115
10116 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10117 p += 3;
10118
10119 if( session->ticket_len != 0 )
10120 {
10121 if( session->ticket_len > (size_t)( end - p ) )
10122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10123
10124 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10125 if( session->ticket == NULL )
10126 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10127
10128 memcpy( session->ticket, p, session->ticket_len );
10129 p += session->ticket_len;
10130 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010131
10132 if( 4 > (size_t)( end - p ) )
10133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10134
10135 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10136 ( (uint32_t) p[1] << 16 ) |
10137 ( (uint32_t) p[2] << 8 ) |
10138 ( (uint32_t) p[3] );
10139 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010140#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10141
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +020010142 /*
10143 * Misc extension-related info
10144 */
10145#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10146 if( 1 > (size_t)( end - p ) )
10147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10148
10149 session->mfl_code = *p++;
10150#endif
10151
10152#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10153 if( 1 > (size_t)( end - p ) )
10154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10155
10156 session->trunc_hmac = *p++;
10157#endif
10158
10159#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10160 if( 1 > (size_t)( end - p ) )
10161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10162
10163 session->encrypt_then_mac = *p++;
10164#endif
10165
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +020010166 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +020010167 if( p != end )
10168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10169
10170 return( 0 );
10171}
10172
10173/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020010174 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010175 */
10176int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10177 const unsigned char *buf,
10178 size_t len )
10179{
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020010180 int ret = ssl_session_load( session, 0, buf, len );
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +020010181
10182 if( ret != 0 )
10183 mbedtls_ssl_session_free( session );
10184
10185 return( ret );
10186}
10187
10188/*
Paul Bakker1961b702013-01-25 14:49:24 +010010189 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010191int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010194
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010195 if( ssl == NULL || ssl->conf == NULL )
10196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010199 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010203 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010205#endif
10206
Hanno Beckerb82350b2019-07-26 07:24:05 +010010207 ssl_send_pending_fatal_alert( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010208 return( ret );
10209}
10210
10211/*
10212 * Perform the SSL handshake
10213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010215{
10216 int ret = 0;
10217
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010218 if( ssl == NULL || ssl->conf == NULL )
10219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010226
10227 if( ret != 0 )
10228 break;
10229 }
10230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010232
10233 return( ret );
10234}
10235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236#if defined(MBEDTLS_SSL_RENEGOTIATION)
10237#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010238/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010239 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010242{
10243 int ret;
10244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010246
10247 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10249 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010250
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010251 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010252 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010254 return( ret );
10255 }
10256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010258
10259 return( 0 );
10260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010262
10263/*
10264 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010265 * - any side: calling mbedtls_ssl_renegotiate(),
10266 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10267 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010268 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010269 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010273{
10274 int ret;
10275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010277
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010278 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10279 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010280
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010281 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10282 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010284 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010286 {
Hanno Becker2d9623f2019-06-13 12:07:05 +010010287 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10288 MBEDTLS_SSL_IS_SERVER )
10289 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010290 ssl->handshake->out_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010291 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010292 else
Hanno Becker2d9623f2019-06-13 12:07:05 +010010293 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010294 ssl->handshake->in_msg_seq = 1;
Hanno Becker2d9623f2019-06-13 12:07:05 +010010295 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010296 }
10297#endif
10298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010299 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10300 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010304 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010305 return( ret );
10306 }
10307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010309
10310 return( 0 );
10311}
10312
10313/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010314 * Renegotiate current connection on client,
10315 * or request renegotiation on server
10316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010317int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010320
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010321 if( ssl == NULL || ssl->conf == NULL )
10322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010325 /* On server, just send the request */
Hanno Becker2d9623f2019-06-13 12:07:05 +010010326 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010332
10333 /* Did we already try/start sending HelloRequest? */
10334 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010336
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010337 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010338 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010342 /*
10343 * On client, either start the renegotiation process or,
10344 * if already in progress, continue the handshake
10345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010346 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10349 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010350
10351 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010354 return( ret );
10355 }
10356 }
10357 else
10358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010362 return( ret );
10363 }
10364 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010366
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010367 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010368}
10369
10370/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010371 * Check record counters and renegotiate if they're above the limit.
10372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010374{
Andres AG2196c7f2016-12-15 17:01:16 +000010375 size_t ep_len = ssl_ep_len( ssl );
10376 int in_ctr_cmp;
10377 int out_ctr_cmp;
10378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010379 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10380 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020010381 ! mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010382 {
10383 return( 0 );
10384 }
10385
Andres AG2196c7f2016-12-15 17:01:16 +000010386 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10387 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010388 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010389 ssl->conf->renego_period + ep_len, 8 - ep_len );
10390
10391 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010392 {
10393 return( 0 );
10394 }
10395
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010400
10401/*
10402 * Receive application data decrypted from the SSL layer
10403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010405{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010406 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010407 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010408
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010409 if( ssl == NULL || ssl->conf == NULL )
10410 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010415 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010418 return( ret );
10419
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010420 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010422 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010423 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010424 return( ret );
10425 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010426 }
10427#endif
10428
Hanno Becker4a810fb2017-05-24 16:27:30 +010010429 /*
10430 * Check if renegotiation is necessary and/or handshake is
10431 * in process. If yes, perform/continue, and fall through
10432 * if an unexpected packet is received while the client
10433 * is waiting for the ServerHello.
10434 *
10435 * (There is no equivalent to the last condition on
10436 * the server-side as it is not treated as within
10437 * a handshake while waiting for the ClientHello
10438 * after a renegotiation request.)
10439 */
10440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010442 ret = ssl_check_ctr_renegotiate( ssl );
10443 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10444 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010446 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010447 return( ret );
10448 }
10449#endif
10450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010454 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10455 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010458 return( ret );
10459 }
10460 }
10461
Hanno Beckere41158b2017-10-23 13:30:32 +010010462 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010463 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010464 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010465 /* Start timer if not already running */
Hanno Becker0ae6b242019-06-13 16:45:36 +010010466 if( mbedtls_ssl_get_get_timer( ssl ) != NULL &&
10467 mbedtls_ssl_get_get_timer( ssl )( ssl->p_timer ) == -1 )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010468 {
Hanno Becker1f835fa2019-06-13 10:14:59 +010010469 ssl_set_timer( ssl,
10470 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010471 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010472
Hanno Becker327c93b2018-08-15 13:56:18 +010010473 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010474 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010475 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10476 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010477
Hanno Becker4a810fb2017-05-24 16:27:30 +010010478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10479 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010480 }
10481
10482 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010484 {
10485 /*
10486 * OpenSSL sends empty messages to randomize the IV
10487 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010488 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010491 return( 0 );
10492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010494 return( ret );
10495 }
10496 }
10497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010498 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010500 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010501
Hanno Becker4a810fb2017-05-24 16:27:30 +010010502 /*
10503 * - For client-side, expect SERVER_HELLO_REQUEST.
10504 * - For server-side, expect CLIENT_HELLO.
10505 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10506 */
10507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010508#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010509 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10510 MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010512 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010515
10516 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010518 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010519 {
10520 continue;
10521 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010522 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010523#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010524#if defined(MBEDTLS_SSL_PROTO_TLS)
10525 {
10526 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10527 }
10528#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010529 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010530#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010531
Hanno Becker4a810fb2017-05-24 16:27:30 +010010532#if defined(MBEDTLS_SSL_SRV_C)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010533 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10534 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010538
10539 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010541 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010542 {
10543 continue;
10544 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010545 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010546#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010547#if defined(MBEDTLS_SSL_PROTO_TLS)
10548 {
10549 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10550 }
10551#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010552 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010553#endif /* MBEDTLS_SSL_SRV_C */
10554
Hanno Becker21df7f92017-10-17 11:03:26 +010010555#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010556 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010557 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10558 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010559 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010560 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10561 {
10562 /*
10563 * Accept renegotiation request
10564 */
Paul Bakker48916f92012-09-16 19:57:18 +000010565
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010566 /* DTLS clients need to know renego is server-initiated */
10567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010568 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker2d9623f2019-06-13 12:07:05 +010010569 mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10570 MBEDTLS_SSL_IS_CLIENT )
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010571 {
10572 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10573 }
10574#endif
10575 ret = ssl_start_renegotiation( ssl );
10576 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10577 ret != 0 )
10578 {
10579 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10580 return( ret );
10581 }
10582 }
10583 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010584#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010585 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010586 /*
10587 * Refuse renegotiation
10588 */
10589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010590 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2881d802019-05-22 14:44:53 +010010593 if( mbedtls_ssl_get_minor_ver( ssl ) == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010594 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010595 /* SSLv3 does not have a "no_renegotiation" warning, so
10596 we send a fatal alert and abort the connection. */
10597 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10598 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10599 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010600 }
10601 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10603#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10604 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2881d802019-05-22 14:44:53 +010010605 if( mbedtls_ssl_get_minor_ver( ssl ) >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010606 {
Hanno Becker2e8d1332019-07-25 10:27:36 +010010607 ret = mbedtls_ssl_send_alert_message( ssl,
10608 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10609 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION );
10610 if( ret != 0 )
10611 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010612 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10615 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010619 }
Paul Bakker48916f92012-09-16 19:57:18 +000010620 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010621
Hanno Becker90333da2017-10-10 11:27:13 +010010622 /* At this point, we don't know whether the renegotiation has been
10623 * completed or not. The cases to consider are the following:
10624 * 1) The renegotiation is complete. In this case, no new record
10625 * has been read yet.
10626 * 2) The renegotiation is incomplete because the client received
10627 * an application data record while awaiting the ServerHello.
10628 * 3) The renegotiation is incomplete because the client received
10629 * a non-handshake, non-application data message while awaiting
10630 * the ServerHello.
10631 * In each of these case, looping will be the proper action:
10632 * - For 1), the next iteration will read a new record and check
10633 * if it's application data.
10634 * - For 2), the loop condition isn't satisfied as application data
10635 * is present, hence continue is the same as break
10636 * - For 3), the loop condition is satisfied and read_record
10637 * will re-deliver the message that was held back by the client
10638 * when expecting the ServerHello.
10639 */
10640 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010641 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010642#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010643 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010644 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010645 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010646 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010647 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010650 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010652 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010653 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010654 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010657 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10658 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010661 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010662 }
10663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010664 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10667 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010668 }
10669
10670 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010671
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010672 /* We're going to return something now, cancel timer,
10673 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010675 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010676
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010678 /* If we requested renego but received AppData, resend HelloRequest.
10679 * Do it now, after setting in_offt, to avoid taking this branch
10680 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010681#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker2d9623f2019-06-13 12:07:05 +010010682 if( mbedtls_ssl_conf_get_endpoint( ssl->conf ) ==
10683 MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010685 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010686 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010688 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010689 return( ret );
10690 }
10691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010693#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010694 }
10695
10696 n = ( len < ssl->in_msglen )
10697 ? len : ssl->in_msglen;
10698
10699 memcpy( buf, ssl->in_offt, n );
10700 ssl->in_msglen -= n;
10701
10702 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010703 {
10704 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010705 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010706 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010707 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010708 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010709 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010710 /* more data available */
10711 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010712 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010715
Paul Bakker23986e52011-04-24 08:57:21 +000010716 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010717}
10718
10719/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010720 * Send application data to be encrypted by the SSL layer, taking care of max
10721 * fragment length and buffer size.
10722 *
10723 * According to RFC 5246 Section 6.2.1:
10724 *
10725 * Zero-length fragments of Application data MAY be sent as they are
10726 * potentially useful as a traffic analysis countermeasure.
10727 *
10728 * Therefore, it is possible that the input message length is 0 and the
10729 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010730 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010731static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010732 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010733{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010734 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10735 const size_t max_len = (size_t) ret;
10736
10737 if( ret < 0 )
10738 {
10739 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10740 return( ret );
10741 }
10742
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010743 if( len > max_len )
10744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010746 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010749 "maximum fragment length: %d > %d",
10750 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010752 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010753 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010754#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010755#if defined(MBEDTLS_SSL_PROTO_TLS)
10756 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010757 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010758 }
10759#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010760 }
Paul Bakker887bd502011-06-08 13:10:54 +000010761
Paul Bakker5121ce52009-01-03 21:22:43 +000010762 if( ssl->out_left != 0 )
10763 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010764 /*
10765 * The user has previously tried to send the data and
10766 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10767 * written. In this case, we expect the high-level write function
10768 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010770 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010773 return( ret );
10774 }
10775 }
Paul Bakker887bd502011-06-08 13:10:54 +000010776 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010777 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010778 /*
10779 * The user is trying to send a message the first time, so we need to
10780 * copy the data into the internal buffers and setup the data structure
10781 * to keep track of partial writes
10782 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010783 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010784 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010785 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010786
Hanno Becker67bc7c32018-08-06 11:33:50 +010010787 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010790 return( ret );
10791 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010792 }
10793
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010794 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010795}
10796
10797/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010798 * Write application data, doing 1/n-1 splitting if necessary.
10799 *
10800 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010801 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010802 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010804#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010805static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010806 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010807{
10808 int ret;
10809
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010810 if( ssl->conf->cbc_record_splitting ==
10811 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010812 len <= 1 ||
Hanno Becker2881d802019-05-22 14:44:53 +010010813 mbedtls_ssl_get_minor_ver( ssl ) > MBEDTLS_SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10815 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010816 {
10817 return( ssl_write_real( ssl, buf, len ) );
10818 }
10819
10820 if( ssl->split_done == 0 )
10821 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010822 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010823 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010824 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010825 }
10826
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010827 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10828 return( ret );
10829 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010830
10831 return( ret + 1 );
10832}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010834
10835/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010836 * Write application data (public-facing wrapper)
10837 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010838int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010839{
10840 int ret;
10841
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010843
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010844 if( ssl == NULL || ssl->conf == NULL )
10845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10846
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010847#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010848 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10849 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010850 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010851 return( ret );
10852 }
10853#endif
10854
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010855 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010856 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010857 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010858 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010860 return( ret );
10861 }
10862 }
10863
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010864#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010865 ret = ssl_write_split( ssl, buf, len );
10866#else
10867 ret = ssl_write_real( ssl, buf, len );
10868#endif
10869
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010871
10872 return( ret );
10873}
10874
10875/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010876 * Notify the peer that the connection is being closed
10877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010878int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010879{
10880 int ret;
10881
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010882 if( ssl == NULL || ssl->conf == NULL )
10883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010886
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010887 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010890 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010892 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10893 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10894 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010897 return( ret );
10898 }
10899 }
10900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010902
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010903 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010904}
10905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010907{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010908 if( transform == NULL )
10909 return;
10910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010912 deflateEnd( &transform->ctx_deflate );
10913 inflateEnd( &transform->ctx_inflate );
10914#endif
10915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010916 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10917 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010918
Hanno Becker92231322018-01-03 15:32:51 +000010919#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920 mbedtls_md_free( &transform->md_ctx_enc );
10921 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010922#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010923
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010924 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010925}
10926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010927#if defined(MBEDTLS_X509_CRT_PARSE_C)
10928static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010930 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010931
10932 while( cur != NULL )
10933 {
10934 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010936 cur = next;
10937 }
10938}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010940
Hanno Becker0271f962018-08-16 13:23:47 +010010941#if defined(MBEDTLS_SSL_PROTO_DTLS)
10942
10943static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10944{
10945 unsigned offset;
10946 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10947
10948 if( hs == NULL )
10949 return;
10950
Hanno Becker283f5ef2018-08-24 09:34:47 +010010951 ssl_free_buffered_record( ssl );
10952
Hanno Becker0271f962018-08-16 13:23:47 +010010953 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010954 ssl_buffering_free_slot( ssl, offset );
10955}
10956
10957static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10958 uint8_t slot )
10959{
10960 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10961 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010962
10963 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10964 return;
10965
Hanno Beckere605b192018-08-21 15:59:07 +010010966 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010967 {
Hanno Beckere605b192018-08-21 15:59:07 +010010968 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010969 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010970 mbedtls_free( hs_buf->data );
10971 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010972 }
10973}
10974
10975#endif /* MBEDTLS_SSL_PROTO_DTLS */
10976
Gilles Peskine9b562d52018-04-25 20:32:43 +020010977void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010978{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010979 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10980
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010981 if( handshake == NULL )
10982 return;
10983
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010984#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10985 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10986 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010987 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010988 handshake->async_in_progress = 0;
10989 }
10990#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10991
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010992#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10993 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10994 mbedtls_md5_free( &handshake->fin_md5 );
10995 mbedtls_sha1_free( &handshake->fin_sha1 );
10996#endif
10997#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10998#if defined(MBEDTLS_SHA256_C)
10999 mbedtls_sha256_free( &handshake->fin_sha256 );
11000#endif
11001#if defined(MBEDTLS_SHA512_C)
11002 mbedtls_sha512_free( &handshake->fin_sha512 );
11003#endif
11004#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006#if defined(MBEDTLS_DHM_C)
11007 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011009#if defined(MBEDTLS_ECDH_C)
11010 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011011#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011012#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011013 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011014#if defined(MBEDTLS_SSL_CLI_C)
11015 mbedtls_free( handshake->ecjpake_cache );
11016 handshake->ecjpake_cache = NULL;
11017 handshake->ecjpake_cache_len = 0;
11018#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011019#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011020
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011021#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11022 if( handshake->psk != NULL )
11023 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011024 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011025 mbedtls_free( handshake->psk );
11026 }
11027#endif
11028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11030 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011031 /*
11032 * Free only the linked list wrapper, not the keys themselves
11033 * since the belong to the SNI callback
11034 */
11035 if( handshake->sni_key_cert != NULL )
11036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011038
11039 while( cur != NULL )
11040 {
11041 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011042 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011043 cur = next;
11044 }
11045 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011047
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011048#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011049 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000011050 if( handshake->ecrs_peer_cert != NULL )
11051 {
11052 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11053 mbedtls_free( handshake->ecrs_peer_cert );
11054 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011055#endif
11056
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000011057#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11058 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11059 mbedtls_pk_free( &handshake->peer_pubkey );
11060#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062#if defined(MBEDTLS_SSL_PROTO_DTLS)
11063 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011064 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011065 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011066#endif
11067
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011068 mbedtls_platform_zeroize( handshake,
11069 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011070}
11071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011073{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011074 if( session == NULL )
11075 return;
11076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011077#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000011078 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011079#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011080
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011081#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011082 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011083#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011084
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011085 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011086}
11087
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011088#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011089
11090#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11091#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
11092#else
11093#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
11094#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11095
11096#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11097#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
11098#else
11099#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
11100#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11101
11102#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11103#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
11104#else
11105#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
11106#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11107
11108#if defined(MBEDTLS_SSL_ALPN)
11109#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
11110#else
11111#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
11112#endif /* MBEDTLS_SSL_ALPN */
11113
11114#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
11115#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
11116#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
11117#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
11118
11119#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
11120 ( (uint32_t) ( \
11121 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
11122 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
11123 ( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
11124 ( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
11125 0u ) )
11126
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011127static unsigned char ssl_serialized_context_header[] = {
11128 MBEDTLS_VERSION_MAJOR,
11129 MBEDTLS_VERSION_MINOR,
11130 MBEDTLS_VERSION_PATCH,
11131 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
11132 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011133 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
11134 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
11135 ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011136};
11137
Paul Bakker5121ce52009-01-03 21:22:43 +000011138/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011139 * Serialize a full SSL context
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011140 *
11141 * The format of the serialized data is:
11142 * (in the presentation language of TLS, RFC 8446 section 3)
11143 *
11144 * // header
11145 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011146 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011147 * // the format of the remaining
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011148 * // serialized data.
Manuel Pégourié-Gonnarda7cd4832019-07-23 16:31:16 +020011149 * Note: When updating the format, remember to keep these
11150 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnardb6163ef2019-07-10 14:58:45 +020011151 *
11152 * // session sub-structure
11153 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
11154 * // transform sub-structure
11155 * uint8 random[64]; // ServerHello.random+ClientHello.random
11156 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
11157 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
11158 * // fields from ssl_context
11159 * uint32 badmac_seen; // DTLS: number of records with failing MAC
11160 * uint64 in_window_top; // DTLS: last validated record seq_num
11161 * uint64 in_window; // DTLS: bitmask for replay protection
11162 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
11163 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
11164 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
11165 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
11166 *
11167 * Note that many fields of the ssl_context or sub-structures are not
11168 * serialized, as they fall in one of the following categories:
11169 *
11170 * 1. forced value (eg in_left must be 0)
11171 * 2. pointer to dynamically-allocated memory (eg session, transform)
11172 * 3. value can be re-derived from other data (eg session keys from MS)
11173 * 4. value was temporary (eg content of input buffer)
11174 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011175 */
11176int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
11177 unsigned char *buf,
11178 size_t buf_len,
11179 size_t *olen )
11180{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011181 unsigned char *p = buf;
11182 size_t used = 0;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011183 size_t session_len;
11184 int ret = 0;
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011185
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011186 /*
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011187 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
11188 * this function's documentation.
11189 *
11190 * These are due to assumptions/limitations in the implementation. Some of
11191 * them are likely to stay (no handshake in progress) some might go away
11192 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011193 */
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011194 /* The initial handshake must be over */
11195 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard569ed6b2019-07-10 14:14:05 +020011196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard69a3e412019-07-29 12:28:52 +020011197 if( ssl->handshake != NULL )
11198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11199 /* Double-check that sub-structures are indeed ready */
11200 if( ssl->transform == NULL || ssl->session == NULL )
11201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11202 /* There must be no pending incoming or outgoing data */
11203 if( mbedtls_ssl_check_pending( ssl ) != 0 )
11204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11205 if( ssl->out_left != 0 )
11206 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11207 /* Protocol must be DLTS, not TLS */
11208 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
11209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11210 /* Version must be 1.2 */
11211 if( mbedtls_ssl_get_major_ver( ssl ) != MBEDTLS_SSL_MAJOR_VERSION_3 )
11212 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11213 if( mbedtls_ssl_get_minor_ver( ssl ) != MBEDTLS_SSL_MINOR_VERSION_3 )
11214 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11215 /* We must be using an AEAD ciphersuite */
11216 if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
11217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11218 /* Renegotiation must not be enabled */
11219 if( mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
11220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011221
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011222 /*
11223 * Version and format identifier
11224 */
11225 used += sizeof( ssl_serialized_context_header );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011226
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011227 if( used <= buf_len )
11228 {
11229 memcpy( p, ssl_serialized_context_header,
11230 sizeof( ssl_serialized_context_header ) );
11231 p += sizeof( ssl_serialized_context_header );
11232 }
11233
11234 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011235 * Session (length + data)
11236 */
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011237 ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011238 if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
11239 return( ret );
11240
11241 used += 4 + session_len;
11242 if( used <= buf_len )
11243 {
11244 *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
11245 *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
11246 *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
11247 *p++ = (unsigned char)( ( session_len ) & 0xFF );
11248
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011249 ret = ssl_session_save( ssl->session, 1,
11250 p, session_len, &session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011251 if( ret != 0 )
11252 return( ret );
11253
11254 p += session_len;
11255 }
11256
11257 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011258 * Transform
11259 */
11260 used += sizeof( ssl->transform->randbytes );
11261 if( used <= buf_len )
11262 {
11263 memcpy( p, ssl->transform->randbytes,
11264 sizeof( ssl->transform->randbytes ) );
11265 p += sizeof( ssl->transform->randbytes );
11266 }
11267
11268#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11269 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
11270 if( used <= buf_len )
11271 {
11272 *p++ = ssl->transform->in_cid_len;
11273 memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
11274 p += ssl->transform->in_cid_len;
11275
11276 *p++ = ssl->transform->out_cid_len;
11277 memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
11278 p += ssl->transform->out_cid_len;
11279 }
11280#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11281
11282 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011283 * Saved fields from top-level ssl_context structure
11284 */
11285#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11286 used += 4;
11287 if( used <= buf_len )
11288 {
11289 *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
11290 *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
11291 *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
11292 *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
11293 }
11294#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11295
11296#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11297 used += 16;
11298 if( used <= buf_len )
11299 {
11300 *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
11301 *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
11302 *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
11303 *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
11304 *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
11305 *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
11306 *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
11307 *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
11308
11309 *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
11310 *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
11311 *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
11312 *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
11313 *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
11314 *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
11315 *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
11316 *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
11317 }
11318#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11319
11320#if defined(MBEDTLS_SSL_PROTO_DTLS)
11321 used += 1;
11322 if( used <= buf_len )
11323 {
11324 *p++ = ssl->disable_datagram_packing;
11325 }
11326#endif /* MBEDTLS_SSL_PROTO_DTLS */
11327
11328 used += 8;
11329 if( used <= buf_len )
11330 {
11331 memcpy( p, ssl->cur_out_ctr, 8 );
11332 p += 8;
11333 }
11334
11335#if defined(MBEDTLS_SSL_PROTO_DTLS)
11336 used += 2;
11337 if( used <= buf_len )
11338 {
11339 *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
11340 *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
11341 }
11342#endif /* MBEDTLS_SSL_PROTO_DTLS */
11343
11344#if defined(MBEDTLS_SSL_ALPN)
11345 {
11346 const uint8_t alpn_len = ssl->alpn_chosen
Manuel Pégourié-Gonnard7af73752019-07-24 00:58:27 +020011347 ? (uint8_t) strlen( ssl->alpn_chosen )
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011348 : 0;
11349
11350 used += 1 + alpn_len;
11351 if( used <= buf_len )
11352 {
11353 *p++ = alpn_len;
11354
11355 if( ssl->alpn_chosen != NULL )
11356 {
11357 memcpy( p, ssl->alpn_chosen, alpn_len );
11358 p += alpn_len;
11359 }
11360 }
11361 }
11362#endif /* MBEDTLS_SSL_ALPN */
11363
11364 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011365 * Done
11366 */
11367 *olen = used;
11368
11369 if( used > buf_len )
11370 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011371
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011372 MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
11373
Manuel Pégourié-Gonnardbc847ca2019-07-23 14:51:09 +020011374 return( ssl_session_reset_int( ssl, 0 ) );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011375}
11376
11377/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011378 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011379 *
11380 * This internal version is wrapped by a public function that cleans up in
11381 * case of error.
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011382 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011383static int ssl_context_load( mbedtls_ssl_context *ssl,
11384 const unsigned char *buf,
11385 size_t len )
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011386{
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011387 const unsigned char *p = buf;
11388 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011389 size_t session_len;
11390 int ret;
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011391
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011392 /*
11393 * The context should have been freshly setup or reset.
11394 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard14e2a8a2019-07-26 16:31:53 +020011395 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011396 * renegotiating, or if the user mistakenly loaded a session first.)
11397 */
11398 if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
11399 ssl->session != NULL )
11400 {
11401 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11402 }
11403
11404 /*
11405 * We can't check that the config matches the initial one, but we can at
11406 * least check it matches the requirements for serializing.
11407 */
11408 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Manuel Pégourié-Gonnard73a46362019-07-23 15:16:19 +020011409 mbedtls_ssl_conf_get_max_major_ver( ssl->conf ) <
11410 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11411 mbedtls_ssl_conf_get_min_major_ver( ssl->conf ) >
11412 MBEDTLS_SSL_MAJOR_VERSION_3 ||
11413 mbedtls_ssl_conf_get_max_minor_ver( ssl->conf ) <
11414 MBEDTLS_SSL_MINOR_VERSION_3 ||
11415 mbedtls_ssl_conf_get_min_minor_ver( ssl->conf ) >
11416 MBEDTLS_SSL_MINOR_VERSION_3 ||
Manuel Pégourié-Gonnard18332c52019-07-29 12:17:52 +020011417 mbedtls_ssl_conf_is_renegotiation_enabled( ssl->conf ) )
Manuel Pégourié-Gonnard5e534ba2019-07-11 09:56:30 +020011418 {
11419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11420 }
11421
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011422 MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
11423
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011424 /*
11425 * Check version identifier
11426 */
11427 if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
11428 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11429
11430 if( memcmp( p, ssl_serialized_context_header,
11431 sizeof( ssl_serialized_context_header ) ) != 0 )
11432 {
11433 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
11434 }
11435 p += sizeof( ssl_serialized_context_header );
11436
11437 /*
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011438 * Session
11439 */
11440 if( (size_t)( end - p ) < 4 )
11441 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11442
11443 session_len = ( (size_t) p[0] << 24 ) |
11444 ( (size_t) p[1] << 16 ) |
11445 ( (size_t) p[2] << 8 ) |
11446 ( (size_t) p[3] );
11447 p += 4;
11448
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011449 /* This has been allocated by ssl_handshake_init(), called by
11450 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11451 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011452 ssl->session_in = ssl->session;
11453 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011454 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011455
11456 if( (size_t)( end - p ) < session_len )
11457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11458
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011459 ret = ssl_session_load( ssl->session, 1, p, session_len );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011460 if( ret != 0 )
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011461 {
11462 mbedtls_ssl_session_free( ssl->session );
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011463 return( ret );
Manuel Pégourié-Gonnard7ce94462019-07-23 16:52:45 +020011464 }
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011465
11466 p += session_len;
11467
11468 /*
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011469 * Transform
11470 */
11471
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011472 /* This has been allocated by ssl_handshake_init(), called by
11473 * by either ssl_session_reset_int() or mbedtls_ssl_setup(). */
11474 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011475 ssl->transform_in = ssl->transform;
11476 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnardff222002019-07-23 14:43:30 +020011477 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011478
11479 /* Read random bytes and populate structure */
11480 if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
11481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11482
11483 ret = ssl_populate_transform( ssl->transform,
11484 mbedtls_ssl_session_get_ciphersuite( ssl->session ),
11485 ssl->session->master,
11486#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
11487#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11488 ssl->session->encrypt_then_mac,
11489#endif
11490#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
11491 ssl->session->trunc_hmac,
11492#endif
11493#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
11494#if defined(MBEDTLS_ZLIB_SUPPORT)
11495 ssl->session->compression,
11496#endif
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011497 p, /* currently pointing to randbytes */
11498 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
11499 mbedtls_ssl_conf_get_endpoint( ssl->conf ),
11500 ssl );
11501 if( ret != 0 )
11502 return( ret );
11503
11504 p += sizeof( ssl->transform->randbytes );
11505
11506#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
11507 /* Read connection IDs and store them */
11508 if( (size_t)( end - p ) < 1 )
11509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11510
11511 ssl->transform->in_cid_len = *p++;
11512
Manuel Pégourié-Gonnard2f3fa622019-07-23 15:02:54 +020011513 if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
Manuel Pégourié-Gonnard322f3c72019-07-15 09:04:11 +020011514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11515
11516 memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
11517 p += ssl->transform->in_cid_len;
11518
11519 ssl->transform->out_cid_len = *p++;
11520
11521 if( (size_t)( end - p ) < ssl->transform->out_cid_len )
11522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11523
11524 memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
11525 p += ssl->transform->out_cid_len;
11526#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
11527
11528 /*
Manuel Pégourié-Gonnard16d14852019-07-15 11:23:03 +020011529 * Saved fields from top-level ssl_context structure
11530 */
11531#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
11532 if( (size_t)( end - p ) < 4 )
11533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11534
11535 ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
11536 ( (uint32_t) p[1] << 16 ) |
11537 ( (uint32_t) p[2] << 8 ) |
11538 ( (uint32_t) p[3] );
11539 p += 4;
11540#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
11541
11542#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11543 if( (size_t)( end - p ) < 16 )
11544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11545
11546 ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
11547 ( (uint64_t) p[1] << 48 ) |
11548 ( (uint64_t) p[2] << 40 ) |
11549 ( (uint64_t) p[3] << 32 ) |
11550 ( (uint64_t) p[4] << 24 ) |
11551 ( (uint64_t) p[5] << 16 ) |
11552 ( (uint64_t) p[6] << 8 ) |
11553 ( (uint64_t) p[7] );
11554 p += 8;
11555
11556 ssl->in_window = ( (uint64_t) p[0] << 56 ) |
11557 ( (uint64_t) p[1] << 48 ) |
11558 ( (uint64_t) p[2] << 40 ) |
11559 ( (uint64_t) p[3] << 32 ) |
11560 ( (uint64_t) p[4] << 24 ) |
11561 ( (uint64_t) p[5] << 16 ) |
11562 ( (uint64_t) p[6] << 8 ) |
11563 ( (uint64_t) p[7] );
11564 p += 8;
11565#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
11566
11567#if defined(MBEDTLS_SSL_PROTO_DTLS)
11568 if( (size_t)( end - p ) < 1 )
11569 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11570
11571 ssl->disable_datagram_packing = *p++;
11572#endif /* MBEDTLS_SSL_PROTO_DTLS */
11573
11574 if( (size_t)( end - p ) < 8 )
11575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11576
11577 memcpy( ssl->cur_out_ctr, p, 8 );
11578 p += 8;
11579
11580#if defined(MBEDTLS_SSL_PROTO_DTLS)
11581 if( (size_t)( end - p ) < 2 )
11582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11583
11584 ssl->mtu = ( p[0] << 8 ) | p[1];
11585 p += 2;
11586#endif /* MBEDTLS_SSL_PROTO_DTLS */
11587
11588#if defined(MBEDTLS_SSL_ALPN)
11589 {
11590 uint8_t alpn_len;
11591 const char **cur;
11592
11593 if( (size_t)( end - p ) < 1 )
11594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11595
11596 alpn_len = *p++;
11597
11598 if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
11599 {
11600 /* alpn_chosen should point to an item in the configured list */
11601 for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
11602 {
11603 if( strlen( *cur ) == alpn_len &&
11604 memcmp( p, cur, alpn_len ) == 0 )
11605 {
11606 ssl->alpn_chosen = *cur;
11607 break;
11608 }
11609 }
11610 }
11611
11612 /* can only happen on conf mismatch */
11613 if( alpn_len != 0 && ssl->alpn_chosen == NULL )
11614 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
11615
11616 p += alpn_len;
11617 }
11618#endif /* MBEDTLS_SSL_ALPN */
11619
11620 /*
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011621 * Forced fields from top-level ssl_context structure
11622 *
11623 * Most of them already set to the correct value by mbedtls_ssl_init() and
11624 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
11625 */
11626 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
11627
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011628#if !defined(MBEDTLS_SSL_CONF_FIXED_MAJOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011629 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011630#endif /* !MBEDTLS_SSL_CONF_FIXED_MAJOR_VER */
11631#if !defined(MBEDTLS_SSL_CONF_FIXED_MINOR_VER)
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011632 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard2cc92232019-07-23 17:11:24 +020011633#endif /* !MBEDTLS_SSL_CONF_FIXED_MINOR_VER */
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011634
Hanno Becker83985822019-08-30 10:42:49 +010011635 /* Adjust pointers for header fields of outgoing records to
11636 * the given transform, accounting for explicit IV and CID. */
11637 ssl_update_out_pointers( ssl, ssl->transform );
11638
Manuel Pégourié-Gonnard138079d2019-07-15 11:53:51 +020011639#if defined(MBEDTLS_SSL_PROTO_DTLS)
11640 ssl->in_epoch = 1;
11641#endif
11642
11643 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
11644 * which we don't want - otherwise we'd end up freeing the wrong transform
11645 * by calling ssl_handshake_wrapup_free_hs_transform() inappropriately. */
11646 if( ssl->handshake != NULL )
11647 {
11648 mbedtls_ssl_handshake_free( ssl );
11649 mbedtls_free( ssl->handshake );
11650 ssl->handshake = NULL;
11651 }
11652
11653 /*
Manuel Pégourié-Gonnardd0dd1042019-07-11 10:58:10 +020011654 * Done - should have consumed entire buffer
11655 */
11656 if( p != end )
11657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011658
11659 return( 0 );
11660}
11661
11662/*
Manuel Pégourié-Gonnard81758162019-07-12 10:50:19 +020011663 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011664 */
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011665int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011666 const unsigned char *buf,
11667 size_t len )
11668{
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011669 int ret = ssl_context_load( context, buf, len );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011670
Manuel Pégourié-Gonnardf1f3e522019-07-11 12:50:53 +020011671 if( ret != 0 )
11672 mbedtls_ssl_free( context );
11673
11674 return( ret );
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011675}
Manuel Pégourié-Gonnard4c1d06e2019-07-23 16:13:17 +020011676#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020011677
11678/*
Paul Bakker5121ce52009-01-03 21:22:43 +000011679 * Free an SSL context
11680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011681void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011682{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011683 if( ssl == NULL )
11684 return;
11685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011687
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011688 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011689 {
Angus Grattond8213d02016-05-25 20:56:48 +100011690 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011691 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011692 }
11693
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011694 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011695 {
Angus Grattond8213d02016-05-25 20:56:48 +100011696 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011697 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011698 }
11699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011700#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011701 if( ssl->compress_buf != NULL )
11702 {
Angus Grattond8213d02016-05-25 20:56:48 +100011703 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011704 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011705 }
11706#endif
11707
Paul Bakker48916f92012-09-16 19:57:18 +000011708 if( ssl->transform )
11709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011710 mbedtls_ssl_transform_free( ssl->transform );
11711 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011712 }
11713
11714 if( ssl->handshake )
11715 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011716 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011717 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11718 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011720 mbedtls_free( ssl->handshake );
11721 mbedtls_free( ssl->transform_negotiate );
11722 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011723 }
11724
Paul Bakkerc0463502013-02-14 11:19:38 +010011725 if( ssl->session )
11726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011727 mbedtls_ssl_session_free( ssl->session );
11728 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011729 }
11730
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030011731#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +020011732 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011733 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011734 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011735 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011736 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011737#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011739#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11740 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11743 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011744 }
11745#endif
11746
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011747#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011748 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011749#endif
11750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011752
Paul Bakker86f04f42013-02-14 11:20:09 +010011753 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011754 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011755}
11756
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011757/*
11758 * Initialze mbedtls_ssl_config
11759 */
11760void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11761{
11762 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010011763
11764#if !defined(MBEDTLS_SSL_PROTO_TLS)
11765 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
11766#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011767}
11768
Simon Butcherc97b6972015-12-27 23:48:17 +000011769#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011770#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011771static int ssl_preset_default_hashes[] = {
11772#if defined(MBEDTLS_SHA512_C)
11773 MBEDTLS_MD_SHA512,
11774 MBEDTLS_MD_SHA384,
11775#endif
11776#if defined(MBEDTLS_SHA256_C)
11777 MBEDTLS_MD_SHA256,
11778 MBEDTLS_MD_SHA224,
11779#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011780#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011781 MBEDTLS_MD_SHA1,
11782#endif
11783 MBEDTLS_MD_NONE
11784};
Simon Butcherc97b6972015-12-27 23:48:17 +000011785#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011786#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011787
Hanno Becker73f4cb12019-06-27 13:51:07 +010011788#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011789static int ssl_preset_suiteb_ciphersuites[] = {
11790 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11791 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11792 0
11793};
Hanno Becker73f4cb12019-06-27 13:51:07 +010011794#endif /* !MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011795
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011796#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011797#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011798static int ssl_preset_suiteb_hashes[] = {
11799 MBEDTLS_MD_SHA256,
11800 MBEDTLS_MD_SHA384,
11801 MBEDTLS_MD_NONE
11802};
11803#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011804#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011805
Hanno Beckerc1096e72019-06-19 12:30:41 +010011806#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011807static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amero16529b22019-06-03 08:27:16 +010011808#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011809 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011810#endif
11811#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011812 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amero16529b22019-06-03 08:27:16 +010011813#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011814 MBEDTLS_ECP_DP_NONE
11815};
11816#endif
11817
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011818/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011819 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011820 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011821int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011822 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011823{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011824#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011825 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011826#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011827
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011828 /* Use the functions here so that they are covered in tests,
11829 * but otherwise access member directly for efficiency */
11830 mbedtls_ssl_conf_endpoint( conf, endpoint );
11831 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011832
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011833 /*
11834 * Things that are common to all presets
11835 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011836#if defined(MBEDTLS_SSL_CLI_C)
11837 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11838 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011839#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011840 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010011841#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011842#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11843 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11844#endif
11845 }
11846#endif
11847
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011848#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011849 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011850#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011851
11852#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11853 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11854#endif
11855
11856#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010011857#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011858 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011859#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
11860#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030011861 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030011862 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010011863#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011864#endif
11865
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011866#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11867 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11868#endif
11869
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011870#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011871 conf->f_cookie_write = ssl_cookie_write_dummy;
11872 conf->f_cookie_check = ssl_cookie_check_dummy;
11873#endif
11874
Hanno Becker7f376f42019-06-12 16:20:48 +010011875#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
11876 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011877 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11878#endif
11879
Janos Follath088ce432017-04-10 12:42:31 +010011880#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011881#if !defined(MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST)
Janos Follath088ce432017-04-10 12:42:31 +010011882 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
Hanno Beckerc2cfdaa2019-06-13 12:33:03 +010011883#endif /* !MBEDTLS_SSL_CONF_CERT_REQ_CA_LIST */
11884#endif /* MBEDTLS_SSL_SRV_C */
Janos Follath088ce432017-04-10 12:42:31 +010011885
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011886#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010011887#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011888 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011889#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
11890#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011891 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010011892#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
11893#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011894
11895#if defined(MBEDTLS_SSL_RENEGOTIATION)
11896 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011897 memset( conf->renego_period, 0x00, 2 );
11898 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011899#endif
11900
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011901#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11902 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11903 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011904 const unsigned char dhm_p[] =
11905 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11906 const unsigned char dhm_g[] =
11907 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11908
Hanno Beckera90658f2017-10-04 15:29:08 +010011909 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11910 dhm_p, sizeof( dhm_p ),
11911 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011912 {
11913 return( ret );
11914 }
11915 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011916#endif
11917
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011918 /*
11919 * Preset-specific defaults
11920 */
11921 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011922 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011923 /*
11924 * NSA Suite B
11925 */
11926 case MBEDTLS_SSL_PRESET_SUITEB:
Hanno Beckere965bd32019-06-12 14:04:34 +010011927#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011928 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
Hanno Beckere965bd32019-06-12 14:04:34 +010011929#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11930#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011931 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
Hanno Beckere965bd32019-06-12 14:04:34 +010011932#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11933#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011934 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011935#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11936#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011937 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011938#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011939
Hanno Becker73f4cb12019-06-27 13:51:07 +010011940#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011941 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11942 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11943 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11944 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11945 ssl_preset_suiteb_ciphersuites;
Hanno Becker73f4cb12019-06-27 13:51:07 +010011946#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011947
11948#if defined(MBEDTLS_X509_CRT_PARSE_C)
11949 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011950#endif
11951
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011952#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010011953#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011954 conf->sig_hashes = ssl_preset_suiteb_hashes;
11955#endif
Hanno Becker56595f42019-06-19 16:31:38 +010011956#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011957
11958#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010011959#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011960 conf->curve_list = ssl_preset_suiteb_curves;
11961#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010011962#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011963 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011964
11965 /*
11966 * Default
11967 */
11968 default:
Hanno Beckere965bd32019-06-12 14:04:34 +010011969#if !defined(MBEDTLS_SSL_CONF_MIN_MAJOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011970 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11971 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11972 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11973 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
Hanno Beckere965bd32019-06-12 14:04:34 +010011974#endif /* !MBEDTLS_SSL_CONF_MIN_MAJOR_VER */
11975#if !defined(MBEDTLS_SSL_CONF_MIN_MINOR_VER)
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011976 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11977 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11978 MBEDTLS_SSL_MIN_MINOR_VERSION :
11979 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020011981 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011982 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11983#endif
Hanno Beckere965bd32019-06-12 14:04:34 +010011984#endif /* !MBEDTLS_SSL_CONF_MIN_MINOR_VER */
11985#if !defined(MBEDTLS_SSL_CONF_MAX_MAJOR_VER)
11986 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11987#endif /* !MBEDTLS_SSL_CONF_MAX_MAJOR_VER */
11988#if !defined(MBEDTLS_SSL_CONF_MAX_MINOR_VER)
11989 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11990#endif /* !MBEDTLS_SSL_CONF_MAX_MINOR_VER */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011991
Hanno Becker73f4cb12019-06-27 13:51:07 +010011992#if !defined(MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011993 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11994 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11995 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11996 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11997 mbedtls_ssl_list_ciphersuites();
Hanno Becker73f4cb12019-06-27 13:51:07 +010011998#endif /* MBEDTLS_SSL_CONF_SINGLE_CIPHERSUITE */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011999
12000#if defined(MBEDTLS_X509_CRT_PARSE_C)
12001 conf->cert_profile = &mbedtls_x509_crt_profile_default;
12002#endif
12003
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012004#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Hanno Becker56595f42019-06-19 16:31:38 +010012005#if !defined(MBEDTLS_SSL_CONF_SINGLE_SIG_HASH)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010012006 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012007#endif
Hanno Becker56595f42019-06-19 16:31:38 +010012008#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012009
12010#if defined(MBEDTLS_ECP_C)
Hanno Beckerc1096e72019-06-19 12:30:41 +010012011#if !defined(MBEDTLS_SSL_CONF_SINGLE_EC)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012012 conf->curve_list = mbedtls_ecp_grp_id_list();
12013#endif
Hanno Beckerc1096e72019-06-19 12:30:41 +010012014#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020012015
12016#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
12017 conf->dhm_min_bitlen = 1024;
12018#endif
12019 }
12020
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012021 return( 0 );
12022}
12023
12024/*
12025 * Free mbedtls_ssl_config
12026 */
12027void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
12028{
12029#if defined(MBEDTLS_DHM_C)
12030 mbedtls_mpi_free( &conf->dhm_P );
12031 mbedtls_mpi_free( &conf->dhm_G );
12032#endif
12033
12034#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
12035 if( conf->psk != NULL )
12036 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012037 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012038 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000012039 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012040 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090012041 }
12042
12043 if( conf->psk_identity != NULL )
12044 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012045 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090012046 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000012047 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012048 conf->psk_identity_len = 0;
12049 }
12050#endif
12051
12052#if defined(MBEDTLS_X509_CRT_PARSE_C)
12053 ssl_key_cert_free( conf->key_cert );
12054#endif
12055
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050012056 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020012057}
12058
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012059#if defined(MBEDTLS_PK_C) && \
12060 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012061/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012062 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012064unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012066#if defined(MBEDTLS_RSA_C)
12067 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
12068 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012070#if defined(MBEDTLS_ECDSA_C)
12071 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
12072 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012074 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020012075}
12076
Hanno Becker7e5437a2017-04-28 17:15:26 +010012077unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
12078{
12079 switch( type ) {
12080 case MBEDTLS_PK_RSA:
12081 return( MBEDTLS_SSL_SIG_RSA );
12082 case MBEDTLS_PK_ECDSA:
12083 case MBEDTLS_PK_ECKEY:
12084 return( MBEDTLS_SSL_SIG_ECDSA );
12085 default:
12086 return( MBEDTLS_SSL_SIG_ANON );
12087 }
12088}
12089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012090mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012091{
12092 switch( sig )
12093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012094#if defined(MBEDTLS_RSA_C)
12095 case MBEDTLS_SSL_SIG_RSA:
12096 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012098#if defined(MBEDTLS_ECDSA_C)
12099 case MBEDTLS_SSL_SIG_ECDSA:
12100 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012101#endif
12102 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012103 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012104 }
12105}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020012106#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012107
Hanno Becker7e5437a2017-04-28 17:15:26 +010012108#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
12109 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
12110
12111/* Find an entry in a signature-hash set matching a given hash algorithm. */
12112mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
12113 mbedtls_pk_type_t sig_alg )
12114{
12115 switch( sig_alg )
12116 {
12117 case MBEDTLS_PK_RSA:
12118 return( set->rsa );
12119 case MBEDTLS_PK_ECDSA:
12120 return( set->ecdsa );
12121 default:
12122 return( MBEDTLS_MD_NONE );
12123 }
12124}
12125
12126/* Add a signature-hash-pair to a signature-hash set */
12127void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
12128 mbedtls_pk_type_t sig_alg,
12129 mbedtls_md_type_t md_alg )
12130{
12131 switch( sig_alg )
12132 {
12133 case MBEDTLS_PK_RSA:
12134 if( set->rsa == MBEDTLS_MD_NONE )
12135 set->rsa = md_alg;
12136 break;
12137
12138 case MBEDTLS_PK_ECDSA:
12139 if( set->ecdsa == MBEDTLS_MD_NONE )
12140 set->ecdsa = md_alg;
12141 break;
12142
12143 default:
12144 break;
12145 }
12146}
12147
12148/* Allow exactly one hash algorithm for each signature. */
12149void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
12150 mbedtls_md_type_t md_alg )
12151{
12152 set->rsa = md_alg;
12153 set->ecdsa = md_alg;
12154}
12155
12156#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
12157 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
12158
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012159/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012160 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020012161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012162mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012163{
12164 switch( hash )
12165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012166#if defined(MBEDTLS_MD5_C)
12167 case MBEDTLS_SSL_HASH_MD5:
12168 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012170#if defined(MBEDTLS_SHA1_C)
12171 case MBEDTLS_SSL_HASH_SHA1:
12172 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012174#if defined(MBEDTLS_SHA256_C)
12175 case MBEDTLS_SSL_HASH_SHA224:
12176 return( MBEDTLS_MD_SHA224 );
12177 case MBEDTLS_SSL_HASH_SHA256:
12178 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012180#if defined(MBEDTLS_SHA512_C)
12181 case MBEDTLS_SSL_HASH_SHA384:
12182 return( MBEDTLS_MD_SHA384 );
12183 case MBEDTLS_SSL_HASH_SHA512:
12184 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012185#endif
12186 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012187 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020012188 }
12189}
12190
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012191/*
12192 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
12193 */
12194unsigned char mbedtls_ssl_hash_from_md_alg( int md )
12195{
12196 switch( md )
12197 {
12198#if defined(MBEDTLS_MD5_C)
12199 case MBEDTLS_MD_MD5:
12200 return( MBEDTLS_SSL_HASH_MD5 );
12201#endif
12202#if defined(MBEDTLS_SHA1_C)
12203 case MBEDTLS_MD_SHA1:
12204 return( MBEDTLS_SSL_HASH_SHA1 );
12205#endif
12206#if defined(MBEDTLS_SHA256_C)
12207 case MBEDTLS_MD_SHA224:
12208 return( MBEDTLS_SSL_HASH_SHA224 );
12209 case MBEDTLS_MD_SHA256:
12210 return( MBEDTLS_SSL_HASH_SHA256 );
12211#endif
12212#if defined(MBEDTLS_SHA512_C)
12213 case MBEDTLS_MD_SHA384:
12214 return( MBEDTLS_SSL_HASH_SHA384 );
12215 case MBEDTLS_MD_SHA512:
12216 return( MBEDTLS_SSL_HASH_SHA512 );
12217#endif
12218 default:
12219 return( MBEDTLS_SSL_HASH_NONE );
12220 }
12221}
12222
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012223#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012224/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012225 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012226 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012227 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012228int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012229{
Hanno Beckera4a9c692019-06-18 16:55:47 +010012230 MBEDTLS_SSL_BEGIN_FOR_EACH_SUPPORTED_EC_GRP_ID( own_ec_id )
12231 if( own_ec_id == grp_id )
12232 return( 0 );
12233 MBEDTLS_SSL_END_FOR_EACH_SUPPORTED_EC_GRP_ID
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012234
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020012235 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010012236}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020012237#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012238
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012239#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012240/*
12241 * Check if a hash proposed by the peer is in our list.
12242 * Return 0 if we're willing to use it, -1 otherwise.
12243 */
12244int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
12245 mbedtls_md_type_t md )
12246{
Hanno Beckerf1bc9e12019-06-19 16:23:21 +010012247 MBEDTLS_SSL_BEGIN_FOR_EACH_SIG_HASH( md_alg )
12248 if( md_alg == md )
12249 return( 0 );
12250 MBEDTLS_SSL_END_FOR_EACH_SIG_HASH
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012251
12252 return( -1 );
12253}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020012254#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020012255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012256#if defined(MBEDTLS_X509_CRT_PARSE_C)
12257int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
Hanno Becker473f98f2019-06-26 10:27:32 +010012258 mbedtls_ssl_ciphersuite_handle_t ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012259 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020012260 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012261{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012262 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012263#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012264 int usage = 0;
12265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012266#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012267 const char *ext_oid;
12268 size_t ext_len;
12269#endif
12270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012271#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
12272 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012273 ((void) cert);
12274 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012275 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012276#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012278#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
12279 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012280 {
12281 /* Server part of the key exchange */
Hanno Becker473f98f2019-06-26 10:27:32 +010012282 switch( mbedtls_ssl_suite_get_key_exchange( ciphersuite ) )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012284 case MBEDTLS_KEY_EXCHANGE_RSA:
12285 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012286 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012287 break;
12288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012289 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
12290 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
12291 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
12292 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012293 break;
12294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012295 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
12296 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012297 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012298 break;
12299
12300 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012301 case MBEDTLS_KEY_EXCHANGE_NONE:
12302 case MBEDTLS_KEY_EXCHANGE_PSK:
12303 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
12304 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020012305 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012306 usage = 0;
12307 }
12308 }
12309 else
12310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012311 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
12312 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012313 }
12314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012315 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012316 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012317 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012318 ret = -1;
12319 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012320#else
12321 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012322#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012324#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
12325 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012327 ext_oid = MBEDTLS_OID_SERVER_AUTH;
12328 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012329 }
12330 else
12331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012332 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
12333 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012334 }
12335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012336 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012337 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010012338 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012339 ret = -1;
12340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012341#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020012342
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010012343 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020012344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012345#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020012346
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012347#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
12348 defined(MBEDTLS_SSL_PROTO_TLS1_1)
12349int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
12350 unsigned char *output,
12351 unsigned char *data, size_t data_len )
12352{
12353 int ret = 0;
12354 mbedtls_md5_context mbedtls_md5;
12355 mbedtls_sha1_context mbedtls_sha1;
12356
12357 mbedtls_md5_init( &mbedtls_md5 );
12358 mbedtls_sha1_init( &mbedtls_sha1 );
12359
12360 /*
12361 * digitally-signed struct {
12362 * opaque md5_hash[16];
12363 * opaque sha_hash[20];
12364 * };
12365 *
12366 * md5_hash
12367 * MD5(ClientHello.random + ServerHello.random
12368 * + ServerParams);
12369 * sha_hash
12370 * SHA(ClientHello.random + ServerHello.random
12371 * + ServerParams);
12372 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012373 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012374 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012376 goto exit;
12377 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012378 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012379 ssl->handshake->randbytes, 64 ) ) != 0 )
12380 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012382 goto exit;
12383 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012384 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012385 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012387 goto exit;
12388 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012389 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012390 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012392 goto exit;
12393 }
12394
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012395 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012396 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012398 goto exit;
12399 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012400 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012401 ssl->handshake->randbytes, 64 ) ) != 0 )
12402 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012404 goto exit;
12405 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012406 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012407 data_len ) ) != 0 )
12408 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012410 goto exit;
12411 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012412 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012413 output + 16 ) ) != 0 )
12414 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010012415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012416 goto exit;
12417 }
12418
12419exit:
12420 mbedtls_md5_free( &mbedtls_md5 );
12421 mbedtls_sha1_free( &mbedtls_sha1 );
12422
12423 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012424 mbedtls_ssl_pend_fatal_alert( ssl,
12425 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012426
12427 return( ret );
12428
12429}
12430#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12431 MBEDTLS_SSL_PROTO_TLS1_1 */
12432
12433#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12434 defined(MBEDTLS_SSL_PROTO_TLS1_2)
12435int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012436 unsigned char *hash, size_t *hashlen,
12437 unsigned char *data, size_t data_len,
12438 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012439{
12440 int ret = 0;
12441 mbedtls_md_context_t ctx;
12442 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012443 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012444
12445 mbedtls_md_init( &ctx );
12446
12447 /*
12448 * digitally-signed struct {
12449 * opaque client_random[32];
12450 * opaque server_random[32];
12451 * ServerDHParams params;
12452 * };
12453 */
12454 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12455 {
12456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12457 goto exit;
12458 }
12459 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12460 {
12461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12462 goto exit;
12463 }
12464 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12465 {
12466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12467 goto exit;
12468 }
12469 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12470 {
12471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12472 goto exit;
12473 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012474 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012475 {
12476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12477 goto exit;
12478 }
12479
12480exit:
12481 mbedtls_md_free( &ctx );
12482
12483 if( ret != 0 )
Hanno Beckerde62da92019-07-24 13:23:50 +010012484 mbedtls_ssl_pend_fatal_alert( ssl,
12485 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012486
12487 return( ret );
12488}
12489#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12490 MBEDTLS_SSL_PROTO_TLS1_2 */
12491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012492#endif /* MBEDTLS_SSL_TLS_C */