blob: 9431212decf0ae4eb4e7061ffe4a569cf4d65b07 [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"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
Hanno Beckercfe45792019-07-03 16:13:00 +0100115#if defined(MBEDTLS_SSL_RECORD_CHECKING)
116int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
117 unsigned char *buf,
118 size_t buflen )
119{
120 ((void) ssl);
121 ((void) buf);
122 ((void) buflen);
123 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
124}
125#endif /* MBEDTLS_SSL_RECORD_CHECKING */
126
Hanno Becker67bc7c32018-08-06 11:33:50 +0100127#define SSL_DONT_FORCE_FLUSH 0
128#define SSL_FORCE_FLUSH 1
129
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100131
Hanno Beckera0e20d02019-05-15 14:03:01 +0100132#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100133/* Top-level Connection ID API */
134
Hanno Becker8367ccc2019-05-14 11:30:10 +0100135int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
136 size_t len,
137 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100138{
139 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
141
Hanno Becker611ac772019-05-14 11:45:26 +0100142 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
143 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
144 {
145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
146 }
147
148 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100149 conf->cid_len = len;
150 return( 0 );
151}
152
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100153int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
154 int enable,
155 unsigned char const *own_cid,
156 size_t own_cid_len )
157{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100158 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
160
Hanno Beckerca092242019-04-25 16:01:49 +0100161 ssl->negotiate_cid = enable;
162 if( enable == MBEDTLS_SSL_CID_DISABLED )
163 {
164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
165 return( 0 );
166 }
167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100168 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100169
Hanno Beckerad4a1372019-05-03 13:06:44 +0100170 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100171 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
173 (unsigned) own_cid_len,
174 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
176 }
177
178 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100179 /* Truncation is not an issue here because
180 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
181 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100182
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100183 return( 0 );
184}
185
186int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
187 int *enabled,
188 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
189 size_t *peer_cid_len )
190{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100191 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
Hanno Becker76a79ab2019-05-03 14:38:32 +0100193 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
194 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
195 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100197 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100198
Hanno Beckerc5f24222019-05-03 12:54:52 +0100199 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
200 * were used, but client and server requested the empty CID.
201 * This is indistinguishable from not using the CID extension
202 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203 if( ssl->transform_in->in_cid_len == 0 &&
204 ssl->transform_in->out_cid_len == 0 )
205 {
206 return( 0 );
207 }
208
Hanno Becker615ef172019-05-22 16:50:35 +0100209 if( peer_cid_len != NULL )
210 {
211 *peer_cid_len = ssl->transform_in->out_cid_len;
212 if( peer_cid != NULL )
213 {
214 memcpy( peer_cid, ssl->transform_in->out_cid,
215 ssl->transform_in->out_cid_len );
216 }
217 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100218
219 *enabled = MBEDTLS_SSL_CID_ENABLED;
220
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100221 return( 0 );
222}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100223#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100224
Hanno Beckerd5847772018-08-28 10:09:23 +0100225/* Forward declarations for functions related to message buffering. */
226static void ssl_buffering_free( mbedtls_ssl_context *ssl );
227static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
228 uint8_t slot );
229static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
230static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
231static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
232static int ssl_buffer_message( mbedtls_ssl_context *ssl );
233static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100234static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100235
Hanno Beckera67dee22018-08-22 10:05:20 +0100236static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100237static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100238{
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100240
241 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100242 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100243
244 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
245}
246
Hanno Becker67bc7c32018-08-06 11:33:50 +0100247static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
248{
Hanno Becker11682cc2018-08-22 14:41:02 +0100249 size_t const bytes_written = ssl->out_left;
250 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100251
252 /* Double-check that the write-index hasn't gone
253 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100254 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100255 {
256 /* Should never happen... */
257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
258 }
259
260 return( (int) ( mtu - bytes_written ) );
261}
262
263static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
264{
265 int ret;
266 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400267 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100268
269#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
270 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
271
272 if( max_len > mfl )
273 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100274
275 /* By the standard (RFC 6066 Sect. 4), the MFL extension
276 * only limits the maximum record payload size, so in theory
277 * we would be allowed to pack multiple records of payload size
278 * MFL into a single datagram. However, this would mean that there's
279 * no way to explicitly communicate MTU restrictions to the peer.
280 *
281 * The following reduction of max_len makes sure that we never
282 * write datagrams larger than MFL + Record Expansion Overhead.
283 */
284 if( max_len <= ssl->out_left )
285 return( 0 );
286
287 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100288#endif
289
290 ret = ssl_get_remaining_space_in_datagram( ssl );
291 if( ret < 0 )
292 return( ret );
293 remaining = (size_t) ret;
294
295 ret = mbedtls_ssl_get_record_expansion( ssl );
296 if( ret < 0 )
297 return( ret );
298 expansion = (size_t) ret;
299
300 if( remaining <= expansion )
301 return( 0 );
302
303 remaining -= expansion;
304 if( remaining >= max_len )
305 remaining = max_len;
306
307 return( (int) remaining );
308}
309
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200310/*
311 * Double the retransmit timeout value, within the allowed range,
312 * returning -1 if the maximum value has already been reached.
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200315{
316 uint32_t new_timeout;
317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200318 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319 return( -1 );
320
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200321 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
322 * in the following way: after the initial transmission and a first
323 * retransmission, back off to a temporary estimated MTU of 508 bytes.
324 * This value is guaranteed to be deliverable (if not guaranteed to be
325 * delivered) of any compliant IPv4 (and IPv6) network, and should work
326 * on most non-IP stacks too. */
327 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400328 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200329 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
331 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200332
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200333 new_timeout = 2 * ssl->handshake->retransmit_timeout;
334
335 /* Avoid arithmetic overflow and range overflow */
336 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200337 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200339 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200340 }
341
342 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200344 ssl->handshake->retransmit_timeout ) );
345
346 return( 0 );
347}
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200350{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200351 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200353 ssl->handshake->retransmit_timeout ) );
354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200358/*
359 * Convert max_fragment_length codes to length.
360 * RFC 6066 says:
361 * enum{
362 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
363 * } MaxFragmentLength;
364 * and we add 0 -> extension unused
365 */
Angus Grattond8213d02016-05-25 20:56:48 +1000366static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200367{
Angus Grattond8213d02016-05-25 20:56:48 +1000368 switch( mfl )
369 {
370 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
371 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
372 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
373 return 512;
374 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
375 return 1024;
376 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
377 return 2048;
378 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
379 return 4096;
380 default:
381 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
382 }
383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200385
Hanno Becker52055ae2019-02-06 14:30:46 +0000386int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
387 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_ssl_session_free( dst );
390 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000393
394#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200395 if( src->peer_cert != NULL )
396 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200397 int ret;
398
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200399 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200400 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200401 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200406 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200409 dst->peer_cert = NULL;
410 return( ret );
411 }
412 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000413#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000414 if( src->peer_cert_digest != NULL )
415 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000416 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000417 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000418 if( dst->peer_cert_digest == NULL )
419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
420
421 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
422 src->peer_cert_digest_len );
423 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000424 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000425 }
426#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200429
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200430#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200431 if( src->ticket != NULL )
432 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200433 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200434 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200435 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200436
437 memcpy( dst->ticket, src->ticket, src->ticket_len );
438 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200439#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200440
441 return( 0 );
442}
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
445int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200446 const unsigned char *key_enc, const unsigned char *key_dec,
447 size_t keylen,
448 const unsigned char *iv_enc, const unsigned char *iv_dec,
449 size_t ivlen,
450 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200451 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
453int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
454int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
455int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
456int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
457#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459/*
460 * Key material generation
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200463static int ssl3_prf( const unsigned char *secret, size_t slen,
464 const char *label,
465 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000466 unsigned char *dstbuf, size_t dlen )
467{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100468 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000469 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200470 mbedtls_md5_context md5;
471 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000472 unsigned char padding[16];
473 unsigned char sha1sum[20];
474 ((void)label);
475
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200476 mbedtls_md5_init( &md5 );
477 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200478
Paul Bakker5f70b252012-09-13 14:23:06 +0000479 /*
480 * SSLv3:
481 * block =
482 * MD5( secret + SHA1( 'A' + secret + random ) ) +
483 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
484 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
485 * ...
486 */
487 for( i = 0; i < dlen / 16; i++ )
488 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200489 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000490
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100495 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100497 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100498 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100499 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100500 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000501
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100502 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100504 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100505 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100506 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100507 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100508 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100509 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000510 }
511
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100512exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200513 mbedtls_md5_free( &md5 );
514 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000515
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500516 mbedtls_platform_zeroize( padding, sizeof( padding ) );
517 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000518
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100519 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200524static int tls1_prf( const unsigned char *secret, size_t slen,
525 const char *label,
526 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000527 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000528{
Paul Bakker23986e52011-04-24 08:57:21 +0000529 size_t nb, hs;
530 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200531 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300532 unsigned char *tmp;
533 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 const mbedtls_md_info_t *md_info;
536 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100537 int ret;
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Ron Eldor3b350852019-05-07 18:31:49 +0300541 tmp_len = 20 + strlen( label ) + rlen;
542 tmp = mbedtls_calloc( 1, tmp_len );
543 if( tmp == NULL )
544 {
545 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
546 goto exit;
547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 hs = ( slen + 1 ) / 2;
550 S1 = secret;
551 S2 = secret + slen - hs;
552
553 nb = strlen( label );
554 memcpy( tmp + 20, label, nb );
555 memcpy( tmp + 20 + nb, random, rlen );
556 nb += rlen;
557
558 /*
559 * First compute P_md5(secret,label+random)[0..dlen]
560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300562 {
563 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
564 goto exit;
565 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300568 {
569 goto exit;
570 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
573 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
574 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 for( i = 0; i < dlen; i += 16 )
577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_hmac_reset ( &md_ctx );
579 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
580 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
584 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
587
588 for( j = 0; j < k; j++ )
589 dstbuf[i + j] = h_i[j];
590 }
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100593
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 /*
595 * XOR out with P_sha1(secret,label+random)[0..dlen]
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300598 {
599 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
600 goto exit;
601 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300604 {
605 goto exit;
606 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
609 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
610 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
612 for( i = 0; i < dlen; i += 20 )
613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_md_hmac_reset ( &md_ctx );
615 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
616 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_md_hmac_reset ( &md_ctx );
619 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
620 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
623
624 for( j = 0; j < k; j++ )
625 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
626 }
627
Ron Eldor3b350852019-05-07 18:31:49 +0300628exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100630
Ron Eldor3b350852019-05-07 18:31:49 +0300631 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500632 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Ron Eldor3b350852019-05-07 18:31:49 +0300634 mbedtls_free( tmp );
635 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500640#if defined(MBEDTLS_USE_PSA_CRYPTO)
641static int tls_prf_generic( mbedtls_md_type_t md_type,
642 const unsigned char *secret, size_t slen,
643 const char *label,
644 const unsigned char *random, size_t rlen,
645 unsigned char *dstbuf, size_t dlen )
646{
647 psa_status_t status;
648 psa_algorithm_t alg;
649 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500650 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500651 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
652
Andrzej Kurek2f760752019-01-28 08:08:15 -0500653 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500654 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500655
Andrzej Kurekc929a822019-01-14 03:51:11 -0500656 if( md_type == MBEDTLS_MD_SHA384 )
657 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
658 else
659 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
660
Andrzej Kurek2f760752019-01-28 08:08:15 -0500661 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500662 psa_key_policy_set_usage( &policy,
663 PSA_KEY_USAGE_DERIVE,
664 alg );
665 status = psa_set_key_policy( master_slot, &policy );
666 if( status != PSA_SUCCESS )
667 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
668
669 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
670 if( status != PSA_SUCCESS )
671 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
672
673 status = psa_key_derivation( &generator,
674 master_slot, alg,
675 random, rlen,
676 (unsigned char const *) label,
677 (size_t) strlen( label ),
678 dlen );
679 if( status != PSA_SUCCESS )
680 {
681 psa_generator_abort( &generator );
682 psa_destroy_key( master_slot );
683 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
684 }
685
686 status = psa_generator_read( &generator, dstbuf, dlen );
687 if( status != PSA_SUCCESS )
688 {
689 psa_generator_abort( &generator );
690 psa_destroy_key( master_slot );
691 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
692 }
693
694 status = psa_generator_abort( &generator );
695 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500696 {
697 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500698 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500699 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500700
701 status = psa_destroy_key( master_slot );
702 if( status != PSA_SUCCESS )
703 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
704
Andrzej Kurek33171262019-01-15 03:25:18 -0500705 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500706}
707
708#else /* MBEDTLS_USE_PSA_CRYPTO */
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100711 const unsigned char *secret, size_t slen,
712 const char *label,
713 const unsigned char *random, size_t rlen,
714 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000715{
716 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100717 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300718 unsigned char *tmp;
719 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
721 const mbedtls_md_info_t *md_info;
722 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100723 int ret;
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
728 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100731
Ron Eldor3b350852019-05-07 18:31:49 +0300732 tmp_len = md_len + strlen( label ) + rlen;
733 tmp = mbedtls_calloc( 1, tmp_len );
734 if( tmp == NULL )
735 {
736 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
737 goto exit;
738 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000739
740 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100741 memcpy( tmp + md_len, label, nb );
742 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000743 nb += rlen;
744
745 /*
746 * Compute P_<hash>(secret, label + random)[0..dlen]
747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300749 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
752 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
753 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100754
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100755 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_md_hmac_reset ( &md_ctx );
758 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
759 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_md_hmac_reset ( &md_ctx );
762 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
763 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100765 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000766
767 for( j = 0; j < k; j++ )
768 dstbuf[i + j] = h_i[j];
769 }
770
Ron Eldor3b350852019-05-07 18:31:49 +0300771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100773
Ron Eldor3b350852019-05-07 18:31:49 +0300774 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500775 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 mbedtls_free( tmp );
778
779 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500781#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100783static int tls_prf_sha256( const unsigned char *secret, size_t slen,
784 const char *label,
785 const unsigned char *random, size_t rlen,
786 unsigned char *dstbuf, size_t dlen )
787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100789 label, random, rlen, dstbuf, dlen ) );
790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200794static int tls_prf_sha384( const unsigned char *secret, size_t slen,
795 const char *label,
796 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000797 unsigned char *dstbuf, size_t dlen )
798{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#endif /* MBEDTLS_SHA512_C */
803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
808 defined(MBEDTLS_SSL_PROTO_TLS1_1)
809static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#endif
Paul Bakker380da532012-04-18 16:10:25 +0000811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_SSL_PROTO_SSL3)
813static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
814static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200815#endif
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
818static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
819static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#endif
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
823#if defined(MBEDTLS_SHA256_C)
824static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
825static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
826static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_SHA512_C)
830static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
831static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
832static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100833#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000835
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100836#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100837 defined(MBEDTLS_USE_PSA_CRYPTO)
838static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
839{
840 if( ssl->conf->f_psk != NULL )
841 {
842 /* If we've used a callback to select the PSK,
843 * the static configuration is irrelevant. */
844 if( ssl->handshake->psk_opaque != 0 )
845 return( 1 );
846
847 return( 0 );
848 }
849
850 if( ssl->conf->psk_opaque != 0 )
851 return( 1 );
852
853 return( 0 );
854}
855#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100856 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100857
Ron Eldorcf280092019-05-14 20:19:13 +0300858#if defined(MBEDTLS_SSL_EXPORT_KEYS)
859static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
860{
861#if defined(MBEDTLS_SSL_PROTO_SSL3)
862 if( tls_prf == ssl3_prf )
863 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300864 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300865 }
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
869 if( tls_prf == tls1_prf )
870 {
871 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
872 }
873 else
874#endif
875#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
876#if defined(MBEDTLS_SHA512_C)
877 if( tls_prf == tls_prf_sha384 )
878 {
879 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
880 }
881 else
882#endif
883#if defined(MBEDTLS_SHA256_C)
884 if( tls_prf == tls_prf_sha256 )
885 {
886 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
887 }
888 else
889#endif
890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
891 return( MBEDTLS_SSL_TLS_PRF_NONE );
892}
893#endif /* MBEDTLS_SSL_EXPORT_KEYS */
894
Ron Eldor51d3ab52019-05-12 14:54:30 +0300895int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
896 const unsigned char *secret, size_t slen,
897 const char *label,
898 const unsigned char *random, size_t rlen,
899 unsigned char *dstbuf, size_t dlen )
900{
901 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
902
903 switch( prf )
904 {
905#if defined(MBEDTLS_SSL_PROTO_SSL3)
906 case MBEDTLS_SSL_TLS_PRF_SSL3:
907 tls_prf = ssl3_prf;
908 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300909#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300910#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
911 case MBEDTLS_SSL_TLS_PRF_TLS1:
912 tls_prf = tls1_prf;
913 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300914#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
915
916#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300917#if defined(MBEDTLS_SHA512_C)
918 case MBEDTLS_SSL_TLS_PRF_SHA384:
919 tls_prf = tls_prf_sha384;
920 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300921#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300922#if defined(MBEDTLS_SHA256_C)
923 case MBEDTLS_SSL_TLS_PRF_SHA256:
924 tls_prf = tls_prf_sha256;
925 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300926#endif /* MBEDTLS_SHA256_C */
927#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300928 default:
929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
930 }
931
932 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
933}
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000936{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200937 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000938#if defined(MBEDTLS_USE_PSA_CRYPTO)
939 int psa_fallthrough;
940#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 unsigned char keyblk[256];
943 unsigned char *key1;
944 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100945 unsigned char *mac_enc;
946 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000947 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200948 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000949 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000950 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 const mbedtls_cipher_info_t *cipher_info;
952 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100953
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000954 /* cf. RFC 5246, Section 8.1:
955 * "The master secret is always exactly 48 bytes in length." */
956 size_t const master_secret_len = 48;
957
Hanno Becker35b23c72018-10-23 12:10:41 +0100958#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
959 unsigned char session_hash[48];
960#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_ssl_session *session = ssl->session_negotiate;
963 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
964 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Jaeden Amero2de07f12019-06-05 13:32:08 +0100968#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
969 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000970 transform->encrypt_then_mac = session->encrypt_then_mac;
971#endif
972 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000973
974 ciphersuite_info = handshake->ciphersuite_info;
975 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100976 if( cipher_info == NULL )
977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000979 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100981 }
982
Hanno Beckere694c3e2017-12-27 21:34:08 +0000983 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100984 if( md_info == NULL )
985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000987 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100989 }
990
Hanno Beckera0e20d02019-05-15 14:03:01 +0100991#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100992 /* Copy own and peer's CID if the use of the CID
993 * extension has been negotiated. */
994 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
995 {
996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100997
Hanno Becker05154c32019-05-03 15:23:51 +0100998 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100999 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001000 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001001 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001002
1003 transform->out_cid_len = ssl->handshake->peer_cid_len;
1004 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1005 ssl->handshake->peer_cid_len );
1006 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1007 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001008 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001009#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001010
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_SSL_PROTO_SSL3)
1015 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016 {
Paul Bakker48916f92012-09-16 19:57:18 +00001017 handshake->tls_prf = ssl3_prf;
1018 handshake->calc_verify = ssl_calc_verify_ssl;
1019 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001020 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021 else
1022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1024 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001025 {
Paul Bakker48916f92012-09-16 19:57:18 +00001026 handshake->tls_prf = tls1_prf;
1027 handshake->calc_verify = ssl_calc_verify_tls;
1028 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001030 else
1031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1033#if defined(MBEDTLS_SHA512_C)
1034 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001035 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001036 {
Paul Bakker48916f92012-09-16 19:57:18 +00001037 handshake->tls_prf = tls_prf_sha384;
1038 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1039 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001040 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001041 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_SHA256_C)
1044 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001045 {
Paul Bakker48916f92012-09-16 19:57:18 +00001046 handshake->tls_prf = tls_prf_sha256;
1047 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1048 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001049 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001050 else
1051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001056 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001057
1058 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 * SSLv3:
1060 * master =
1061 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1062 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1063 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001064 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 * master = PRF( premaster, "master secret", randbytes )[0..47]
1067 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001068 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1071 }
1072 else
1073 {
1074 /* The label for the KDF used for key expansion.
1075 * This is either "master secret" or "extended master secret"
1076 * depending on whether the Extended Master Secret extension
1077 * is used. */
1078 char const *lbl = "master secret";
1079
1080 /* The salt for the KDF used for key expansion.
1081 * - If the Extended Master Secret extension is not used,
1082 * this is ClientHello.Random + ServerHello.Random
1083 * (see Sect. 8.1 in RFC 5246).
1084 * - If the Extended Master Secret extension is used,
1085 * this is the transcript of the handshake so far.
1086 * (see Sect. 4 in RFC 7627). */
1087 unsigned char const *salt = handshake->randbytes;
1088 size_t salt_len = 64;
1089
1090#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001094
Hanno Becker35b23c72018-10-23 12:10:41 +01001095 lbl = "extended master secret";
1096 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001097 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1099 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001102 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1103 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001104 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001105 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001106 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001107#endif /* MBEDTLS_SHA512_C */
1108 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001109 }
1110 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001112 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001113
Hanno Becker35b23c72018-10-23 12:10:41 +01001114 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001115 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001116#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1117
Hanno Becker7d0a5692018-10-23 15:26:22 +01001118#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1119 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1120 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1121 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1122 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001123 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001124 /* Perform PSK-to-MS expansion in a single step. */
1125 psa_status_t status;
1126 psa_algorithm_t alg;
1127 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001128 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001129
1130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1131
1132 psk = ssl->conf->psk_opaque;
1133 if( ssl->handshake->psk_opaque != 0 )
1134 psk = ssl->handshake->psk_opaque;
1135
Hanno Becker22bf1452019-04-05 11:21:08 +01001136 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001137 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1138 else
1139 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1140
1141 status = psa_key_derivation( &generator, psk, alg,
1142 salt, salt_len,
1143 (unsigned char const *) lbl,
1144 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001145 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001146 if( status != PSA_SUCCESS )
1147 {
1148 psa_generator_abort( &generator );
1149 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1150 }
1151
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001152 status = psa_generator_read( &generator, session->master,
1153 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001154 if( status != PSA_SUCCESS )
1155 {
1156 psa_generator_abort( &generator );
1157 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1158 }
1159
1160 status = psa_generator_abort( &generator );
1161 if( status != PSA_SUCCESS )
1162 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001163 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001164 else
1165#endif
1166 {
1167 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1168 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001169 session->master,
1170 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001171 if( ret != 0 )
1172 {
1173 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1174 return( ret );
1175 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001176
Hanno Becker7d0a5692018-10-23 15:26:22 +01001177 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1178 handshake->premaster,
1179 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001180
Hanno Becker7d0a5692018-10-23 15:26:22 +01001181 mbedtls_platform_zeroize( handshake->premaster,
1182 sizeof(handshake->premaster) );
1183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
1186 /*
1187 * Swap the client and server random values.
1188 */
Paul Bakker48916f92012-09-16 19:57:18 +00001189 memcpy( tmp, handshake->randbytes, 64 );
1190 memcpy( handshake->randbytes, tmp + 32, 32 );
1191 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001192 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001193
1194 /*
1195 * SSLv3:
1196 * key block =
1197 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1198 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1199 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1200 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1201 * ...
1202 *
1203 * TLSv1:
1204 * key block = PRF( master, "key expansion", randbytes )
1205 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001206 ret = handshake->tls_prf( session->master, 48, "key expansion",
1207 handshake->randbytes, 64, keyblk, 256 );
1208 if( ret != 0 )
1209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001211 return( ret );
1212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1215 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1216 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1217 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1218 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 /*
1221 * Determine the appropriate key, IV and MAC length.
1222 */
Paul Bakker68884e32013-01-07 18:20:04 +01001223
Hanno Becker88aaf652017-12-27 08:17:40 +00001224 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001225
Hanno Becker8031d062018-01-03 15:32:31 +00001226#if defined(MBEDTLS_GCM_C) || \
1227 defined(MBEDTLS_CCM_C) || \
1228 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001230 cipher_info->mode == MBEDTLS_MODE_CCM ||
1231 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001233 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001234
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001235 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001236 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001237 transform->taglen =
1238 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001239
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001240 /* All modes haves 96-bit IVs;
1241 * GCM and CCM has 4 implicit and 8 explicit bytes
1242 * ChachaPoly has all 12 bytes implicit
1243 */
Paul Bakker68884e32013-01-07 18:20:04 +01001244 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001245 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1246 transform->fixed_ivlen = 12;
1247 else
1248 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001249
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001250 /* Minimum length of encrypted record */
1251 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001252 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001253 }
1254 else
Hanno Becker8031d062018-01-03 15:32:31 +00001255#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1256#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1257 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1258 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001259 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001260 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1262 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001265 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001268 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001269 mac_key_len = mbedtls_md_get_size( md_info );
1270 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001273 /*
1274 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1275 * (rfc 6066 page 13 or rfc 2104 section 4),
1276 * so we only need to adjust the length here.
1277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001281
1282#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1283 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001284 * HMAC implementation which also truncates the key
1285 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001286 mac_key_len = transform->maclen;
1287#endif
1288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001290
1291 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001292 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001294 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001296 transform->minlen = transform->maclen;
1297 else
Paul Bakker68884e32013-01-07 18:20:04 +01001298 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001299 /*
1300 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001301 * 1. if EtM is in use: one block plus MAC
1302 * otherwise: * first multiple of blocklen greater than maclen
1303 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1306 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001307 {
1308 transform->minlen = transform->maclen
1309 + cipher_info->block_size;
1310 }
1311 else
1312#endif
1313 {
1314 transform->minlen = transform->maclen
1315 + cipher_info->block_size
1316 - transform->maclen % cipher_info->block_size;
1317 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1320 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1321 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001322 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001323 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1327 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001328 {
1329 transform->minlen += transform->ivlen;
1330 }
1331 else
1332#endif
1333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001335 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1336 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001337 }
Paul Bakker68884e32013-01-07 18:20:04 +01001338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Hanno Becker8031d062018-01-03 15:32:31 +00001340 else
1341#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1342 {
1343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Hanno Becker88aaf652017-12-27 08:17:40 +00001347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1348 (unsigned) keylen,
1349 (unsigned) transform->minlen,
1350 (unsigned) transform->ivlen,
1351 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
1353 /*
1354 * Finally setup the cipher contexts, IVs and MAC secrets.
1355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001359 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001360 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Paul Bakker68884e32013-01-07 18:20:04 +01001362 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001363 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001365 /*
1366 * This is not used in TLS v1.1.
1367 */
Paul Bakker48916f92012-09-16 19:57:18 +00001368 iv_copy_len = ( transform->fixed_ivlen ) ?
1369 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001370 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1371 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001372 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 }
1374 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_SSL_CLI_C */
1376#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001377 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001379 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001380 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Hanno Becker81c7b182017-11-09 18:39:33 +00001382 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001383 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001385 /*
1386 * This is not used in TLS v1.1.
1387 */
Paul Bakker48916f92012-09-16 19:57:18 +00001388 iv_copy_len = ( transform->fixed_ivlen ) ?
1389 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001390 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1391 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001394 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001398 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1399 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001400 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Hanno Beckerd56ed242018-01-03 15:32:51 +00001402#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_PROTO_SSL3)
1404 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001405 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001406 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001409 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1410 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001411 }
1412
Hanno Becker81c7b182017-11-09 18:39:33 +00001413 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1414 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001415 }
1416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1419 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1420 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001421 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001422 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1423 For AEAD-based ciphersuites, there is nothing to do here. */
1424 if( mac_key_len != 0 )
1425 {
1426 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1427 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1428 }
Paul Bakker68884e32013-01-07 18:20:04 +01001429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001430 else
1431#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001434 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1435 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001436 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001437#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1440 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001441 {
1442 int ret = 0;
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001445
Hanno Becker88aaf652017-12-27 08:17:40 +00001446 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001447 transform->iv_enc, transform->iv_dec,
1448 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001449 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001450 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001453 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1454 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001455 }
1456 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001457#else
1458 ((void) mac_dec);
1459 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001461
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001462#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1463 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001464 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001465 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1466 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001467 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001468 iv_copy_len );
1469 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001470
1471 if( ssl->conf->f_export_keys_ext != NULL )
1472 {
1473 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1474 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001475 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001476 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001477 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001478 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001479 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001480 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001481#endif
1482
Hanno Beckerf704bef2018-11-16 15:21:18 +00001483#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001484
1485 /* Only use PSA-based ciphers for TLS-1.2.
1486 * That's relevant at least for TLS-1.0, where
1487 * we assume that mbedtls_cipher_crypt() updates
1488 * the structure field for the IV, which the PSA-based
1489 * implementation currently doesn't. */
1490#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1491 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001492 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001493 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001494 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001495 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1496 {
1497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001498 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 }
1500
1501 if( ret == 0 )
1502 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001504 psa_fallthrough = 0;
1505 }
1506 else
1507 {
1508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1509 psa_fallthrough = 1;
1510 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001511 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001512 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001513 psa_fallthrough = 1;
1514#else
1515 psa_fallthrough = 1;
1516#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001517
Hanno Beckercb1cc802018-11-17 22:27:38 +00001518 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001519#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001520 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001521 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001524 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001525 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001526
Hanno Beckerf704bef2018-11-16 15:21:18 +00001527#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001528 /* Only use PSA-based ciphers for TLS-1.2.
1529 * That's relevant at least for TLS-1.0, where
1530 * we assume that mbedtls_cipher_crypt() updates
1531 * the structure field for the IV, which the PSA-based
1532 * implementation currently doesn't. */
1533#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1534 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001535 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001536 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001537 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001538 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1539 {
1540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001541 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 }
1543
1544 if( ret == 0 )
1545 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001547 psa_fallthrough = 0;
1548 }
1549 else
1550 {
1551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1552 psa_fallthrough = 1;
1553 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001554 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001555 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001556 psa_fallthrough = 1;
1557#else
1558 psa_fallthrough = 1;
1559#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001560
Hanno Beckercb1cc802018-11-17 22:27:38 +00001561 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001562#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001563 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 cipher_info ) ) != 0 )
1565 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001567 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001571 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001575 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001579 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001583 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001584 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_CIPHER_MODE_CBC)
1587 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1590 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001593 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001594 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1597 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001600 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001607 // Initialize compression
1608 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001610 {
Paul Bakker16770332013-10-11 09:59:44 +02001611 if( ssl->compress_buf == NULL )
1612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001614 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001615 if( ssl->compress_buf == NULL )
1616 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001618 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001619 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1620 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001621 }
1622 }
1623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001625
Paul Bakker48916f92012-09-16 19:57:18 +00001626 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1627 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001628
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001629 if( deflateInit( &transform->ctx_deflate,
1630 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001631 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001634 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1635 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001636 }
1637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001641end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001642 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1643 mbedtls_platform_zeroize( handshake->randbytes,
1644 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001645 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646}
1647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#if defined(MBEDTLS_SSL_PROTO_SSL3)
1649void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001651 mbedtls_md5_context md5;
1652 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 unsigned char pad_1[48];
1654 unsigned char pad_2[48];
1655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001658 mbedtls_md5_init( &md5 );
1659 mbedtls_sha1_init( &sha1 );
1660
1661 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1662 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Paul Bakker380da532012-04-18 16:10:25 +00001664 memset( pad_1, 0x36, 48 );
1665 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001667 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1668 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1669 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001671 mbedtls_md5_starts_ret( &md5 );
1672 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1673 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1674 mbedtls_md5_update_ret( &md5, hash, 16 );
1675 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001677 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1678 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1679 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001680
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001681 mbedtls_sha1_starts_ret( &sha1 );
1682 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1683 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1684 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1685 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001689
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001690 mbedtls_md5_free( &md5 );
1691 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001692
Paul Bakker380da532012-04-18 16:10:25 +00001693 return;
1694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1698void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001699{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001700 mbedtls_md5_context md5;
1701 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001704
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001705 mbedtls_md5_init( &md5 );
1706 mbedtls_sha1_init( &sha1 );
1707
1708 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1709 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001710
Andrzej Kurekeb342242019-01-29 09:14:33 -05001711 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001712 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001716
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001717 mbedtls_md5_free( &md5 );
1718 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001719
Paul Bakker380da532012-04-18 16:10:25 +00001720 return;
1721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1725#if defined(MBEDTLS_SHA256_C)
1726void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001727{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001728#if defined(MBEDTLS_USE_PSA_CRYPTO)
1729 size_t hash_size;
1730 psa_status_t status;
1731 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1732
1733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1734 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1735 if( status != PSA_SUCCESS )
1736 {
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1738 return;
1739 }
1740
1741 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1742 if( status != PSA_SUCCESS )
1743 {
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1745 return;
1746 }
1747 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1749#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001750 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001751
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001752 mbedtls_sha256_init( &sha256 );
1753
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001755
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001756 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001757 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001761
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001762 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001763#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001764 return;
1765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#if defined(MBEDTLS_SHA512_C)
1769void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001770{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001771#if defined(MBEDTLS_USE_PSA_CRYPTO)
1772 size_t hash_size;
1773 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001774 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001775
1776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001777 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001778 if( status != PSA_SUCCESS )
1779 {
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1781 return;
1782 }
1783
Andrzej Kurek972fba52019-01-30 03:29:12 -05001784 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001785 if( status != PSA_SUCCESS )
1786 {
1787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1788 return;
1789 }
1790 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1792#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001793 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001794
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001795 mbedtls_sha512_init( &sha512 );
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001798
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001799 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001800 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001805 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001806#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 return;
1808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#endif /* MBEDTLS_SHA512_C */
1810#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1813int 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 +02001814{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001815 unsigned char *p = ssl->handshake->premaster;
1816 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001817 const unsigned char *psk = ssl->conf->psk;
1818 size_t psk_len = ssl->conf->psk_len;
1819
1820 /* If the psk callback was called, use its result */
1821 if( ssl->handshake->psk != NULL )
1822 {
1823 psk = ssl->handshake->psk;
1824 psk_len = ssl->handshake->psk_len;
1825 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001826
1827 /*
1828 * PMS = struct {
1829 * opaque other_secret<0..2^16-1>;
1830 * opaque psk<0..2^16-1>;
1831 * };
1832 * with "other_secret" depending on the particular key exchange
1833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1835 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001836 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001837 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001839
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001840 *(p++) = (unsigned char)( psk_len >> 8 );
1841 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001842
1843 if( end < p || (size_t)( end - p ) < psk_len )
1844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1845
1846 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001847 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001848 }
1849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1851#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1852 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001853 {
1854 /*
1855 * other_secret already set by the ClientKeyExchange message,
1856 * and is 48 bytes long
1857 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001858 if( end - p < 2 )
1859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1860
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001861 *p++ = 0;
1862 *p++ = 48;
1863 p += 48;
1864 }
1865 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1867#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1868 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001870 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001871 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001872
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001873 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001875 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001876 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 return( ret );
1880 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001881 *(p++) = (unsigned char)( len >> 8 );
1882 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001883 p += len;
1884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001886 }
1887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1889#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1890 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001891 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001892 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893 size_t zlen;
1894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001896 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001897 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001900 return( ret );
1901 }
1902
1903 *(p++) = (unsigned char)( zlen >> 8 );
1904 *(p++) = (unsigned char)( zlen );
1905 p += zlen;
1906
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001907 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1908 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001909 }
1910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001915 }
1916
1917 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001918 if( end - p < 2 )
1919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001920
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001921 *(p++) = (unsigned char)( psk_len >> 8 );
1922 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001923
1924 if( end < p || (size_t)( end - p ) < psk_len )
1925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1926
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001927 memcpy( p, psk, psk_len );
1928 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001929
1930 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1931
1932 return( 0 );
1933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001937/*
1938 * SSLv3.0 MAC functions
1939 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001940#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001941static void ssl_mac( mbedtls_md_context_t *md_ctx,
1942 const unsigned char *secret,
1943 const unsigned char *buf, size_t len,
1944 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001945 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001946{
1947 unsigned char header[11];
1948 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001949 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1951 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001952
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001953 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001955 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001956 else
Paul Bakker68884e32013-01-07 18:20:04 +01001957 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
1959 memcpy( header, ctr, 8 );
1960 header[ 8] = (unsigned char) type;
1961 header[ 9] = (unsigned char)( len >> 8 );
1962 header[10] = (unsigned char)( len );
1963
Paul Bakker68884e32013-01-07 18:20:04 +01001964 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 mbedtls_md_starts( md_ctx );
1966 mbedtls_md_update( md_ctx, secret, md_size );
1967 mbedtls_md_update( md_ctx, padding, padlen );
1968 mbedtls_md_update( md_ctx, header, 11 );
1969 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001970 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Paul Bakker68884e32013-01-07 18:20:04 +01001972 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 mbedtls_md_starts( md_ctx );
1974 mbedtls_md_update( md_ctx, secret, md_size );
1975 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001976 mbedtls_md_update( md_ctx, out, md_size );
1977 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001980
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001981/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001982 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001983#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001984 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1985 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1986 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1987/* This function makes sure every byte in the memory region is accessed
1988 * (in ascending addresses order) */
1989static void ssl_read_memory( unsigned char *p, size_t len )
1990{
1991 unsigned char acc = 0;
1992 volatile unsigned char force;
1993
1994 for( ; len != 0; p++, len-- )
1995 acc ^= *p;
1996
1997 force = acc;
1998 (void) force;
1999}
2000#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2001
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002002/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002004 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002005
Hanno Beckera0e20d02019-05-15 14:03:01 +01002006#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002007/* This functions transforms a DTLS plaintext fragment and a record content
2008 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002009 *
2010 * struct {
2011 * opaque content[DTLSPlaintext.length];
2012 * ContentType real_type;
2013 * uint8 zeros[length_of_padding];
2014 * } DTLSInnerPlaintext;
2015 *
2016 * Input:
2017 * - `content`: The beginning of the buffer holding the
2018 * plaintext to be wrapped.
2019 * - `*content_size`: The length of the plaintext in Bytes.
2020 * - `max_len`: The number of Bytes available starting from
2021 * `content`. This must be `>= *content_size`.
2022 * - `rec_type`: The desired record content type.
2023 *
2024 * Output:
2025 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2026 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2027 *
2028 * Returns:
2029 * - `0` on success.
2030 * - A negative error code if `max_len` didn't offer enough space
2031 * for the expansion.
2032 */
2033static int ssl_cid_build_inner_plaintext( unsigned char *content,
2034 size_t *content_size,
2035 size_t remaining,
2036 uint8_t rec_type )
2037{
2038 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002039 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2040 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2041 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002042
2043 /* Write real content type */
2044 if( remaining == 0 )
2045 return( -1 );
2046 content[ len ] = rec_type;
2047 len++;
2048 remaining--;
2049
2050 if( remaining < pad )
2051 return( -1 );
2052 memset( content + len, 0, pad );
2053 len += pad;
2054 remaining -= pad;
2055
2056 *content_size = len;
2057 return( 0 );
2058}
2059
Hanno Becker07dc97d2019-05-20 15:08:01 +01002060/* This function parses a DTLSInnerPlaintext structure.
2061 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002062static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2063 size_t *content_size,
2064 uint8_t *rec_type )
2065{
2066 size_t remaining = *content_size;
2067
2068 /* Determine length of padding by skipping zeroes from the back. */
2069 do
2070 {
2071 if( remaining == 0 )
2072 return( -1 );
2073 remaining--;
2074 } while( content[ remaining ] == 0 );
2075
2076 *content_size = remaining;
2077 *rec_type = content[ remaining ];
2078
2079 return( 0 );
2080}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002081#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002082
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002083/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002084 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002085static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002086 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002087 mbedtls_record *rec )
2088{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002089 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002090 *
2091 * additional_data = seq_num + TLSCompressed.type +
2092 * TLSCompressed.version + TLSCompressed.length;
2093 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002094 * For the CID extension, this is extended as follows
2095 * (quoting draft-ietf-tls-dtls-connection-id-05,
2096 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002097 *
2098 * additional_data = seq_num + DTLSPlaintext.type +
2099 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002100 * cid +
2101 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002102 * length_of_DTLSInnerPlaintext;
2103 */
2104
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002105 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2106 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002107 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002108
Hanno Beckera0e20d02019-05-15 14:03:01 +01002109#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002110 if( rec->cid_len != 0 )
2111 {
2112 memcpy( add_data + 11, rec->cid, rec->cid_len );
2113 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2114 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2115 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2116 *add_data_len = 13 + 1 + rec->cid_len;
2117 }
2118 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002119#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002120 {
2121 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2122 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2123 *add_data_len = 13;
2124 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002125}
2126
Hanno Beckera18d1322018-01-03 14:27:32 +00002127int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2128 mbedtls_ssl_transform *transform,
2129 mbedtls_record *rec,
2130 int (*f_rng)(void *, unsigned char *, size_t),
2131 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002134 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002136 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002137 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002138 size_t post_avail;
2139
2140 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002141#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002142 ((void) ssl);
2143#endif
2144
2145 /* The PRNG is used for dynamic IV generation that's used
2146 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2147#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2148 ( defined(MBEDTLS_AES_C) || \
2149 defined(MBEDTLS_ARIA_C) || \
2150 defined(MBEDTLS_CAMELLIA_C) ) && \
2151 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2152 ((void) f_rng);
2153 ((void) p_rng);
2154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002158 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002159 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2162 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002163 if( rec == NULL
2164 || rec->buf == NULL
2165 || rec->buf_len < rec->data_offset
2166 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002167#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002168 || rec->cid_len != 0
2169#endif
2170 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002171 {
2172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002174 }
2175
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002176 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002177 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002179 data, rec->data_len );
2180
2181 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2182
2183 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2184 {
2185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2186 (unsigned) rec->data_len,
2187 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2189 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002190
Hanno Beckera0e20d02019-05-15 14:03:01 +01002191#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002192 /*
2193 * Add CID information
2194 */
2195 rec->cid_len = transform->out_cid_len;
2196 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2197 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002198
2199 if( rec->cid_len != 0 )
2200 {
2201 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002202 * Wrap plaintext into DTLSInnerPlaintext structure.
2203 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002204 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002205 * Note that this changes `rec->data_len`, and hence
2206 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002207 */
2208 if( ssl_cid_build_inner_plaintext( data,
2209 &rec->data_len,
2210 post_avail,
2211 rec->type ) != 0 )
2212 {
2213 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2214 }
2215
2216 rec->type = MBEDTLS_SSL_MSG_CID;
2217 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002218#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002219
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002220 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2221
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002223 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 */
Hanno Becker52344c22018-01-03 15:24:20 +00002225#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 if( mode == MBEDTLS_MODE_STREAM ||
2227 ( mode == MBEDTLS_MODE_CBC
2228#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002229 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002230#endif
2231 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 if( post_avail < transform->maclen )
2234 {
2235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2236 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2237 }
2238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002240 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002242 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2244 data, rec->data_len, rec->ctr, rec->type, mac );
2245 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002246 }
2247 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2250 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002251 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002252 {
Hanno Becker992b6872017-11-09 18:57:39 +00002253 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2254
Hanno Beckercab87e62019-04-29 13:52:53 +01002255 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002256
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002258 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002259 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2260 data, rec->data_len );
2261 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2262 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2263
2264 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002265 }
2266 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002267#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002271 }
2272
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002273 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2274 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002275
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 rec->data_len += transform->maclen;
2277 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002278 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002279 }
Hanno Becker52344c22018-01-03 15:24:20 +00002280#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002282 /*
2283 * Encrypt
2284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2286 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002288 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002289 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002291 "including %d bytes of padding",
2292 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002294 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2295 transform->iv_enc, transform->ivlen,
2296 data, rec->data_len,
2297 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002300 return( ret );
2301 }
2302
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002303 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
Paul Bakker68884e32013-01-07 18:20:04 +01002309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002311
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002312#if defined(MBEDTLS_GCM_C) || \
2313 defined(MBEDTLS_CCM_C) || \
2314 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002316 mode == MBEDTLS_MODE_CCM ||
2317 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002318 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002319 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002320 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002321 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002322
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323 /* Check that there's space for both the authentication tag
2324 * and the explicit IV before and after the record content. */
2325 if( post_avail < transform->taglen ||
2326 rec->data_offset < explicit_iv_len )
2327 {
2328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2329 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2330 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002331
Paul Bakker68884e32013-01-07 18:20:04 +01002332 /*
2333 * Generate IV
2334 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2336 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002337 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2340 explicit_iv_len );
2341 /* Prefix record content with explicit IV. */
2342 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002343 }
2344 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2345 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002346 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 unsigned char i;
2348
2349 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2350
2351 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 }
2354 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002355 {
2356 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002359 }
2360
Hanno Beckercab87e62019-04-29 13:52:53 +01002361 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002362
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2364 iv, transform->ivlen );
2365 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002366 data - explicit_iv_len, explicit_iv_len );
2367 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002368 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002370 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002371 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002372
Paul Bakker68884e32013-01-07 18:20:04 +01002373 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002374 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002375 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002376
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002377 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002379 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 data, rec->data_len, /* source */
2381 data, &rec->data_len, /* destination */
2382 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002385 return( ret );
2386 }
2387
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2389 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002390
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002391 rec->data_len += transform->taglen + explicit_iv_len;
2392 rec->data_offset -= explicit_iv_len;
2393 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002394 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2398#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002399 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002402 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002403 size_t padlen, i;
2404 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002405
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 /* Currently we're always using minimal padding
2407 * (up to 255 bytes would be allowed). */
2408 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2409 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 padlen = 0;
2411
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002412 /* Check there's enough space in the buffer for the padding. */
2413 if( post_avail < padlen + 1 )
2414 {
2415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2416 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2417 }
2418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002422 rec->data_len += padlen + 1;
2423 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002427 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2428 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 if( f_rng == NULL )
2433 {
2434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2435 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2436 }
2437
2438 if( rec->data_offset < transform->ivlen )
2439 {
2440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2441 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2442 }
2443
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002444 /*
2445 * Generate IV
2446 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002448 if( ret != 0 )
2449 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002450
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 memcpy( data - transform->ivlen, transform->iv_enc,
2452 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002454 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002458 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002460 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2463 transform->iv_enc,
2464 transform->ivlen,
2465 data, rec->data_len,
2466 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002469 return( ret );
2470 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002471
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002472 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002476 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002479 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002480 {
2481 /*
2482 * Save IV in SSL3 and TLS1
2483 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002484 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2485 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002487 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002488#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002489 {
2490 data -= transform->ivlen;
2491 rec->data_offset -= transform->ivlen;
2492 rec->data_len += transform->ivlen;
2493 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002496 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002498 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2499
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 /*
2501 * MAC(MAC_write_key, seq_num +
2502 * TLSCipherText.type +
2503 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002504 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505 * IV + // except for TLS 1.0
2506 * ENC(content + padding + padding_length));
2507 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508
2509 if( post_avail < transform->maclen)
2510 {
2511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2512 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2513 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Beckercab87e62019-04-29 13:52:53 +01002515 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002518 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002519 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002520
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002521 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002522 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002523 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2524 data, rec->data_len );
2525 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2526 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002527
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002530 rec->data_len += transform->maclen;
2531 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002532 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002536 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002538 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002544 /* Make extra sure authentication was performed, exactly once */
2545 if( auth_done != 1 )
2546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002549 }
2550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
2553 return( 0 );
2554}
2555
Hanno Beckera18d1322018-01-03 14:27:32 +00002556int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2557 mbedtls_ssl_transform *transform,
2558 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002559{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002560 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002563#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002564 size_t padlen = 0, correct = 1;
2565#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002566 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002567 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002568 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002569
Hanno Beckera18d1322018-01-03 14:27:32 +00002570#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002571 ((void) ssl);
2572#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002575 if( rec == NULL ||
2576 rec->buf == NULL ||
2577 rec->buf_len < rec->data_offset ||
2578 rec->buf_len - rec->data_offset < rec->data_len )
2579 {
2580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002582 }
2583
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002584 data = rec->buf + rec->data_offset;
2585 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Hanno Beckera0e20d02019-05-15 14:03:01 +01002587#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002588 /*
2589 * Match record's CID with incoming CID.
2590 */
Hanno Becker938489a2019-05-08 13:02:22 +01002591 if( rec->cid_len != transform->in_cid_len ||
2592 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2593 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002594 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002595 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002596#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2599 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002600 {
2601 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002602 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2603 transform->iv_dec,
2604 transform->ivlen,
2605 data, rec->data_len,
2606 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002609 return( ret );
2610 }
2611
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002612 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 }
Paul Bakker68884e32013-01-07 18:20:04 +01002618 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002620#if defined(MBEDTLS_GCM_C) || \
2621 defined(MBEDTLS_CCM_C) || \
2622 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002624 mode == MBEDTLS_MODE_CCM ||
2625 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002626 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002627 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002628 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002629
Hanno Beckerd96a6522019-07-10 13:55:25 +01002630 /*
2631 * Prepare IV from explicit and implicit data.
2632 */
2633
2634 /* Check that there's enough space for the explicit IV
2635 * (at the beginning of the record) and the MAC (at the
2636 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002637 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002640 "+ taglen (%d)", rec->data_len,
2641 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002643 }
Paul Bakker68884e32013-01-07 18:20:04 +01002644
Hanno Beckerd96a6522019-07-10 13:55:25 +01002645#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002646 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2647 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002648 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002649
Hanno Beckerd96a6522019-07-10 13:55:25 +01002650 /* Fixed */
2651 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2652 /* Explicit */
2653 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002654 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002655 else
2656#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2657#if defined(MBEDTLS_CHACHAPOLY_C)
2658 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002659 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002660 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002661 unsigned char i;
2662
2663 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2664
2665 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002667 }
2668 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002669#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002670 {
2671 /* Reminder if we ever add an AEAD mode with a different size */
2672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2673 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2674 }
2675
Hanno Beckerd96a6522019-07-10 13:55:25 +01002676 /* Group changes to data, data_len, and add_data, because
2677 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678 data += explicit_iv_len;
2679 rec->data_offset += explicit_iv_len;
2680 rec->data_len -= explicit_iv_len + transform->taglen;
2681
Hanno Beckercab87e62019-04-29 13:52:53 +01002682 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002684 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002685
Hanno Beckerd96a6522019-07-10 13:55:25 +01002686 /* Because of the check above, we know that there are
2687 * explicit_iv_len Bytes preceeding data, and taglen
2688 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002689 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002690 * mbedtls_cipher_auth_decrypt() below. */
2691
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002692 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002693 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002694 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002695
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002696 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002697 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002698 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002699 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2700 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002701 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002702 data, rec->data_len,
2703 data, &olen,
2704 data + rec->data_len,
2705 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2710 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002711
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002712 return( ret );
2713 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002714 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002715
Hanno Beckerd96a6522019-07-10 13:55:25 +01002716 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002717 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002721 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002722 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2725#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002726 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002729 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002730
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 /*
Paul Bakker45829992013-01-03 14:52:21 +01002732 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002734#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002735 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2736 {
2737 /* The ciphertext is prefixed with the CBC IV. */
2738 minlen += transform->ivlen;
2739 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002740#endif
Paul Bakker45829992013-01-03 14:52:21 +01002741
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002742 /* Size considerations:
2743 *
2744 * - The CBC cipher text must not be empty and hence
2745 * at least of size transform->ivlen.
2746 *
2747 * Together with the potential IV-prefix, this explains
2748 * the first of the two checks below.
2749 *
2750 * - The record must contain a MAC, either in plain or
2751 * encrypted, depending on whether Encrypt-then-MAC
2752 * is used or not.
2753 * - If it is, the message contains the IV-prefix,
2754 * the CBC ciphertext, and the MAC.
2755 * - If it is not, the padded plaintext, and hence
2756 * the CBC ciphertext, has at least length maclen + 1
2757 * because there is at least the padding length byte.
2758 *
2759 * As the CBC ciphertext is not empty, both cases give the
2760 * lower bound minlen + maclen + 1 on the record size, which
2761 * we test for in the second check below.
2762 */
2763 if( rec->data_len < minlen + transform->ivlen ||
2764 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002767 "+ 1 ) ( + expl IV )", rec->data_len,
2768 transform->ivlen,
2769 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002770 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002771 }
2772
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002773 /*
2774 * Authenticate before decrypt if enabled
2775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002776#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002777 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002778 {
Hanno Becker992b6872017-11-09 18:57:39 +00002779 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002782
Hanno Beckerd96a6522019-07-10 13:55:25 +01002783 /* Update data_len in tandem with add_data.
2784 *
2785 * The subtraction is safe because of the previous check
2786 * data_len >= minlen + maclen + 1.
2787 *
2788 * Afterwards, we know that data + data_len is followed by at
2789 * least maclen Bytes, which justifies the call to
2790 * mbedtls_ssl_safer_memcmp() below.
2791 *
2792 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002793 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002794 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002795
Hanno Beckerd96a6522019-07-10 13:55:25 +01002796 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002797 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2798 add_data_len );
2799 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2800 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002801 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2802 data, rec->data_len );
2803 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2804 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002805
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2807 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002808 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002809 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002810
Hanno Beckerd96a6522019-07-10 13:55:25 +01002811 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002812 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2813 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002817 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002818 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002820#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002821
2822 /*
2823 * Check length sanity
2824 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002825
2826 /* We know from above that data_len > minlen >= 0,
2827 * so the following check in particular implies that
2828 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002829 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002834 }
2835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002836#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002837 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002838 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002839 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002840 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002841 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002842 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002844
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002845 data += transform->ivlen;
2846 rec->data_offset += transform->ivlen;
2847 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002848 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002850
Hanno Beckerd96a6522019-07-10 13:55:25 +01002851 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2852
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002853 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2854 transform->iv_dec, transform->ivlen,
2855 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002858 return( ret );
2859 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002860
Hanno Beckerd96a6522019-07-10 13:55:25 +01002861 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002862 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002866 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002870 {
2871 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002872 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2873 * records is equivalent to CBC decryption of the concatenation
2874 * of the records; in other words, IVs are maintained across
2875 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002876 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002877 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2878 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002880#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 /* Safe since data_len >= minlen + maclen + 1, so after having
2883 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002884 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2885 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002886 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002887
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002888 if( auth_done == 1 )
2889 {
2890 correct *= ( rec->data_len >= padlen + 1 );
2891 padlen *= ( rec->data_len >= padlen + 1 );
2892 }
2893 else
Paul Bakker45829992013-01-03 14:52:21 +01002894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002896 if( rec->data_len < transform->maclen + padlen + 1 )
2897 {
2898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2899 rec->data_len,
2900 transform->maclen,
2901 padlen + 1 ) );
2902 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002903#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002904
2905 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2906 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002907 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002908
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 padlen++;
2910
2911 /* Regardless of the validity of the padding,
2912 * we have data_len >= padlen here. */
2913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002915 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002917 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919#if defined(MBEDTLS_SSL_DEBUG_ALL)
2920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002921 "should be no more than %d",
2922 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002923#endif
Paul Bakker45829992013-01-03 14:52:21 +01002924 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 }
2926 }
2927 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2929#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2930 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002931 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002932 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002933 /* The padding check involves a series of up to 256
2934 * consecutive memory reads at the end of the record
2935 * plaintext buffer. In order to hide the length and
2936 * validity of the padding, always perform exactly
2937 * `min(256,plaintext_len)` reads (but take into account
2938 * only the last `padlen` bytes for the padding check). */
2939 size_t pad_count = 0;
2940 size_t real_count = 0;
2941 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002942
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002943 /* Index of first padding byte; it has been ensured above
2944 * that the subtraction is safe. */
2945 size_t const padding_idx = rec->data_len - padlen;
2946 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2947 size_t const start_idx = rec->data_len - num_checks;
2948 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002949
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002950 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002951 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002952 real_count |= ( idx >= padding_idx );
2953 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002954 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002955 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002958 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002960#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002961 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002963 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2965 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002969 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002970
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 /* If the padding was found to be invalid, padlen == 0
2972 * and the subtraction is safe. If the padding was found valid,
2973 * padlen hasn't been changed and the previous assertion
2974 * data_len >= padlen still holds. */
2975 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002977 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002979 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2982 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002985#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002987 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002988#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002989
2990 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002991 * Authenticate if not done yet.
2992 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 */
Hanno Becker52344c22018-01-03 15:24:20 +00002994#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002995 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002996 {
Hanno Becker992b6872017-11-09 18:57:39 +00002997 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002998
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002999 /* If the initial value of padlen was such that
3000 * data_len < maclen + padlen + 1, then padlen
3001 * got reset to 1, and the initial check
3002 * data_len >= minlen + maclen + 1
3003 * guarantees that at this point we still
3004 * have at least data_len >= maclen.
3005 *
3006 * If the initial value of padlen was such that
3007 * data_len >= maclen + padlen + 1, then we have
3008 * subtracted either padlen + 1 (if the padding was correct)
3009 * or 0 (if the padding was incorrect) since then,
3010 * hence data_len >= maclen in any case.
3011 */
3012 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003013 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003016 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003017 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 ssl_mac( &transform->md_ctx_dec,
3019 transform->mac_dec,
3020 data, rec->data_len,
3021 rec->ctr, rec->type,
3022 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003023 }
3024 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3026#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3027 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003028 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003029 {
3030 /*
3031 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003032 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003033 *
3034 * Known timing attacks:
3035 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3036 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 * To compensate for different timings for the MAC calculation
3038 * depending on how much padding was removed (which is determined
3039 * by padlen), process extra_run more blocks through the hash
3040 * function.
3041 *
3042 * The formula in the paper is
3043 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3044 * where L1 is the size of the header plus the decrypted message
3045 * plus CBC padding and L2 is the size of the header plus the
3046 * decrypted message. This is for an underlying hash function
3047 * with 64-byte blocks.
3048 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3049 * correctly. We round down instead of up, so -56 is the correct
3050 * value for our calculations instead of -55.
3051 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003052 * Repeat the formula rather than defining a block_size variable.
3053 * This avoids requiring division by a variable at runtime
3054 * (which would be marginally less efficient and would require
3055 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003056 */
3057 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003058 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003059
3060 /*
3061 * The next two sizes are the minimum and maximum values of
3062 * in_msglen over all padlen values.
3063 *
3064 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003065 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003066 *
3067 * Note that max_len + maclen is never more than the buffer
3068 * length, as we previously did in_msglen -= maclen too.
3069 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003071 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3072
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003073 memset( tmp, 0, sizeof( tmp ) );
3074
3075 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003076 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003077#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3078 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003079 case MBEDTLS_MD_MD5:
3080 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003081 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003082 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003083 extra_run =
3084 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3085 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003086 break;
3087#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003088#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003089 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003090 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003091 extra_run =
3092 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3093 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003094 break;
3095#endif
3096 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3099 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003100
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003101 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003102
Hanno Beckercab87e62019-04-29 13:52:53 +01003103 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3104 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003105 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3106 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003107 /* Make sure we access everything even when padlen > 0. This
3108 * makes the synchronisation requirements for just-in-time
3109 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003110 ssl_read_memory( data + rec->data_len, padlen );
3111 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003112
3113 /* Call mbedtls_md_process at least once due to cache attacks
3114 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003115 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003116 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003117
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003118 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003119
3120 /* Make sure we access all the memory that could contain the MAC,
3121 * before we check it in the next code block. This makes the
3122 * synchronisation requirements for just-in-time Prime+Probe
3123 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003124 ssl_read_memory( data + min_len,
3125 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003126 }
3127 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3129 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3132 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003133 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003135#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003136 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3137 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003139
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003140 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3141 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#if defined(MBEDTLS_SSL_DEBUG_ALL)
3144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003145#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003146 correct = 0;
3147 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003148 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003149 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003150
3151 /*
3152 * Finally check the correct flag
3153 */
3154 if( correct == 0 )
3155 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003156#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003157
3158 /* Make extra sure authentication was performed, exactly once */
3159 if( auth_done != 1 )
3160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003164
Hanno Beckera0e20d02019-05-15 14:03:01 +01003165#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003166 if( rec->cid_len != 0 )
3167 {
3168 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3169 &rec->type );
3170 if( ret != 0 )
3171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3172 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003173#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003176
3177 return( 0 );
3178}
3179
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003180#undef MAC_NONE
3181#undef MAC_PLAINTEXT
3182#undef MAC_CIPHERTEXT
3183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185/*
3186 * Compression/decompression functions
3187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189{
3190 int ret;
3191 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003192 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003194 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003198 if( len_pre == 0 )
3199 return( 0 );
3200
Paul Bakker2770fbd2012-07-03 13:30:23 +00003201 memcpy( msg_pre, ssl->out_msg, len_pre );
3202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003204 ssl->out_msglen ) );
3205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003207 ssl->out_msg, ssl->out_msglen );
3208
Paul Bakker48916f92012-09-16 19:57:18 +00003209 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3210 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3211 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003212 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003213
Paul Bakker48916f92012-09-16 19:57:18 +00003214 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003215 if( ret != Z_OK )
3216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3218 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003219 }
3220
Angus Grattond8213d02016-05-25 20:56:48 +10003221 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003222 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225 ssl->out_msglen ) );
3226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228 ssl->out_msg, ssl->out_msglen );
3229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003231
3232 return( 0 );
3233}
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003236{
3237 int ret;
3238 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003239 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003240 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003241 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003244
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003245 if( len_pre == 0 )
3246 return( 0 );
3247
Paul Bakker2770fbd2012-07-03 13:30:23 +00003248 memcpy( msg_pre, ssl->in_msg, len_pre );
3249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003251 ssl->in_msglen ) );
3252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003254 ssl->in_msg, ssl->in_msglen );
3255
Paul Bakker48916f92012-09-16 19:57:18 +00003256 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3257 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3258 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003259 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003260 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003261
Paul Bakker48916f92012-09-16 19:57:18 +00003262 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003263 if( ret != Z_OK )
3264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3266 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003267 }
3268
Angus Grattond8213d02016-05-25 20:56:48 +10003269 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003270 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003273 ssl->in_msglen ) );
3274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003276 ssl->in_msg, ssl->in_msglen );
3277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003279
3280 return( 0 );
3281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3285static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287#if defined(MBEDTLS_SSL_PROTO_DTLS)
3288static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003289{
3290 /* If renegotiation is not enforced, retransmit until we would reach max
3291 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003292 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003293 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003294 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003295 unsigned char doublings = 1;
3296
3297 while( ratio != 0 )
3298 {
3299 ++doublings;
3300 ratio >>= 1;
3301 }
3302
3303 if( ++ssl->renego_records_seen > doublings )
3304 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003306 return( 0 );
3307 }
3308 }
3309
3310 return( ssl_write_hello_request( ssl ) );
3311}
3312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003314
Paul Bakker5121ce52009-01-03 21:22:43 +00003315/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003316 * Fill the input message buffer by appending data to it.
3317 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003318 *
3319 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3320 * available (from this read and/or a previous one). Otherwise, an error code
3321 * is returned (possibly EOF or WANT_READ).
3322 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003323 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3324 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3325 * since we always read a whole datagram at once.
3326 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003327 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003328 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003331{
Paul Bakker23986e52011-04-24 08:57:21 +00003332 int ret;
3333 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003337 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003340 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003342 }
3343
Angus Grattond8213d02016-05-25 20:56:48 +10003344 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003348 }
3349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003351 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003353 uint32_t timeout;
3354
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003355 /* Just to be sure */
3356 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3357 {
3358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3359 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3360 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3361 }
3362
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003363 /*
3364 * The point is, we need to always read a full datagram at once, so we
3365 * sometimes read more then requested, and handle the additional data.
3366 * It could be the rest of the current record (while fetching the
3367 * header) and/or some other records in the same datagram.
3368 */
3369
3370 /*
3371 * Move to the next record in the already read datagram if applicable
3372 */
3373 if( ssl->next_record_offset != 0 )
3374 {
3375 if( ssl->in_left < ssl->next_record_offset )
3376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3378 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003379 }
3380
3381 ssl->in_left -= ssl->next_record_offset;
3382
3383 if( ssl->in_left != 0 )
3384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003386 ssl->next_record_offset ) );
3387 memmove( ssl->in_hdr,
3388 ssl->in_hdr + ssl->next_record_offset,
3389 ssl->in_left );
3390 }
3391
3392 ssl->next_record_offset = 0;
3393 }
3394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003397
3398 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003399 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003400 */
3401 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003404 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003405 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003406
3407 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003408 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003409 * are not at the beginning of a new record, the caller did something
3410 * wrong.
3411 */
3412 if( ssl->in_left != 0 )
3413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003416 }
3417
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003418 /*
3419 * Don't even try to read if time's out already.
3420 * This avoids by-passing the timer when repeatedly receiving messages
3421 * that will end up being dropped.
3422 */
3423 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003424 {
3425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003426 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003427 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003428 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003429 {
Angus Grattond8213d02016-05-25 20:56:48 +10003430 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003433 timeout = ssl->handshake->retransmit_timeout;
3434 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003435 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003438
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003439 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003440 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3441 timeout );
3442 else
3443 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003446
3447 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003449 }
3450
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003451 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003454 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003457 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003458 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003461 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003462 }
3463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003467 return( ret );
3468 }
3469
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003470 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003471 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003473 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003475 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003476 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003479 return( ret );
3480 }
3481
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003482 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003483 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003485 }
3486
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 if( ret < 0 )
3488 return( ret );
3489
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003490 ssl->in_left = ret;
3491 }
3492 else
3493#endif
3494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003496 ssl->in_left, nb_want ) );
3497
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003498 while( ssl->in_left < nb_want )
3499 {
3500 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003501
3502 if( ssl_check_timer( ssl ) != 0 )
3503 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3504 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003505 {
3506 if( ssl->f_recv_timeout != NULL )
3507 {
3508 ret = ssl->f_recv_timeout( ssl->p_bio,
3509 ssl->in_hdr + ssl->in_left, len,
3510 ssl->conf->read_timeout );
3511 }
3512 else
3513 {
3514 ret = ssl->f_recv( ssl->p_bio,
3515 ssl->in_hdr + ssl->in_left, len );
3516 }
3517 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003520 ssl->in_left, nb_want ) );
3521 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003522
3523 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003525
3526 if( ret < 0 )
3527 return( ret );
3528
mohammad160352aecb92018-03-28 23:41:40 -07003529 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003530 {
Darryl Green11999bb2018-03-13 15:22:58 +00003531 MBEDTLS_SSL_DEBUG_MSG( 1,
3532 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003533 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3535 }
3536
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003537 ssl->in_left += ret;
3538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003539 }
3540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003542
3543 return( 0 );
3544}
3545
3546/*
3547 * Flush any data not yet written
3548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003550{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003551 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003552 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003556 if( ssl->f_send == NULL )
3557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003559 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003561 }
3562
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003563 /* Avoid incrementing counter if data is flushed */
3564 if( ssl->out_left == 0 )
3565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003567 return( 0 );
3568 }
3569
Paul Bakker5121ce52009-01-03 21:22:43 +00003570 while( ssl->out_left > 0 )
3571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003573 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003574
Hanno Becker2b1e3542018-08-06 11:19:13 +01003575 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003576 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003579
3580 if( ret <= 0 )
3581 return( ret );
3582
mohammad160352aecb92018-03-28 23:41:40 -07003583 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003584 {
Darryl Green11999bb2018-03-13 15:22:58 +00003585 MBEDTLS_SSL_DEBUG_MSG( 1,
3586 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003587 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3589 }
3590
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 ssl->out_left -= ret;
3592 }
3593
Hanno Becker2b1e3542018-08-06 11:19:13 +01003594#if defined(MBEDTLS_SSL_PROTO_DTLS)
3595 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003596 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003597 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003598 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003599 else
3600#endif
3601 {
3602 ssl->out_hdr = ssl->out_buf + 8;
3603 }
3604 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
3608 return( 0 );
3609}
3610
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003611/*
3612 * Functions to handle the DTLS retransmission state machine
3613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003615/*
3616 * Append current handshake message to current outgoing flight
3617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003619{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3622 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3623 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003624
3625 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003626 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003627 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003630 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003631 }
3632
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003633 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003634 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003637 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003638 }
3639
3640 /* Copy current handshake message with headers */
3641 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3642 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003643 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003644 msg->next = NULL;
3645
3646 /* Append to the current flight */
3647 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003648 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649 else
3650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003652 while( cur->next != NULL )
3653 cur = cur->next;
3654 cur->next = msg;
3655 }
3656
Hanno Becker3b235902018-08-06 09:54:53 +01003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003658 return( 0 );
3659}
3660
3661/*
3662 * Free the current flight of handshake messages
3663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 mbedtls_ssl_flight_item *cur = flight;
3667 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003668
3669 while( cur != NULL )
3670 {
3671 next = cur->next;
3672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 mbedtls_free( cur->p );
3674 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003675
3676 cur = next;
3677 }
3678}
3679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3681static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003682#endif
3683
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003684/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003685 * Swap transform_out and out_ctr with the alternative ones
3686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003688{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003689 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003690 unsigned char tmp_out_ctr[8];
3691
3692 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003695 return;
3696 }
3697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003699
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003700 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003701 tmp_transform = ssl->transform_out;
3702 ssl->transform_out = ssl->handshake->alt_transform_out;
3703 ssl->handshake->alt_transform_out = tmp_transform;
3704
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003705 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003706 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3707 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003708 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003709
3710 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003711 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3714 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3719 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003720 }
3721 }
3722#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003723}
3724
3725/*
3726 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003727 */
3728int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3729{
3730 int ret = 0;
3731
3732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3733
3734 ret = mbedtls_ssl_flight_transmit( ssl );
3735
3736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3737
3738 return( ret );
3739}
3740
3741/*
3742 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003743 *
3744 * Need to remember the current message in case flush_output returns
3745 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003746 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003747 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003748int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003749{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003750 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003754 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003756
3757 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003758 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003759 ssl_swap_epochs( ssl );
3760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003762 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003763
3764 while( ssl->handshake->cur_msg != NULL )
3765 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003766 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003767 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003768
Hanno Beckere1dcb032018-08-17 16:47:58 +01003769 int const is_finished =
3770 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3771 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3772
Hanno Becker04da1892018-08-14 13:22:10 +01003773 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3774 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3775
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003776 /* Swap epochs before sending Finished: we can't do it after
3777 * sending ChangeCipherSpec, in case write returns WANT_READ.
3778 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003779 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003780 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003782 ssl_swap_epochs( ssl );
3783 }
3784
Hanno Becker67bc7c32018-08-06 11:33:50 +01003785 ret = ssl_get_remaining_payload_in_datagram( ssl );
3786 if( ret < 0 )
3787 return( ret );
3788 max_frag_len = (size_t) ret;
3789
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003790 /* CCS is copied as is, while HS messages may need fragmentation */
3791 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3792 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003793 if( max_frag_len == 0 )
3794 {
3795 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3796 return( ret );
3797
3798 continue;
3799 }
3800
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003801 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003802 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003803 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003804
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003805 /* Update position inside current message */
3806 ssl->handshake->cur_msg_p += cur->len;
3807 }
3808 else
3809 {
3810 const unsigned char * const p = ssl->handshake->cur_msg_p;
3811 const size_t hs_len = cur->len - 12;
3812 const size_t frag_off = p - ( cur->p + 12 );
3813 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003815
Hanno Beckere1dcb032018-08-17 16:47:58 +01003816 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003817 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003818 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003819 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003820
Hanno Becker67bc7c32018-08-06 11:33:50 +01003821 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3822 return( ret );
3823
3824 continue;
3825 }
3826 max_hs_frag_len = max_frag_len - 12;
3827
3828 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3829 max_hs_frag_len : rem_len;
3830
3831 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003832 {
3833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003834 (unsigned) cur_hs_frag_len,
3835 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003836 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003837
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003838 /* Messages are stored with handshake headers as if not fragmented,
3839 * copy beginning of headers then fill fragmentation fields.
3840 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3841 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003843 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3844 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3845 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3846
Hanno Becker67bc7c32018-08-06 11:33:50 +01003847 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3848 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3849 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003850
3851 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3852
Hanno Becker3f7b9732018-08-28 09:53:25 +01003853 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003854 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3855 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003856 ssl->out_msgtype = cur->type;
3857
3858 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003859 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003860 }
3861
3862 /* If done with the current message move to the next one if any */
3863 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3864 {
3865 if( cur->next != NULL )
3866 {
3867 ssl->handshake->cur_msg = cur->next;
3868 ssl->handshake->cur_msg_p = cur->next->p + 12;
3869 }
3870 else
3871 {
3872 ssl->handshake->cur_msg = NULL;
3873 ssl->handshake->cur_msg_p = NULL;
3874 }
3875 }
3876
3877 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003878 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003881 return( ret );
3882 }
3883 }
3884
Hanno Becker67bc7c32018-08-06 11:33:50 +01003885 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3886 return( ret );
3887
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003888 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3890 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003891 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003894 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3895 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003896
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003897 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003898
3899 return( 0 );
3900}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003901
3902/*
3903 * To be called when the last message of an incoming flight is received.
3904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003906{
3907 /* We won't need to resend that one any more */
3908 ssl_flight_free( ssl->handshake->flight );
3909 ssl->handshake->flight = NULL;
3910 ssl->handshake->cur_msg = NULL;
3911
3912 /* The next incoming flight will start with this msg_seq */
3913 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3914
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003915 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003916 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003917
Hanno Becker0271f962018-08-16 13:23:47 +01003918 /* Clear future message buffering structure. */
3919 ssl_buffering_free( ssl );
3920
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003921 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003922 ssl_set_timer( ssl, 0 );
3923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003924 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3925 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003928 }
3929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003931}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003932
3933/*
3934 * To be called when the last message of an outgoing flight is send.
3935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003937{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003938 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003939 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003941 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3942 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003944 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003945 }
3946 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003950
Paul Bakker5121ce52009-01-03 21:22:43 +00003951/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003952 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003954
3955/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003956 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003957 *
3958 * - fill in handshake headers
3959 * - update handshake checksum
3960 * - DTLS: save message for resending
3961 * - then pass to the record layer
3962 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003963 * DTLS: except for HelloRequest, messages are only queued, and will only be
3964 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003965 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003966 * Inputs:
3967 * - ssl->out_msglen: 4 + actual handshake message len
3968 * (4 is the size of handshake headers for TLS)
3969 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3970 * - ssl->out_msg + 4: the handshake message body
3971 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003972 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003973 * - ssl->out_msglen: the length of the record contents
3974 * (including handshake headers but excluding record headers)
3975 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003976 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003977int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003978{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003979 int ret;
3980 const size_t hs_len = ssl->out_msglen - 4;
3981 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003982
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3984
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003985 /*
3986 * Sanity checks
3987 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003988 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003989 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3990 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003991 /* In SSLv3, the client might send a NoCertificate alert. */
3992#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3993 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3994 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3995 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3996#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3997 {
3998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4000 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004001 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004002
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004003 /* Whenever we send anything different from a
4004 * HelloRequest we should be in a handshake - double check. */
4005 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4006 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004007 ssl->handshake == NULL )
4008 {
4009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004015 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004017 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004020 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004021#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004022
Hanno Beckerb50a2532018-08-06 11:52:54 +01004023 /* Double-check that we did not exceed the bounds
4024 * of the outgoing record buffer.
4025 * This should never fail as the various message
4026 * writing functions must obey the bounds of the
4027 * outgoing record buffer, but better be safe.
4028 *
4029 * Note: We deliberately do not check for the MTU or MFL here.
4030 */
4031 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4032 {
4033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4034 "size %u, maximum %u",
4035 (unsigned) ssl->out_msglen,
4036 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4038 }
4039
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004040 /*
4041 * Fill handshake headers
4042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004043 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004045 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4046 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4047 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004048
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004049 /*
4050 * DTLS has additional fields in the Handshake layer,
4051 * between the length field and the actual payload:
4052 * uint16 message_seq;
4053 * uint24 fragment_offset;
4054 * uint24 fragment_length;
4055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004057 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004058 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004059 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004060 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004061 {
4062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4063 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004064 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004065 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4067 }
4068
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004069 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004070 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004071
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004072 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004073 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004074 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004075 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4076 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4077 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004078 }
4079 else
4080 {
4081 ssl->out_msg[4] = 0;
4082 ssl->out_msg[5] = 0;
4083 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004084
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004085 /* Handshake hashes are computed without fragmentation,
4086 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004087 memset( ssl->out_msg + 6, 0x00, 3 );
4088 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004091
Hanno Becker0207e532018-08-28 10:28:28 +01004092 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004093 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4094 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004095 }
4096
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004097 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004100 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4101 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004102 {
4103 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004106 return( ret );
4107 }
4108 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004109 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004110#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004111 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004112 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004113 {
4114 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4115 return( ret );
4116 }
4117 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004118
4119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4120
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004121 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004122}
4123
4124/*
4125 * Record layer functions
4126 */
4127
4128/*
4129 * Write current record.
4130 *
4131 * Uses:
4132 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4133 * - ssl->out_msglen: length of the record content (excl headers)
4134 * - ssl->out_msg: record content
4135 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004136int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004137{
4138 int ret, done = 0;
4139 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004140 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004141
4142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004145 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004147 {
4148 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004151 return( ret );
4152 }
4153
4154 len = ssl->out_msglen;
4155 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4159 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004163 ret = mbedtls_ssl_hw_record_write( ssl );
4164 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4167 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004168 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004169
4170 if( ret == 0 )
4171 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004174 if( !done )
4175 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004176 unsigned i;
4177 size_t protected_record_size;
4178
Hanno Becker6430faf2019-05-08 11:57:13 +01004179 /* Skip writing the record content type to after the encryption,
4180 * as it may change when using the CID extension. */
4181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004183 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004184
Hanno Becker19859472018-08-06 09:40:20 +01004185 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004186 ssl->out_len[0] = (unsigned char)( len >> 8 );
4187 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004188
Paul Bakker48916f92012-09-16 19:57:18 +00004189 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004190 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004191 mbedtls_record rec;
4192
4193 rec.buf = ssl->out_iv;
4194 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4195 ( ssl->out_iv - ssl->out_buf );
4196 rec.data_len = ssl->out_msglen;
4197 rec.data_offset = ssl->out_msg - rec.buf;
4198
4199 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4200 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4201 ssl->conf->transport, rec.ver );
4202 rec.type = ssl->out_msgtype;
4203
Hanno Beckera0e20d02019-05-15 14:03:01 +01004204#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004205 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004206 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004207#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004208
Hanno Beckera18d1322018-01-03 14:27:32 +00004209 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004210 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004213 return( ret );
4214 }
4215
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004216 if( rec.data_offset != 0 )
4217 {
4218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4220 }
4221
Hanno Becker6430faf2019-05-08 11:57:13 +01004222 /* Update the record content type and CID. */
4223 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004224#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004225 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004226#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004227 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004228 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4229 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004230 }
4231
Hanno Becker5903de42019-05-03 14:46:38 +01004232 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004233
4234#if defined(MBEDTLS_SSL_PROTO_DTLS)
4235 /* In case of DTLS, double-check that we don't exceed
4236 * the remaining space in the datagram. */
4237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4238 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004239 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004240 if( ret < 0 )
4241 return( ret );
4242
4243 if( protected_record_size > (size_t) ret )
4244 {
4245 /* Should never happen */
4246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4247 }
4248 }
4249#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004250
Hanno Becker6430faf2019-05-08 11:57:13 +01004251 /* Now write the potentially updated record content type. */
4252 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004255 "version = [%d:%d], msglen = %d",
4256 ssl->out_hdr[0], ssl->out_hdr[1],
4257 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004260 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004261
4262 ssl->out_left += protected_record_size;
4263 ssl->out_hdr += protected_record_size;
4264 ssl_update_out_pointers( ssl, ssl->transform_out );
4265
Hanno Becker04484622018-08-06 09:49:38 +01004266 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4267 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4268 break;
4269
4270 /* The loop goes to its end iff the counter is wrapping */
4271 if( i == ssl_ep_len( ssl ) )
4272 {
4273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4274 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004276 }
4277
Hanno Becker67bc7c32018-08-06 11:33:50 +01004278#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004279 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4280 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004281 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004282 size_t remaining;
4283 ret = ssl_get_remaining_payload_in_datagram( ssl );
4284 if( ret < 0 )
4285 {
4286 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4287 ret );
4288 return( ret );
4289 }
4290
4291 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004292 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004293 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004294 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004295 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004296 else
4297 {
Hanno Becker513815a2018-08-20 11:56:09 +01004298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004299 }
4300 }
4301#endif /* MBEDTLS_SSL_PROTO_DTLS */
4302
4303 if( ( flush == SSL_FORCE_FLUSH ) &&
4304 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004307 return( ret );
4308 }
4309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004311
4312 return( 0 );
4313}
4314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004316
4317static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4318{
4319 if( ssl->in_msglen < ssl->in_hslen ||
4320 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4321 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4322 {
4323 return( 1 );
4324 }
4325 return( 0 );
4326}
Hanno Becker44650b72018-08-16 12:51:11 +01004327
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004328static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004329{
4330 return( ( ssl->in_msg[9] << 16 ) |
4331 ( ssl->in_msg[10] << 8 ) |
4332 ssl->in_msg[11] );
4333}
4334
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004335static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004336{
4337 return( ( ssl->in_msg[6] << 16 ) |
4338 ( ssl->in_msg[7] << 8 ) |
4339 ssl->in_msg[8] );
4340}
4341
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004342static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004343{
4344 uint32_t msg_len, frag_off, frag_len;
4345
4346 msg_len = ssl_get_hs_total_len( ssl );
4347 frag_off = ssl_get_hs_frag_off( ssl );
4348 frag_len = ssl_get_hs_frag_len( ssl );
4349
4350 if( frag_off > msg_len )
4351 return( -1 );
4352
4353 if( frag_len > msg_len - frag_off )
4354 return( -1 );
4355
4356 if( frag_len + 12 > ssl->in_msglen )
4357 return( -1 );
4358
4359 return( 0 );
4360}
4361
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004362/*
4363 * Mark bits in bitmask (used for DTLS HS reassembly)
4364 */
4365static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4366{
4367 unsigned int start_bits, end_bits;
4368
4369 start_bits = 8 - ( offset % 8 );
4370 if( start_bits != 8 )
4371 {
4372 size_t first_byte_idx = offset / 8;
4373
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004374 /* Special case */
4375 if( len <= start_bits )
4376 {
4377 for( ; len != 0; len-- )
4378 mask[first_byte_idx] |= 1 << ( start_bits - len );
4379
4380 /* Avoid potential issues with offset or len becoming invalid */
4381 return;
4382 }
4383
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004384 offset += start_bits; /* Now offset % 8 == 0 */
4385 len -= start_bits;
4386
4387 for( ; start_bits != 0; start_bits-- )
4388 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4389 }
4390
4391 end_bits = len % 8;
4392 if( end_bits != 0 )
4393 {
4394 size_t last_byte_idx = ( offset + len ) / 8;
4395
4396 len -= end_bits; /* Now len % 8 == 0 */
4397
4398 for( ; end_bits != 0; end_bits-- )
4399 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4400 }
4401
4402 memset( mask + offset / 8, 0xFF, len / 8 );
4403}
4404
4405/*
4406 * Check that bitmask is full
4407 */
4408static int ssl_bitmask_check( unsigned char *mask, size_t len )
4409{
4410 size_t i;
4411
4412 for( i = 0; i < len / 8; i++ )
4413 if( mask[i] != 0xFF )
4414 return( -1 );
4415
4416 for( i = 0; i < len % 8; i++ )
4417 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4418 return( -1 );
4419
4420 return( 0 );
4421}
4422
Hanno Becker56e205e2018-08-16 09:06:12 +01004423/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004424static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004425 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004426{
Hanno Becker56e205e2018-08-16 09:06:12 +01004427 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004428
Hanno Becker56e205e2018-08-16 09:06:12 +01004429 alloc_len = 12; /* Handshake header */
4430 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004431
Hanno Beckerd07df862018-08-16 09:14:58 +01004432 if( add_bitmap )
4433 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004434
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004435 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004436}
Hanno Becker56e205e2018-08-16 09:06:12 +01004437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004439
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004440static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004441{
4442 return( ( ssl->in_msg[1] << 16 ) |
4443 ( ssl->in_msg[2] << 8 ) |
4444 ssl->in_msg[3] );
4445}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004446
Simon Butcher99000142016-10-13 17:21:01 +01004447int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004452 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004454 }
4455
Hanno Becker12555c62018-08-16 12:47:53 +01004456 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004459 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004460 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004463 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004464 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004465 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004466 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004467
Hanno Becker44650b72018-08-16 12:51:11 +01004468 if( ssl_check_hs_header( ssl ) != 0 )
4469 {
4470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4471 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4472 }
4473
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004474 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004475 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4476 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4477 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4478 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004479 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004480 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4481 {
4482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4483 recv_msg_seq,
4484 ssl->handshake->in_msg_seq ) );
4485 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4486 }
4487
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004488 /* Retransmit only on last message from previous flight, to avoid
4489 * too many retransmissions.
4490 * Besides, No sane server ever retransmits HelloVerifyRequest */
4491 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004495 "message_seq = %d, start_of_flight = %d",
4496 recv_msg_seq,
4497 ssl->handshake->in_flight_start_seq ) );
4498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004502 return( ret );
4503 }
4504 }
4505 else
4506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004508 "message_seq = %d, expected = %d",
4509 recv_msg_seq,
4510 ssl->handshake->in_msg_seq ) );
4511 }
4512
Hanno Becker90333da2017-10-10 11:27:13 +01004513 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004514 }
4515 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004516
Hanno Becker6d97ef52018-08-16 13:09:04 +01004517 /* Message reassembly is handled alongside buffering of future
4518 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004519 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004520 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004521 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004524 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004525 }
4526 }
4527 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004529 /* With TLS we don't handle fragmentation (for now) */
4530 if( ssl->in_msglen < ssl->in_hslen )
4531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4533 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004534 }
4535
Simon Butcher99000142016-10-13 17:21:01 +01004536 return( 0 );
4537}
4538
4539void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4540{
Hanno Becker0271f962018-08-16 13:23:47 +01004541 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004542
Hanno Becker0271f962018-08-16 13:23:47 +01004543 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004544 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004545 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004546 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004547
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004548 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004550 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004551 ssl->handshake != NULL )
4552 {
Hanno Becker0271f962018-08-16 13:23:47 +01004553 unsigned offset;
4554 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004555
Hanno Becker0271f962018-08-16 13:23:47 +01004556 /* Increment handshake sequence number */
4557 hs->in_msg_seq++;
4558
4559 /*
4560 * Clear up handshake buffering and reassembly structure.
4561 */
4562
4563 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004564 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004565
4566 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004567 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4568 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004569 offset++, hs_buf++ )
4570 {
4571 *hs_buf = *(hs_buf + 1);
4572 }
4573
4574 /* Create a fresh last entry */
4575 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004576 }
4577#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004578}
4579
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004580/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004581 * DTLS anti-replay: RFC 6347 4.1.2.6
4582 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004583 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4584 * Bit n is set iff record number in_window_top - n has been seen.
4585 *
4586 * Usually, in_window_top is the last record number seen and the lsb of
4587 * in_window is set. The only exception is the initial state (record number 0
4588 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4591static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004592{
4593 ssl->in_window_top = 0;
4594 ssl->in_window = 0;
4595}
4596
4597static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4598{
4599 return( ( (uint64_t) buf[0] << 40 ) |
4600 ( (uint64_t) buf[1] << 32 ) |
4601 ( (uint64_t) buf[2] << 24 ) |
4602 ( (uint64_t) buf[3] << 16 ) |
4603 ( (uint64_t) buf[4] << 8 ) |
4604 ( (uint64_t) buf[5] ) );
4605}
4606
4607/*
4608 * Return 0 if sequence number is acceptable, -1 otherwise
4609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004610int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004611{
4612 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4613 uint64_t bit;
4614
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004615 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004616 return( 0 );
4617
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004618 if( rec_seqnum > ssl->in_window_top )
4619 return( 0 );
4620
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004621 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004622
4623 if( bit >= 64 )
4624 return( -1 );
4625
4626 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4627 return( -1 );
4628
4629 return( 0 );
4630}
4631
4632/*
4633 * Update replay window on new validated record
4634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004636{
4637 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4638
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004639 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004640 return;
4641
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004642 if( rec_seqnum > ssl->in_window_top )
4643 {
4644 /* Update window_top and the contents of the window */
4645 uint64_t shift = rec_seqnum - ssl->in_window_top;
4646
4647 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004648 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004649 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004650 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004651 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004652 ssl->in_window |= 1;
4653 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004654
4655 ssl->in_window_top = rec_seqnum;
4656 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004657 else
4658 {
4659 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004660 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004661
4662 if( bit < 64 ) /* Always true, but be extra sure */
4663 ssl->in_window |= (uint64_t) 1 << bit;
4664 }
4665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004667
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004668#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004669/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004670static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4671
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004672/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004673 * Without any SSL context, check if a datagram looks like a ClientHello with
4674 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004675 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004676 *
4677 * - if cookie is valid, return 0
4678 * - if ClientHello looks superficially valid but cookie is not,
4679 * fill obuf and set olen, then
4680 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4681 * - otherwise return a specific error code
4682 */
4683static int ssl_check_dtls_clihlo_cookie(
4684 mbedtls_ssl_cookie_write_t *f_cookie_write,
4685 mbedtls_ssl_cookie_check_t *f_cookie_check,
4686 void *p_cookie,
4687 const unsigned char *cli_id, size_t cli_id_len,
4688 const unsigned char *in, size_t in_len,
4689 unsigned char *obuf, size_t buf_len, size_t *olen )
4690{
4691 size_t sid_len, cookie_len;
4692 unsigned char *p;
4693
4694 if( f_cookie_write == NULL || f_cookie_check == NULL )
4695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4696
4697 /*
4698 * Structure of ClientHello with record and handshake headers,
4699 * and expected values. We don't need to check a lot, more checks will be
4700 * done when actually parsing the ClientHello - skipping those checks
4701 * avoids code duplication and does not make cookie forging any easier.
4702 *
4703 * 0-0 ContentType type; copied, must be handshake
4704 * 1-2 ProtocolVersion version; copied
4705 * 3-4 uint16 epoch; copied, must be 0
4706 * 5-10 uint48 sequence_number; copied
4707 * 11-12 uint16 length; (ignored)
4708 *
4709 * 13-13 HandshakeType msg_type; (ignored)
4710 * 14-16 uint24 length; (ignored)
4711 * 17-18 uint16 message_seq; copied
4712 * 19-21 uint24 fragment_offset; copied, must be 0
4713 * 22-24 uint24 fragment_length; (ignored)
4714 *
4715 * 25-26 ProtocolVersion client_version; (ignored)
4716 * 27-58 Random random; (ignored)
4717 * 59-xx SessionID session_id; 1 byte len + sid_len content
4718 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4719 * ...
4720 *
4721 * Minimum length is 61 bytes.
4722 */
4723 if( in_len < 61 ||
4724 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4725 in[3] != 0 || in[4] != 0 ||
4726 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4727 {
4728 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4729 }
4730
4731 sid_len = in[59];
4732 if( sid_len > in_len - 61 )
4733 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4734
4735 cookie_len = in[60 + sid_len];
4736 if( cookie_len > in_len - 60 )
4737 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4738
4739 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4740 cli_id, cli_id_len ) == 0 )
4741 {
4742 /* Valid cookie */
4743 return( 0 );
4744 }
4745
4746 /*
4747 * If we get here, we've got an invalid cookie, let's prepare HVR.
4748 *
4749 * 0-0 ContentType type; copied
4750 * 1-2 ProtocolVersion version; copied
4751 * 3-4 uint16 epoch; copied
4752 * 5-10 uint48 sequence_number; copied
4753 * 11-12 uint16 length; olen - 13
4754 *
4755 * 13-13 HandshakeType msg_type; hello_verify_request
4756 * 14-16 uint24 length; olen - 25
4757 * 17-18 uint16 message_seq; copied
4758 * 19-21 uint24 fragment_offset; copied
4759 * 22-24 uint24 fragment_length; olen - 25
4760 *
4761 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4762 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4763 *
4764 * Minimum length is 28.
4765 */
4766 if( buf_len < 28 )
4767 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4768
4769 /* Copy most fields and adapt others */
4770 memcpy( obuf, in, 25 );
4771 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4772 obuf[25] = 0xfe;
4773 obuf[26] = 0xff;
4774
4775 /* Generate and write actual cookie */
4776 p = obuf + 28;
4777 if( f_cookie_write( p_cookie,
4778 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4779 {
4780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4781 }
4782
4783 *olen = p - obuf;
4784
4785 /* Go back and fill length fields */
4786 obuf[27] = (unsigned char)( *olen - 28 );
4787
4788 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4789 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4790 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4791
4792 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4793 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4794
4795 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4796}
4797
4798/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004799 * Handle possible client reconnect with the same UDP quadruplet
4800 * (RFC 6347 Section 4.2.8).
4801 *
4802 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4803 * that looks like a ClientHello.
4804 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004805 * - if the input looks like a ClientHello without cookies,
4806 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004807 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004808 * - if the input looks like a ClientHello with a valid cookie,
4809 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004810 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004811 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004812 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004813 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004814 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4815 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004816 */
4817static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4818{
4819 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004820 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004821
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004822 ret = ssl_check_dtls_clihlo_cookie(
4823 ssl->conf->f_cookie_write,
4824 ssl->conf->f_cookie_check,
4825 ssl->conf->p_cookie,
4826 ssl->cli_id, ssl->cli_id_len,
4827 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004828 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004829
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004830 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4831
4832 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004833 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004834 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004835 * If the error is permanent we'll catch it later,
4836 * if it's not, then hopefully it'll work next time. */
4837 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4838
4839 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004840 }
4841
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004842 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004843 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004844 /* Got a valid cookie, partially reset context */
4845 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4846 {
4847 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4848 return( ret );
4849 }
4850
4851 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004852 }
4853
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004854 return( ret );
4855}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004856#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004857
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004858static int ssl_check_record_type( uint8_t record_type )
4859{
4860 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4861 record_type != MBEDTLS_SSL_MSG_ALERT &&
4862 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4863 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4864 {
4865 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4866 }
4867
4868 return( 0 );
4869}
4870
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004871/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004872 * ContentType type;
4873 * ProtocolVersion version;
4874 * uint16 epoch; // DTLS only
4875 * uint48 sequence_number; // DTLS only
4876 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004877 *
4878 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004879 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004880 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4881 *
4882 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004883 * 1. proceed with the record if this function returns 0
4884 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4885 * 3. return CLIENT_RECONNECT if this function return that value
4886 * 4. drop the whole datagram if this function returns anything else.
4887 * Point 2 is needed when the peer is resending, and we have already received
4888 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004891{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004892 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004893 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004894
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004895 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004896
Paul Bakker5121ce52009-01-03 21:22:43 +00004897 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004898 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004899
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004900 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004901#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004902 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4903 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4904 ssl->conf->cid_len != 0 )
4905 {
4906 /* Shift pointers to account for record header including CID
4907 * struct {
4908 * ContentType special_type = tls12_cid;
4909 * ProtocolVersion version;
4910 * uint16 epoch;
4911 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004912 * opaque cid[cid_length]; // Additional field compared to
4913 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004914 * uint16 length;
4915 * opaque enc_content[DTLSCiphertext.length];
4916 * } DTLSCiphertext;
4917 */
4918
4919 /* So far, we only support static CID lengths
4920 * fixed in the configuration. */
4921 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4922 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4923 }
4924 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004925#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004926 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004929
4930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004931 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4932 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004933 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4934#endif /* MBEDTLS_SSL_PROTO_DTLS */
4935 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4936 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004939 }
4940
4941 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004942 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4945 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004946 }
4947
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004948 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4951 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004952 }
4953
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004954 /* Now that the total length of the record header is known, ensure
4955 * that the current datagram is large enough to hold it.
4956 * This would fail, for example, if we received a datagram of
4957 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4958 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4959 if( ret != 0 )
4960 {
4961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4962 return( ret );
4963 }
4964 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4965
4966 /* Parse and validate record length
4967 * This must happen after the CID parsing because
4968 * its position in the record header depends on
4969 * the presence of a CID. */
4970
4971 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004972 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004973 "version = [%d:%d], msglen = %d",
4974 ssl->in_msgtype,
4975 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004976
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004977 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004978 * DTLS-related tests.
4979 * Check epoch before checking length constraint because
4980 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4981 * message gets duplicated before the corresponding Finished message,
4982 * the second ChangeCipherSpec should be discarded because it belongs
4983 * to an old epoch, but not because its length is shorter than
4984 * the minimum record length for packets using the new record transform.
4985 * Note that these two kinds of failures are handled differently,
4986 * as an unexpected record is silently skipped but an invalid
4987 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004988 */
4989#if defined(MBEDTLS_SSL_PROTO_DTLS)
4990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4991 {
4992 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4993
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004994 /* Check epoch (and sequence number) with DTLS */
4995 if( rec_epoch != ssl->in_epoch )
4996 {
4997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4998 "expected %d, received %d",
4999 ssl->in_epoch, rec_epoch ) );
5000
5001#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5002 /*
5003 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5004 * access the first byte of record content (handshake type), as we
5005 * have an active transform (possibly iv_len != 0), so use the
5006 * fact that the record header len is 13 instead.
5007 */
5008 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5009 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5010 rec_epoch == 0 &&
5011 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5012 ssl->in_left > 13 &&
5013 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5014 {
5015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5016 "from the same port" ) );
5017 return( ssl_handle_possible_reconnect( ssl ) );
5018 }
5019 else
5020#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01005021 {
5022 /* Consider buffering the record. */
5023 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
5024 {
5025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5026 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5027 }
5028
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005029 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01005030 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005031 }
5032
5033#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5034 /* Replay detection only works for the current epoch */
5035 if( rec_epoch == ssl->in_epoch &&
5036 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5037 {
5038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5039 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5040 }
5041#endif
5042 }
5043#endif /* MBEDTLS_SSL_PROTO_DTLS */
5044
Hanno Becker52c6dc62017-05-26 16:07:36 +01005045
5046 /* Check length against bounds of the current transform and version */
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005047 if( ssl->transform_in != NULL )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005048 {
5049 if( ssl->in_msglen < ssl->transform_in->minlen )
5050 {
5051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5052 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5053 }
Hanno Becker52c6dc62017-05-26 16:07:36 +01005054 }
5055
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005056 return( 0 );
5057}
Paul Bakker5121ce52009-01-03 21:22:43 +00005058
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005059/*
5060 * If applicable, decrypt (and decompress) record content
5061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005063{
5064 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005067 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5070 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 ret = mbedtls_ssl_hw_record_read( ssl );
5075 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5078 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005079 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005080
5081 if( ret == 0 )
5082 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005083 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005085 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005086 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005087 mbedtls_record rec;
5088
5089 rec.buf = ssl->in_iv;
5090 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5091 - ( ssl->in_iv - ssl->in_buf );
5092 rec.data_len = ssl->in_msglen;
5093 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005094#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005095 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005096 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005097#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005098
5099 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5100 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5101 ssl->conf->transport, rec.ver );
5102 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005103 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5104 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005107
Hanno Beckera0e20d02019-05-15 14:03:01 +01005108#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005109 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5110 ssl->conf->ignore_unexpected_cid
5111 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5112 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005114 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005115 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005116#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005117
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 return( ret );
5119 }
5120
Hanno Becker6430faf2019-05-08 11:57:13 +01005121 if( ssl->in_msgtype != rec.type )
5122 {
5123 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5124 ssl->in_msgtype, rec.type ) );
5125 }
5126
5127 /* The record content type may change during decryption,
5128 * so re-read it. */
5129 ssl->in_msgtype = rec.type;
5130 /* Also update the input buffer, because unfortunately
5131 * the server-side ssl_parse_client_hello() reparses the
5132 * record header when receiving a ClientHello initiating
5133 * a renegotiation. */
5134 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005135 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005136 ssl->in_msglen = rec.data_len;
5137 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5138 ssl->in_len[1] = (unsigned char)( rec.data_len );
5139
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005140 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5141 ssl->in_msg, ssl->in_msglen );
5142
Hanno Beckera0e20d02019-05-15 14:03:01 +01005143#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005144 /* We have already checked the record content type
5145 * in ssl_parse_record_header(), failing or silently
5146 * dropping the record in the case of an unknown type.
5147 *
5148 * Since with the use of CIDs, the record content type
5149 * might change during decryption, re-check the record
5150 * content type, but treat a failure as fatal this time. */
5151 if( ssl_check_record_type( ssl->in_msgtype ) )
5152 {
5153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5154 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5155 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005156#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005157
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005158 if( ssl->in_msglen == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005159 {
5160#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5161 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5162 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5163 {
5164 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5166 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5167 }
5168#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5169
5170 ssl->nb_zero++;
5171
5172 /*
5173 * Three or more empty messages may be a DoS attack
5174 * (excessive CPU consumption).
5175 */
5176 if( ssl->nb_zero > 3 )
5177 {
5178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005179 "messages, possible DoS attack" ) );
5180 /* Treat the records as if they were not properly authenticated,
5181 * thereby failing the connection if we see more than allowed
5182 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005183 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5184 }
5185 }
5186 else
5187 ssl->nb_zero = 0;
5188
5189#if defined(MBEDTLS_SSL_PROTO_DTLS)
5190 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5191 {
5192 ; /* in_ctr read from peer, not maintained internally */
5193 }
5194 else
5195#endif
5196 {
5197 unsigned i;
5198 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5199 if( ++ssl->in_ctr[i - 1] != 0 )
5200 break;
5201
5202 /* The loop goes to its end iff the counter is wrapping */
5203 if( i == ssl_ep_len( ssl ) )
5204 {
5205 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5206 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5207 }
5208 }
5209
Paul Bakker5121ce52009-01-03 21:22:43 +00005210 }
5211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005213 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005215 {
5216 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005219 return( ret );
5220 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005221 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005225 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005228 }
5229#endif
5230
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005231 /* Check actual (decrypted) record content length against
5232 * configured maximum. */
5233 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5234 {
5235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5236 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5237 }
5238
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005239 return( 0 );
5240}
5241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005243
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005244/*
5245 * Read a record.
5246 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005247 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5248 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5249 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005250 */
Hanno Becker1097b342018-08-15 14:09:41 +01005251
5252/* Helper functions for mbedtls_ssl_read_record(). */
5253static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005254static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5255static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005256
Hanno Becker327c93b2018-08-15 13:56:18 +01005257int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005258 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005259{
5260 int ret;
5261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005263
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005264 if( ssl->keep_current_message == 0 )
5265 {
5266 do {
Simon Butcher99000142016-10-13 17:21:01 +01005267
Hanno Becker26994592018-08-15 14:14:59 +01005268 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005269 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005270 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005271
Hanno Beckere74d5562018-08-15 14:26:08 +01005272 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005273 {
Hanno Becker40f50842018-08-15 14:48:01 +01005274#if defined(MBEDTLS_SSL_PROTO_DTLS)
5275 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005276
Hanno Becker40f50842018-08-15 14:48:01 +01005277 /* We only check for buffered messages if the
5278 * current datagram is fully consumed. */
5279 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005280 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005281 {
Hanno Becker40f50842018-08-15 14:48:01 +01005282 if( ssl_load_buffered_message( ssl ) == 0 )
5283 have_buffered = 1;
5284 }
5285
5286 if( have_buffered == 0 )
5287#endif /* MBEDTLS_SSL_PROTO_DTLS */
5288 {
5289 ret = ssl_get_next_record( ssl );
5290 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5291 continue;
5292
5293 if( ret != 0 )
5294 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005295 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005296 return( ret );
5297 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005298 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005299 }
5300
5301 ret = mbedtls_ssl_handle_message_type( ssl );
5302
Hanno Becker40f50842018-08-15 14:48:01 +01005303#if defined(MBEDTLS_SSL_PROTO_DTLS)
5304 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5305 {
5306 /* Buffer future message */
5307 ret = ssl_buffer_message( ssl );
5308 if( ret != 0 )
5309 return( ret );
5310
5311 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5312 }
5313#endif /* MBEDTLS_SSL_PROTO_DTLS */
5314
Hanno Becker90333da2017-10-10 11:27:13 +01005315 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5316 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005317
5318 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005319 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005320 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005321 return( ret );
5322 }
5323
Hanno Becker327c93b2018-08-15 13:56:18 +01005324 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005325 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005326 {
5327 mbedtls_ssl_update_handshake_status( ssl );
5328 }
Simon Butcher99000142016-10-13 17:21:01 +01005329 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005330 else
Simon Butcher99000142016-10-13 17:21:01 +01005331 {
Hanno Becker02f59072018-08-15 14:00:24 +01005332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005333 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005334 }
5335
5336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5337
5338 return( 0 );
5339}
5340
Hanno Becker40f50842018-08-15 14:48:01 +01005341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005342static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005343{
Hanno Becker40f50842018-08-15 14:48:01 +01005344 if( ssl->in_left > ssl->next_record_offset )
5345 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005346
Hanno Becker40f50842018-08-15 14:48:01 +01005347 return( 0 );
5348}
5349
5350static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5351{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005352 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005353 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005354 int ret = 0;
5355
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005356 if( hs == NULL )
5357 return( -1 );
5358
Hanno Beckere00ae372018-08-20 09:39:42 +01005359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5360
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005361 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5362 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5363 {
5364 /* Check if we have seen a ChangeCipherSpec before.
5365 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005366 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005367 {
5368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5369 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005370 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005371 }
5372
Hanno Becker39b8bc92018-08-28 17:17:13 +01005373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005374 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5375 ssl->in_msglen = 1;
5376 ssl->in_msg[0] = 1;
5377
5378 /* As long as they are equal, the exact value doesn't matter. */
5379 ssl->in_left = 0;
5380 ssl->next_record_offset = 0;
5381
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005382 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005383 goto exit;
5384 }
Hanno Becker37f95322018-08-16 13:55:32 +01005385
Hanno Beckerb8f50142018-08-28 10:01:34 +01005386#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005387 /* Debug only */
5388 {
5389 unsigned offset;
5390 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5391 {
5392 hs_buf = &hs->buffering.hs[offset];
5393 if( hs_buf->is_valid == 1 )
5394 {
5395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5396 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005397 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005398 }
5399 }
5400 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005401#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005402
5403 /* Check if we have buffered and/or fully reassembled the
5404 * next handshake message. */
5405 hs_buf = &hs->buffering.hs[0];
5406 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5407 {
5408 /* Synthesize a record containing the buffered HS message. */
5409 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5410 ( hs_buf->data[2] << 8 ) |
5411 hs_buf->data[3];
5412
5413 /* Double-check that we haven't accidentally buffered
5414 * a message that doesn't fit into the input buffer. */
5415 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5416 {
5417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5419 }
5420
5421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5422 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5423 hs_buf->data, msg_len + 12 );
5424
5425 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5426 ssl->in_hslen = msg_len + 12;
5427 ssl->in_msglen = msg_len + 12;
5428 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5429
5430 ret = 0;
5431 goto exit;
5432 }
5433 else
5434 {
5435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5436 hs->in_msg_seq ) );
5437 }
5438
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005439 ret = -1;
5440
5441exit:
5442
5443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5444 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005445}
5446
Hanno Beckera02b0b42018-08-21 17:20:27 +01005447static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5448 size_t desired )
5449{
5450 int offset;
5451 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5453 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005454
Hanno Becker01315ea2018-08-21 17:22:17 +01005455 /* Get rid of future records epoch first, if such exist. */
5456 ssl_free_buffered_record( ssl );
5457
5458 /* Check if we have enough space available now. */
5459 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5460 hs->buffering.total_bytes_buffered ) )
5461 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005463 return( 0 );
5464 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005465
Hanno Becker4f432ad2018-08-28 10:02:32 +01005466 /* We don't have enough space to buffer the next expected handshake
5467 * message. Remove buffers used for future messages to gain space,
5468 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005469 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5470 offset >= 0; offset-- )
5471 {
5472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5473 offset ) );
5474
Hanno Beckerb309b922018-08-23 13:18:05 +01005475 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005476
5477 /* Check if we have enough space available now. */
5478 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5479 hs->buffering.total_bytes_buffered ) )
5480 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005482 return( 0 );
5483 }
5484 }
5485
5486 return( -1 );
5487}
5488
Hanno Becker40f50842018-08-15 14:48:01 +01005489static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5490{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005491 int ret = 0;
5492 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5493
5494 if( hs == NULL )
5495 return( 0 );
5496
5497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5498
5499 switch( ssl->in_msgtype )
5500 {
5501 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005503
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005504 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005505 break;
5506
5507 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005508 {
5509 unsigned recv_msg_seq_offset;
5510 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5511 mbedtls_ssl_hs_buffer *hs_buf;
5512 size_t msg_len = ssl->in_hslen - 12;
5513
5514 /* We should never receive an old handshake
5515 * message - double-check nonetheless. */
5516 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5517 {
5518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5519 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5520 }
5521
5522 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5523 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5524 {
5525 /* Silently ignore -- message too far in the future */
5526 MBEDTLS_SSL_DEBUG_MSG( 2,
5527 ( "Ignore future HS message with sequence number %u, "
5528 "buffering window %u - %u",
5529 recv_msg_seq, ssl->handshake->in_msg_seq,
5530 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5531
5532 goto exit;
5533 }
5534
5535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5536 recv_msg_seq, recv_msg_seq_offset ) );
5537
5538 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5539
5540 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005541 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005542 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005543 size_t reassembly_buf_sz;
5544
Hanno Becker37f95322018-08-16 13:55:32 +01005545 hs_buf->is_fragmented =
5546 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5547
5548 /* We copy the message back into the input buffer
5549 * after reassembly, so check that it's not too large.
5550 * This is an implementation-specific limitation
5551 * and not one from the standard, hence it is not
5552 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005553 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005554 {
5555 /* Ignore message */
5556 goto exit;
5557 }
5558
Hanno Beckere0b150f2018-08-21 15:51:03 +01005559 /* Check if we have enough space to buffer the message. */
5560 if( hs->buffering.total_bytes_buffered >
5561 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5562 {
5563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5565 }
5566
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005567 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5568 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005569
5570 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5571 hs->buffering.total_bytes_buffered ) )
5572 {
5573 if( recv_msg_seq_offset > 0 )
5574 {
5575 /* If we can't buffer a future message because
5576 * of space limitations -- ignore. */
5577 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",
5578 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5579 (unsigned) hs->buffering.total_bytes_buffered ) );
5580 goto exit;
5581 }
Hanno Beckere1801392018-08-21 16:51:05 +01005582 else
5583 {
5584 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",
5585 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5586 (unsigned) hs->buffering.total_bytes_buffered ) );
5587 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005588
Hanno Beckera02b0b42018-08-21 17:20:27 +01005589 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005590 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005591 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",
5592 (unsigned) msg_len,
5593 (unsigned) reassembly_buf_sz,
5594 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005595 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005596 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5597 goto exit;
5598 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005599 }
5600
5601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5602 msg_len ) );
5603
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005604 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5605 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005606 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005607 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005608 goto exit;
5609 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005610 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005611
5612 /* Prepare final header: copy msg_type, length and message_seq,
5613 * then add standardised fragment_offset and fragment_length */
5614 memcpy( hs_buf->data, ssl->in_msg, 6 );
5615 memset( hs_buf->data + 6, 0, 3 );
5616 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5617
5618 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005619
5620 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005621 }
5622 else
5623 {
5624 /* Make sure msg_type and length are consistent */
5625 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5626 {
5627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5628 /* Ignore */
5629 goto exit;
5630 }
5631 }
5632
Hanno Becker4422bbb2018-08-20 09:40:19 +01005633 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005634 {
5635 size_t frag_len, frag_off;
5636 unsigned char * const msg = hs_buf->data + 12;
5637
5638 /*
5639 * Check and copy current fragment
5640 */
5641
5642 /* Validation of header fields already done in
5643 * mbedtls_ssl_prepare_handshake_record(). */
5644 frag_off = ssl_get_hs_frag_off( ssl );
5645 frag_len = ssl_get_hs_frag_len( ssl );
5646
5647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5648 frag_off, frag_len ) );
5649 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5650
5651 if( hs_buf->is_fragmented )
5652 {
5653 unsigned char * const bitmask = msg + msg_len;
5654 ssl_bitmask_set( bitmask, frag_off, frag_len );
5655 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5656 msg_len ) == 0 );
5657 }
5658 else
5659 {
5660 hs_buf->is_complete = 1;
5661 }
5662
5663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5664 hs_buf->is_complete ? "" : "not yet " ) );
5665 }
5666
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005667 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005668 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005669
5670 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005671 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005672 break;
5673 }
5674
5675exit:
5676
5677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5678 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005679}
5680#endif /* MBEDTLS_SSL_PROTO_DTLS */
5681
Hanno Becker1097b342018-08-15 14:09:41 +01005682static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005683{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005684 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005685 * Consume last content-layer message and potentially
5686 * update in_msglen which keeps track of the contents'
5687 * consumption state.
5688 *
5689 * (1) Handshake messages:
5690 * Remove last handshake message, move content
5691 * and adapt in_msglen.
5692 *
5693 * (2) Alert messages:
5694 * Consume whole record content, in_msglen = 0.
5695 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005696 * (3) Change cipher spec:
5697 * Consume whole record content, in_msglen = 0.
5698 *
5699 * (4) Application data:
5700 * Don't do anything - the record layer provides
5701 * the application data as a stream transport
5702 * and consumes through mbedtls_ssl_read only.
5703 *
5704 */
5705
5706 /* Case (1): Handshake messages */
5707 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005708 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005709 /* Hard assertion to be sure that no application data
5710 * is in flight, as corrupting ssl->in_msglen during
5711 * ssl->in_offt != NULL is fatal. */
5712 if( ssl->in_offt != NULL )
5713 {
5714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5715 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5716 }
5717
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005718 /*
5719 * Get next Handshake message in the current record
5720 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721
Hanno Becker4a810fb2017-05-24 16:27:30 +01005722 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005723 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005724 * current handshake content: If DTLS handshake
5725 * fragmentation is used, that's the fragment
5726 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005727 * size here is faulty and should be changed at
5728 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005729 * (2) While it doesn't seem to cause problems, one
5730 * has to be very careful not to assume that in_hslen
5731 * is always <= in_msglen in a sensible communication.
5732 * Again, it's wrong for DTLS handshake fragmentation.
5733 * The following check is therefore mandatory, and
5734 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005735 * Additionally, ssl->in_hslen might be arbitrarily out of
5736 * bounds after handling a DTLS message with an unexpected
5737 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005738 */
5739 if( ssl->in_hslen < ssl->in_msglen )
5740 {
5741 ssl->in_msglen -= ssl->in_hslen;
5742 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5743 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005744
Hanno Becker4a810fb2017-05-24 16:27:30 +01005745 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5746 ssl->in_msg, ssl->in_msglen );
5747 }
5748 else
5749 {
5750 ssl->in_msglen = 0;
5751 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005752
Hanno Becker4a810fb2017-05-24 16:27:30 +01005753 ssl->in_hslen = 0;
5754 }
5755 /* Case (4): Application data */
5756 else if( ssl->in_offt != NULL )
5757 {
5758 return( 0 );
5759 }
5760 /* Everything else (CCS & Alerts) */
5761 else
5762 {
5763 ssl->in_msglen = 0;
5764 }
5765
Hanno Becker1097b342018-08-15 14:09:41 +01005766 return( 0 );
5767}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005768
Hanno Beckere74d5562018-08-15 14:26:08 +01005769static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5770{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005771 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005772 return( 1 );
5773
5774 return( 0 );
5775}
5776
Hanno Becker5f066e72018-08-16 14:56:31 +01005777#if defined(MBEDTLS_SSL_PROTO_DTLS)
5778
5779static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5780{
5781 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5782 if( hs == NULL )
5783 return;
5784
Hanno Becker01315ea2018-08-21 17:22:17 +01005785 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005786 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005787 hs->buffering.total_bytes_buffered -=
5788 hs->buffering.future_record.len;
5789
5790 mbedtls_free( hs->buffering.future_record.data );
5791 hs->buffering.future_record.data = NULL;
5792 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005793}
5794
5795static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5796{
5797 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5798 unsigned char * rec;
5799 size_t rec_len;
5800 unsigned rec_epoch;
5801
5802 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5803 return( 0 );
5804
5805 if( hs == NULL )
5806 return( 0 );
5807
Hanno Becker5f066e72018-08-16 14:56:31 +01005808 rec = hs->buffering.future_record.data;
5809 rec_len = hs->buffering.future_record.len;
5810 rec_epoch = hs->buffering.future_record.epoch;
5811
5812 if( rec == NULL )
5813 return( 0 );
5814
Hanno Becker4cb782d2018-08-20 11:19:05 +01005815 /* Only consider loading future records if the
5816 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005817 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005818 return( 0 );
5819
Hanno Becker5f066e72018-08-16 14:56:31 +01005820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5821
5822 if( rec_epoch != ssl->in_epoch )
5823 {
5824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5825 goto exit;
5826 }
5827
5828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5829
5830 /* Double-check that the record is not too large */
5831 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5832 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5833 {
5834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5836 }
5837
5838 memcpy( ssl->in_hdr, rec, rec_len );
5839 ssl->in_left = rec_len;
5840 ssl->next_record_offset = 0;
5841
5842 ssl_free_buffered_record( ssl );
5843
5844exit:
5845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5846 return( 0 );
5847}
5848
5849static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5850{
5851 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5852 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005853 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005854
5855 /* Don't buffer future records outside handshakes. */
5856 if( hs == NULL )
5857 return( 0 );
5858
5859 /* Only buffer handshake records (we are only interested
5860 * in Finished messages). */
5861 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5862 return( 0 );
5863
5864 /* Don't buffer more than one future epoch record. */
5865 if( hs->buffering.future_record.data != NULL )
5866 return( 0 );
5867
Hanno Becker01315ea2018-08-21 17:22:17 +01005868 /* Don't buffer record if there's not enough buffering space remaining. */
5869 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5870 hs->buffering.total_bytes_buffered ) )
5871 {
5872 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",
5873 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5874 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005875 return( 0 );
5876 }
5877
Hanno Becker5f066e72018-08-16 14:56:31 +01005878 /* Buffer record */
5879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5880 ssl->in_epoch + 1 ) );
5881 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5882 rec_hdr_len + ssl->in_msglen );
5883
5884 /* ssl_parse_record_header() only considers records
5885 * of the next epoch as candidates for buffering. */
5886 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005887 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005888
5889 hs->buffering.future_record.data =
5890 mbedtls_calloc( 1, hs->buffering.future_record.len );
5891 if( hs->buffering.future_record.data == NULL )
5892 {
5893 /* If we run out of RAM trying to buffer a
5894 * record from the next epoch, just ignore. */
5895 return( 0 );
5896 }
5897
Hanno Becker01315ea2018-08-21 17:22:17 +01005898 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005899
Hanno Becker01315ea2018-08-21 17:22:17 +01005900 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005901 return( 0 );
5902}
5903
5904#endif /* MBEDTLS_SSL_PROTO_DTLS */
5905
Hanno Beckere74d5562018-08-15 14:26:08 +01005906static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005907{
5908 int ret;
5909
Hanno Becker5f066e72018-08-16 14:56:31 +01005910#if defined(MBEDTLS_SSL_PROTO_DTLS)
5911 /* We might have buffered a future record; if so,
5912 * and if the epoch matches now, load it.
5913 * On success, this call will set ssl->in_left to
5914 * the length of the buffered record, so that
5915 * the calls to ssl_fetch_input() below will
5916 * essentially be no-ops. */
5917 ret = ssl_load_buffered_record( ssl );
5918 if( ret != 0 )
5919 return( ret );
5920#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005921
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005922 /* Reset in pointers to default state for TLS/DTLS records,
5923 * assuming no CID and no offset between record content and
5924 * record plaintext. */
5925 ssl_update_in_pointers( ssl );
5926
5927 /* Ensure that we have enough space available for the default form
5928 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5929 * with no space for CIDs counted in). */
5930 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5931 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005934 return( ret );
5935 }
5936
5937 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005940 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5941 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005942 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005943 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5944 {
5945 ret = ssl_buffer_future_record( ssl );
5946 if( ret != 0 )
5947 return( ret );
5948
5949 /* Fall through to handling of unexpected records */
5950 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5951 }
5952
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005953 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5954 {
5955 /* Skip unexpected record (but not whole datagram) */
5956 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005957 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005958
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5960 "(header)" ) );
5961 }
5962 else
5963 {
5964 /* Skip invalid record and the rest of the datagram */
5965 ssl->next_record_offset = 0;
5966 ssl->in_left = 0;
5967
5968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5969 "(header)" ) );
5970 }
5971
5972 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005973 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005974 }
5975#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005976 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005977 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005978
5979 /*
5980 * Read and optionally decrypt the message contents
5981 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005983 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005986 return( ret );
5987 }
5988
5989 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005991 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005992 {
Hanno Becker5903de42019-05-03 14:46:38 +01005993 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005994 if( ssl->next_record_offset < ssl->in_left )
5995 {
5996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5997 }
5998 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005999 else
6000#endif
6001 ssl->in_left = 0;
6002
6003 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006006 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006007 {
6008 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006009 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006010 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006011 /* Except when waiting for Finished as a bad mac here
6012 * probably means something went wrong in the handshake
6013 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6014 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6015 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6016 {
6017#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6018 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6019 {
6020 mbedtls_ssl_send_alert_message( ssl,
6021 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6022 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6023 }
6024#endif
6025 return( ret );
6026 }
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006029 if( ssl->conf->badmac_limit != 0 &&
6030 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6033 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006034 }
6035#endif
6036
Hanno Becker4a810fb2017-05-24 16:27:30 +01006037 /* As above, invalid records cause
6038 * dismissal of the whole datagram. */
6039
6040 ssl->next_record_offset = 0;
6041 ssl->in_left = 0;
6042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006044 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006045 }
6046
6047 return( ret );
6048 }
6049 else
6050#endif
6051 {
6052 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6054 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 mbedtls_ssl_send_alert_message( ssl,
6057 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6058 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006059 }
6060#endif
6061 return( ret );
6062 }
6063 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006064
Simon Butcher99000142016-10-13 17:21:01 +01006065 return( 0 );
6066}
6067
6068int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6069{
6070 int ret;
6071
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006072 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006073 * Handle particular types of records
6074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 {
Simon Butcher99000142016-10-13 17:21:01 +01006077 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6078 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006079 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006080 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006081 }
6082
Hanno Beckere678eaa2018-08-21 14:57:46 +01006083 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006084 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006085 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006086 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6088 ssl->in_msglen ) );
6089 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006090 }
6091
Hanno Beckere678eaa2018-08-21 14:57:46 +01006092 if( ssl->in_msg[0] != 1 )
6093 {
6094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6095 ssl->in_msg[0] ) );
6096 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6097 }
6098
6099#if defined(MBEDTLS_SSL_PROTO_DTLS)
6100 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6101 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6102 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6103 {
6104 if( ssl->handshake == NULL )
6105 {
6106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6107 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6108 }
6109
6110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6111 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6112 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006113#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006114 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006117 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006118 if( ssl->in_msglen != 2 )
6119 {
6120 /* Note: Standard allows for more than one 2 byte alert
6121 to be packed in a single message, but Mbed TLS doesn't
6122 currently support this. */
6123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6124 ssl->in_msglen ) );
6125 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6126 }
6127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 ssl->in_msg[0], ssl->in_msg[1] ) );
6130
6131 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006132 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006137 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006139 }
6140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6142 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6145 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006146 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006147
6148#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6149 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6150 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6151 {
Hanno Becker90333da2017-10-10 11:27:13 +01006152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006153 /* Will be handled when trying to parse ServerHello */
6154 return( 0 );
6155 }
6156#endif
6157
6158#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6159 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6160 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6161 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6162 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6163 {
6164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6165 /* Will be handled in mbedtls_ssl_parse_certificate() */
6166 return( 0 );
6167 }
6168#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6169
6170 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006171 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006172 }
6173
Hanno Beckerc76c6192017-06-06 10:03:17 +01006174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006175 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006176 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006177 /* Drop unexpected ApplicationData records,
6178 * except at the beginning of renegotiations */
6179 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6180 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6181#if defined(MBEDTLS_SSL_RENEGOTIATION)
6182 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6183 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006184#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006185 )
6186 {
6187 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6188 return( MBEDTLS_ERR_SSL_NON_FATAL );
6189 }
6190
6191 if( ssl->handshake != NULL &&
6192 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6193 {
6194 ssl_handshake_wrapup_free_hs_transform( ssl );
6195 }
6196 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006197#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006198
Paul Bakker5121ce52009-01-03 21:22:43 +00006199 return( 0 );
6200}
6201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006203{
6204 int ret;
6205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6207 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6208 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006209 {
6210 return( ret );
6211 }
6212
6213 return( 0 );
6214}
6215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006217 unsigned char level,
6218 unsigned char message )
6219{
6220 int ret;
6221
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006222 if( ssl == NULL || ssl->conf == NULL )
6223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006229 ssl->out_msglen = 2;
6230 ssl->out_msg[0] = level;
6231 ssl->out_msg[1] = message;
6232
Hanno Becker67bc7c32018-08-06 11:33:50 +01006233 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006236 return( ret );
6237 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006239
6240 return( 0 );
6241}
6242
Hanno Beckerb9d44792019-02-08 07:19:04 +00006243#if defined(MBEDTLS_X509_CRT_PARSE_C)
6244static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6245{
6246#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6247 if( session->peer_cert != NULL )
6248 {
6249 mbedtls_x509_crt_free( session->peer_cert );
6250 mbedtls_free( session->peer_cert );
6251 session->peer_cert = NULL;
6252 }
6253#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6254 if( session->peer_cert_digest != NULL )
6255 {
6256 /* Zeroization is not necessary. */
6257 mbedtls_free( session->peer_cert_digest );
6258 session->peer_cert_digest = NULL;
6259 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6260 session->peer_cert_digest_len = 0;
6261 }
6262#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6263}
6264#endif /* MBEDTLS_X509_CRT_PARSE_C */
6265
Paul Bakker5121ce52009-01-03 21:22:43 +00006266/*
6267 * Handshake functions
6268 */
Hanno Becker21489932019-02-05 13:20:55 +00006269#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006270/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006272{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006273 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6274 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006277
Hanno Becker7177a882019-02-05 13:36:46 +00006278 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006281 ssl->state++;
6282 return( 0 );
6283 }
6284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006287}
6288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006290{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006291 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6292 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006295
Hanno Becker7177a882019-02-05 13:36:46 +00006296 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006299 ssl->state++;
6300 return( 0 );
6301 }
6302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6304 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006305}
Gilles Peskinef9828522017-05-03 12:28:43 +02006306
Hanno Becker21489932019-02-05 13:20:55 +00006307#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006308/* Some certificate support -> implement write and parse */
6309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006313 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006315 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6316 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006319
Hanno Becker7177a882019-02-05 13:36:46 +00006320 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006323 ssl->state++;
6324 return( 0 );
6325 }
6326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006328 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 {
6330 if( ssl->client_auth == 0 )
6331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006333 ssl->state++;
6334 return( 0 );
6335 }
6336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 /*
6339 * If using SSLv3 and got no cert, send an Alert message
6340 * (otherwise an empty Certificate message will be sent).
6341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6343 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006344 {
6345 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6347 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6348 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006351 goto write_msg;
6352 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355#endif /* MBEDTLS_SSL_CLI_C */
6356#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6362 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 }
6364 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006365#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006368
6369 /*
6370 * 0 . 0 handshake type
6371 * 1 . 3 handshake length
6372 * 4 . 6 length of all certs
6373 * 7 . 9 length of cert. 1
6374 * 10 . n-1 peer certificate
6375 * n . n+2 length of cert. 2
6376 * n+3 . ... upper level cert, etc.
6377 */
6378 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006380
Paul Bakker29087132010-03-21 21:03:34 +00006381 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006382 {
6383 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006384 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006387 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006389 }
6390
6391 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6392 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6393 ssl->out_msg[i + 2] = (unsigned char)( n );
6394
6395 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6396 i += n; crt = crt->next;
6397 }
6398
6399 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6400 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6401 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6402
6403 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6405 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006406
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006407#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006408write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006409#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006410
6411 ssl->state++;
6412
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006413 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006414 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006416 return( ret );
6417 }
6418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006420
Paul Bakkered27a042013-04-18 22:46:23 +02006421 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006422}
6423
Hanno Becker84879e32019-01-31 07:44:03 +00006424#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006425
6426#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006427static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6428 unsigned char *crt_buf,
6429 size_t crt_buf_len )
6430{
6431 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6432
6433 if( peer_crt == NULL )
6434 return( -1 );
6435
6436 if( peer_crt->raw.len != crt_buf_len )
6437 return( -1 );
6438
Hanno Becker46f34d02019-02-08 14:00:04 +00006439 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006440}
Hanno Becker177475a2019-02-05 17:02:46 +00006441#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6442static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6443 unsigned char *crt_buf,
6444 size_t crt_buf_len )
6445{
6446 int ret;
6447 unsigned char const * const peer_cert_digest =
6448 ssl->session->peer_cert_digest;
6449 mbedtls_md_type_t const peer_cert_digest_type =
6450 ssl->session->peer_cert_digest_type;
6451 mbedtls_md_info_t const * const digest_info =
6452 mbedtls_md_info_from_type( peer_cert_digest_type );
6453 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6454 size_t digest_len;
6455
6456 if( peer_cert_digest == NULL || digest_info == NULL )
6457 return( -1 );
6458
6459 digest_len = mbedtls_md_get_size( digest_info );
6460 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6461 return( -1 );
6462
6463 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6464 if( ret != 0 )
6465 return( -1 );
6466
6467 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6468}
6469#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006470#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006471
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006472/*
6473 * Once the certificate message is read, parse it into a cert chain and
6474 * perform basic checks, but leave actual verification to the caller
6475 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006476static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6477 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006478{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006479 int ret;
6480#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6481 int crt_cnt=0;
6482#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006483 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006484 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006489 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6490 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006492 }
6493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6495 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006498 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6499 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006501 }
6502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006504
Paul Bakker5121ce52009-01-03 21:22:43 +00006505 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006507 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006508 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006509
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006510 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006514 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6515 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006517 }
6518
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006519 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6520 i += 3;
6521
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006522 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 while( i < ssl->in_hslen )
6524 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006525 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006526 if ( i + 3 > ssl->in_hslen ) {
6527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006528 mbedtls_ssl_send_alert_message( ssl,
6529 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6530 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006531 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6532 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006533 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6534 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 if( ssl->in_msg[i] != 0 )
6536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006538 mbedtls_ssl_send_alert_message( ssl,
6539 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6540 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006542 }
6543
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006544 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006545 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6546 | (unsigned int) ssl->in_msg[i + 2];
6547 i += 3;
6548
6549 if( n < 128 || i + n > ssl->in_hslen )
6550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006552 mbedtls_ssl_send_alert_message( ssl,
6553 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6554 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006555 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006556 }
6557
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006558 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006559#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6560 if( crt_cnt++ == 0 &&
6561 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6562 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006563 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006564 /* During client-side renegotiation, check that the server's
6565 * end-CRTs hasn't changed compared to the initial handshake,
6566 * mitigating the triple handshake attack. On success, reuse
6567 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6569 if( ssl_check_peer_crt_unchanged( ssl,
6570 &ssl->in_msg[i],
6571 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006572 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6574 mbedtls_ssl_send_alert_message( ssl,
6575 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6576 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6577 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006578 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006579
6580 /* Now we can safely free the original chain. */
6581 ssl_clear_peer_cert( ssl->session );
6582 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006583#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6584
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006585 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006586#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006587 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006588#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006589 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006590 * it in-place from the input buffer instead of making a copy. */
6591 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6592#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006593 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006594 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006595 case 0: /*ok*/
6596 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6597 /* Ignore certificate with an unknown algorithm: maybe a
6598 prior certificate was already trusted. */
6599 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006600
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006601 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6602 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6603 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006604
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006605 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6606 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6607 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006608
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006609 default:
6610 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6611 crt_parse_der_failed:
6612 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6613 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6614 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006615 }
6616
6617 i += n;
6618 }
6619
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006620 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006621 return( 0 );
6622}
6623
Hanno Becker4a55f632019-02-05 12:49:06 +00006624#if defined(MBEDTLS_SSL_SRV_C)
6625static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6626{
6627 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6628 return( -1 );
6629
6630#if defined(MBEDTLS_SSL_PROTO_SSL3)
6631 /*
6632 * Check if the client sent an empty certificate
6633 */
6634 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6635 {
6636 if( ssl->in_msglen == 2 &&
6637 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6638 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6639 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6640 {
6641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6642 return( 0 );
6643 }
6644
6645 return( -1 );
6646 }
6647#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6648
6649#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6650 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6651 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6652 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6653 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6654 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6655 {
6656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6657 return( 0 );
6658 }
6659
6660 return( -1 );
6661#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6662 MBEDTLS_SSL_PROTO_TLS1_2 */
6663}
6664#endif /* MBEDTLS_SSL_SRV_C */
6665
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006666/* Check if a certificate message is expected.
6667 * Return either
6668 * - SSL_CERTIFICATE_EXPECTED, or
6669 * - SSL_CERTIFICATE_SKIP
6670 * indicating whether a Certificate message is expected or not.
6671 */
6672#define SSL_CERTIFICATE_EXPECTED 0
6673#define SSL_CERTIFICATE_SKIP 1
6674static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6675 int authmode )
6676{
6677 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006678 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006679
6680 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6681 return( SSL_CERTIFICATE_SKIP );
6682
6683#if defined(MBEDTLS_SSL_SRV_C)
6684 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6685 {
6686 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6687 return( SSL_CERTIFICATE_SKIP );
6688
6689 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6690 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006691 ssl->session_negotiate->verify_result =
6692 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6693 return( SSL_CERTIFICATE_SKIP );
6694 }
6695 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006696#else
6697 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006698#endif /* MBEDTLS_SSL_SRV_C */
6699
6700 return( SSL_CERTIFICATE_EXPECTED );
6701}
6702
Hanno Becker68636192019-02-05 14:36:34 +00006703static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6704 int authmode,
6705 mbedtls_x509_crt *chain,
6706 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006707{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006708 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006709 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006710 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006711 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006712
Hanno Becker8927c832019-04-03 12:52:50 +01006713 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6714 void *p_vrfy;
6715
Hanno Becker68636192019-02-05 14:36:34 +00006716 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6717 return( 0 );
6718
Hanno Becker8927c832019-04-03 12:52:50 +01006719 if( ssl->f_vrfy != NULL )
6720 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006722 f_vrfy = ssl->f_vrfy;
6723 p_vrfy = ssl->p_vrfy;
6724 }
6725 else
6726 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006728 f_vrfy = ssl->conf->f_vrfy;
6729 p_vrfy = ssl->conf->p_vrfy;
6730 }
6731
Hanno Becker68636192019-02-05 14:36:34 +00006732 /*
6733 * Main check: verify certificate
6734 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006735#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6736 if( ssl->conf->f_ca_cb != NULL )
6737 {
6738 ((void) rs_ctx);
6739 have_ca_chain = 1;
6740
6741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006742 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006743 chain,
6744 ssl->conf->f_ca_cb,
6745 ssl->conf->p_ca_cb,
6746 ssl->conf->cert_profile,
6747 ssl->hostname,
6748 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006749 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006750 }
6751 else
6752#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6753 {
6754 mbedtls_x509_crt *ca_chain;
6755 mbedtls_x509_crl *ca_crl;
6756
6757#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6758 if( ssl->handshake->sni_ca_chain != NULL )
6759 {
6760 ca_chain = ssl->handshake->sni_ca_chain;
6761 ca_crl = ssl->handshake->sni_ca_crl;
6762 }
6763 else
6764#endif
6765 {
6766 ca_chain = ssl->conf->ca_chain;
6767 ca_crl = ssl->conf->ca_crl;
6768 }
6769
6770 if( ca_chain != NULL )
6771 have_ca_chain = 1;
6772
6773 ret = mbedtls_x509_crt_verify_restartable(
6774 chain,
6775 ca_chain, ca_crl,
6776 ssl->conf->cert_profile,
6777 ssl->hostname,
6778 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006779 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006780 }
Hanno Becker68636192019-02-05 14:36:34 +00006781
6782 if( ret != 0 )
6783 {
6784 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6785 }
6786
6787#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6788 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6789 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6790#endif
6791
6792 /*
6793 * Secondary checks: always done, but change 'ret' only if it was 0
6794 */
6795
6796#if defined(MBEDTLS_ECP_C)
6797 {
6798 const mbedtls_pk_context *pk = &chain->pk;
6799
6800 /* If certificate uses an EC key, make sure the curve is OK */
6801 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6802 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6803 {
6804 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6805
6806 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6807 if( ret == 0 )
6808 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6809 }
6810 }
6811#endif /* MBEDTLS_ECP_C */
6812
6813 if( mbedtls_ssl_check_cert_usage( chain,
6814 ciphersuite_info,
6815 ! ssl->conf->endpoint,
6816 &ssl->session_negotiate->verify_result ) != 0 )
6817 {
6818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6819 if( ret == 0 )
6820 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6821 }
6822
6823 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6824 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6825 * with details encoded in the verification flags. All other kinds
6826 * of error codes, including those from the user provided f_vrfy
6827 * functions, are treated as fatal and lead to a failure of
6828 * ssl_parse_certificate even if verification was optional. */
6829 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6830 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6831 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6832 {
6833 ret = 0;
6834 }
6835
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006836 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006837 {
6838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6839 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6840 }
6841
6842 if( ret != 0 )
6843 {
6844 uint8_t alert;
6845
6846 /* The certificate may have been rejected for several reasons.
6847 Pick one and send the corresponding alert. Which alert to send
6848 may be a subject of debate in some cases. */
6849 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6850 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6851 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6852 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6853 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6854 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6855 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6856 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6857 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6858 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6859 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6860 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6861 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6862 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6863 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6864 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6865 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6866 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6867 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6868 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6869 else
6870 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6871 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6872 alert );
6873 }
6874
6875#if defined(MBEDTLS_DEBUG_C)
6876 if( ssl->session_negotiate->verify_result != 0 )
6877 {
6878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6879 ssl->session_negotiate->verify_result ) );
6880 }
6881 else
6882 {
6883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6884 }
6885#endif /* MBEDTLS_DEBUG_C */
6886
6887 return( ret );
6888}
6889
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006890#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6891static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6892 unsigned char *start, size_t len )
6893{
6894 int ret;
6895 /* Remember digest of the peer's end-CRT. */
6896 ssl->session_negotiate->peer_cert_digest =
6897 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6898 if( ssl->session_negotiate->peer_cert_digest == NULL )
6899 {
6900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6901 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6902 mbedtls_ssl_send_alert_message( ssl,
6903 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6904 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6905
6906 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6907 }
6908
6909 ret = mbedtls_md( mbedtls_md_info_from_type(
6910 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6911 start, len,
6912 ssl->session_negotiate->peer_cert_digest );
6913
6914 ssl->session_negotiate->peer_cert_digest_type =
6915 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6916 ssl->session_negotiate->peer_cert_digest_len =
6917 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6918
6919 return( ret );
6920}
6921
6922static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6923 unsigned char *start, size_t len )
6924{
6925 unsigned char *end = start + len;
6926 int ret;
6927
6928 /* Make a copy of the peer's raw public key. */
6929 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6930 ret = mbedtls_pk_parse_subpubkey( &start, end,
6931 &ssl->handshake->peer_pubkey );
6932 if( ret != 0 )
6933 {
6934 /* We should have parsed the public key before. */
6935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6936 }
6937
6938 return( 0 );
6939}
6940#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6941
Hanno Becker68636192019-02-05 14:36:34 +00006942int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6943{
6944 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006945 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006946#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6947 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6948 ? ssl->handshake->sni_authmode
6949 : ssl->conf->authmode;
6950#else
6951 const int authmode = ssl->conf->authmode;
6952#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006953 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006954 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006955
6956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6957
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006958 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6959 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006960 {
6961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006962 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006963 }
6964
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006965#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6966 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006967 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006968 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006969 chain = ssl->handshake->ecrs_peer_cert;
6970 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006971 goto crt_verify;
6972 }
6973#endif
6974
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006975 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006976 {
6977 /* mbedtls_ssl_read_record may have sent an alert already. We
6978 let it decide whether to alert. */
6979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006980 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006981 }
6982
Hanno Becker4a55f632019-02-05 12:49:06 +00006983#if defined(MBEDTLS_SSL_SRV_C)
6984 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6985 {
6986 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006987
6988 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006989 ret = 0;
6990 else
6991 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006992
Hanno Becker6bdfab22019-02-05 13:11:17 +00006993 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006994 }
6995#endif /* MBEDTLS_SSL_SRV_C */
6996
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006997 /* Clear existing peer CRT structure in case we tried to
6998 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006999 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007000
7001 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7002 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007003 {
7004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7005 sizeof( mbedtls_x509_crt ) ) );
7006 mbedtls_ssl_send_alert_message( ssl,
7007 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7008 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007009
Hanno Becker3dad3112019-02-05 17:19:52 +00007010 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7011 goto exit;
7012 }
7013 mbedtls_x509_crt_init( chain );
7014
7015 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007016 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007017 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007018
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007019#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7020 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007021 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007022
7023crt_verify:
7024 if( ssl->handshake->ecrs_enabled)
7025 rs_ctx = &ssl->handshake->ecrs_ctx;
7026#endif
7027
Hanno Becker68636192019-02-05 14:36:34 +00007028 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007029 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007030 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007031 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007032
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007033#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007034 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007035 unsigned char *crt_start, *pk_start;
7036 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007037
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007038 /* We parse the CRT chain without copying, so
7039 * these pointers point into the input buffer,
7040 * and are hence still valid after freeing the
7041 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007042
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007043 crt_start = chain->raw.p;
7044 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007045
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007046 pk_start = chain->pk_raw.p;
7047 pk_len = chain->pk_raw.len;
7048
7049 /* Free the CRT structures before computing
7050 * digest and copying the peer's public key. */
7051 mbedtls_x509_crt_free( chain );
7052 mbedtls_free( chain );
7053 chain = NULL;
7054
7055 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007056 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007057 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007058
7059 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7060 if( ret != 0 )
7061 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007062 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007063#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7064 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007065 ssl->session_negotiate->peer_cert = chain;
7066 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007067#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Hanno Becker6bdfab22019-02-05 13:11:17 +00007071exit:
7072
Hanno Becker3dad3112019-02-05 17:19:52 +00007073 if( ret == 0 )
7074 ssl->state++;
7075
7076#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7077 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7078 {
7079 ssl->handshake->ecrs_peer_cert = chain;
7080 chain = NULL;
7081 }
7082#endif
7083
7084 if( chain != NULL )
7085 {
7086 mbedtls_x509_crt_free( chain );
7087 mbedtls_free( chain );
7088 }
7089
Paul Bakker5121ce52009-01-03 21:22:43 +00007090 return( ret );
7091}
Hanno Becker21489932019-02-05 13:20:55 +00007092#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007095{
7096 int ret;
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 ssl->out_msglen = 1;
7102 ssl->out_msg[0] = 1;
7103
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 ssl->state++;
7105
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007106 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007107 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007109 return( ret );
7110 }
7111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113
7114 return( 0 );
7115}
7116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007118{
7119 int ret;
7120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007122
Hanno Becker327c93b2018-08-15 13:56:18 +01007123 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007126 return( ret );
7127 }
7128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007132 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7133 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007135 }
7136
Hanno Beckere678eaa2018-08-21 14:57:46 +01007137 /* CCS records are only accepted if they have length 1 and content '1',
7138 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007139
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007140 /*
7141 * Switch to our negotiated transform and session parameters for inbound
7142 * data.
7143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007145 ssl->transform_in = ssl->transform_negotiate;
7146 ssl->session_in = ssl->session_negotiate;
7147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007149 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007152 ssl_dtls_replay_reset( ssl );
7153#endif
7154
7155 /* Increment epoch */
7156 if( ++ssl->in_epoch == 0 )
7157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007159 /* This is highly unlikely to happen for legitimate reasons, so
7160 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007162 }
7163 }
7164 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007166 memset( ssl->in_ctr, 0, 8 );
7167
Hanno Becker79594fd2019-05-08 09:38:41 +01007168 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7171 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007176 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7177 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007179 }
7180 }
7181#endif
7182
Paul Bakker5121ce52009-01-03 21:22:43 +00007183 ssl->state++;
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007186
7187 return( 0 );
7188}
7189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7191 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007192{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007193 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7196 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7197 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007198 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007199 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7202#if defined(MBEDTLS_SHA512_C)
7203 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007204 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7205 else
7206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#if defined(MBEDTLS_SHA256_C)
7208 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007209 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007210 else
7211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007215 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007216 }
Paul Bakker380da532012-04-18 16:10:25 +00007217}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7222 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007223 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7224 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7227#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007228#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007229 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007230 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7231#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007232 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007233#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007237 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007238 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007239#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007240 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007241#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007244}
7245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007247 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007248{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7250 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007251 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7252 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007253#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7255#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007256#if defined(MBEDTLS_USE_PSA_CRYPTO)
7257 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7258#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007259 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007260#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007263#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007264 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007265#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007266 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007267#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007268#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007270}
7271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7273 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7274static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007275 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007276{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007277 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7278 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007279}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007280#endif
Paul Bakker380da532012-04-18 16:10:25 +00007281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7283#if defined(MBEDTLS_SHA256_C)
7284static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007285 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007286{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007287#if defined(MBEDTLS_USE_PSA_CRYPTO)
7288 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7289#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007290 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007291#endif
Paul Bakker380da532012-04-18 16:10:25 +00007292}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007293#endif
Paul Bakker380da532012-04-18 16:10:25 +00007294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295#if defined(MBEDTLS_SHA512_C)
7296static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007297 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007298{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007299#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007300 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007301#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007302 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007303#endif
Paul Bakker380da532012-04-18 16:10:25 +00007304}
Paul Bakker769075d2012-11-24 11:26:46 +01007305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007309static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007311{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007312 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007313 mbedtls_md5_context md5;
7314 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007315
Paul Bakker5121ce52009-01-03 21:22:43 +00007316 unsigned char padbuf[48];
7317 unsigned char md5sum[16];
7318 unsigned char sha1sum[20];
7319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007321 if( !session )
7322 session = ssl->session;
7323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007325
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007326 mbedtls_md5_init( &md5 );
7327 mbedtls_sha1_init( &sha1 );
7328
7329 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7330 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007331
7332 /*
7333 * SSLv3:
7334 * hash =
7335 * MD5( master + pad2 +
7336 * MD5( handshake + sender + master + pad1 ) )
7337 * + SHA1( master + pad2 +
7338 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007339 */
7340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007342 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7343 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007344#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007347 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7348 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007349#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007352 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007353
Paul Bakker1ef83d62012-04-11 12:09:53 +00007354 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007356 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7357 mbedtls_md5_update_ret( &md5, session->master, 48 );
7358 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7359 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007361 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7362 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7363 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7364 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007365
Paul Bakker1ef83d62012-04-11 12:09:53 +00007366 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007367
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007368 mbedtls_md5_starts_ret( &md5 );
7369 mbedtls_md5_update_ret( &md5, session->master, 48 );
7370 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7371 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7372 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007373
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007374 mbedtls_sha1_starts_ret( &sha1 );
7375 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7376 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7377 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7378 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007381
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007382 mbedtls_md5_free( &md5 );
7383 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007384
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007385 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7386 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7387 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007390}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007394static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007396{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007397 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007398 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007399 mbedtls_md5_context md5;
7400 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007401 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007404 if( !session )
7405 session = ssl->session;
7406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007408
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007409 mbedtls_md5_init( &md5 );
7410 mbedtls_sha1_init( &sha1 );
7411
7412 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7413 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007414
Paul Bakker1ef83d62012-04-11 12:09:53 +00007415 /*
7416 * TLSv1:
7417 * hash = PRF( master, finished_label,
7418 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7419 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007422 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7423 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007424#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007427 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7428 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007429#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007432 ? "client finished"
7433 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007434
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007435 mbedtls_md5_finish_ret( &md5, padbuf );
7436 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007437
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007438 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007439 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007442
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007443 mbedtls_md5_free( &md5 );
7444 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007445
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007446 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007449}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7453#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007454static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007456{
7457 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007458 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007459 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007460#if defined(MBEDTLS_USE_PSA_CRYPTO)
7461 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007462 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007463 psa_status_t status;
7464#else
7465 mbedtls_sha256_context sha256;
7466#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007469 if( !session )
7470 session = ssl->session;
7471
Andrzej Kurekeb342242019-01-29 09:14:33 -05007472 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7473 ? "client finished"
7474 : "server finished";
7475
7476#if defined(MBEDTLS_USE_PSA_CRYPTO)
7477 sha256_psa = psa_hash_operation_init();
7478
7479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7480
7481 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7482 if( status != PSA_SUCCESS )
7483 {
7484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7485 return;
7486 }
7487
7488 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7489 if( status != PSA_SUCCESS )
7490 {
7491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7492 return;
7493 }
7494 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7495#else
7496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007497 mbedtls_sha256_init( &sha256 );
7498
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007500
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007501 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007502
7503 /*
7504 * TLSv1.2:
7505 * hash = PRF( master, finished_label,
7506 * Hash( handshake ) )[0.11]
7507 */
7508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509#if !defined(MBEDTLS_SHA256_ALT)
7510 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007511 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007512#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007513
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007514 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007515 mbedtls_sha256_free( &sha256 );
7516#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007517
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007518 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007519 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007522
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007523 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007530static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007532{
7533 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007534 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007535 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007536#if defined(MBEDTLS_USE_PSA_CRYPTO)
7537 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007538 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007539 psa_status_t status;
7540#else
7541 mbedtls_sha512_context sha512;
7542#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007545 if( !session )
7546 session = ssl->session;
7547
Andrzej Kurekeb342242019-01-29 09:14:33 -05007548 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7549 ? "client finished"
7550 : "server finished";
7551
7552#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007553 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007554
7555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7556
Andrzej Kurek972fba52019-01-30 03:29:12 -05007557 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007558 if( status != PSA_SUCCESS )
7559 {
7560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7561 return;
7562 }
7563
Andrzej Kurek972fba52019-01-30 03:29:12 -05007564 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007565 if( status != PSA_SUCCESS )
7566 {
7567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7568 return;
7569 }
7570 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7571#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007572 mbedtls_sha512_init( &sha512 );
7573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007575
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007576 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007577
7578 /*
7579 * TLSv1.2:
7580 * hash = PRF( master, finished_label,
7581 * Hash( handshake ) )[0.11]
7582 */
7583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007585 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7586 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007587#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007588
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007589 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007590 mbedtls_sha512_free( &sha512 );
7591#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007592
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007593 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007594 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007597
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007598 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#endif /* MBEDTLS_SHA512_C */
7603#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007606{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007608
7609 /*
7610 * Free our handshake params
7611 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007612 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007614 ssl->handshake = NULL;
7615
7616 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007617 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007618 */
7619 if( ssl->transform )
7620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 mbedtls_ssl_transform_free( ssl->transform );
7622 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007623 }
7624 ssl->transform = ssl->transform_negotiate;
7625 ssl->transform_negotiate = NULL;
7626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007628}
7629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007631{
7632 int resume = ssl->handshake->resume;
7633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636#if defined(MBEDTLS_SSL_RENEGOTIATION)
7637 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640 ssl->renego_records_seen = 0;
7641 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007642#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007643
7644 /*
7645 * Free the previous session and switch in the current one
7646 */
Paul Bakker0a597072012-09-25 21:55:46 +00007647 if( ssl->session )
7648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007650 /* RFC 7366 3.1: keep the EtM state */
7651 ssl->session_negotiate->encrypt_then_mac =
7652 ssl->session->encrypt_then_mac;
7653#endif
7654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 mbedtls_ssl_session_free( ssl->session );
7656 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007657 }
7658 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007659 ssl->session_negotiate = NULL;
7660
Paul Bakker0a597072012-09-25 21:55:46 +00007661 /*
7662 * Add cache entry
7663 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007664 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007665 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007666 resume == 0 )
7667 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007668 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007670 }
Paul Bakker0a597072012-09-25 21:55:46 +00007671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007674 ssl->handshake->flight != NULL )
7675 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007676 /* Cancel handshake timer */
7677 ssl_set_timer( ssl, 0 );
7678
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007679 /* Keep last flight around in case we need to resend it:
7680 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007682 }
7683 else
7684#endif
7685 ssl_handshake_wrapup_free_hs_transform( ssl );
7686
Paul Bakker48916f92012-09-16 19:57:18 +00007687 ssl->state++;
7688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007690}
7691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007693{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007694 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007697
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007698 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007699
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007700 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007701
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007702 /*
7703 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7704 * may define some other value. Currently (early 2016), no defined
7705 * ciphersuite does this (and this is unlikely to change as activity has
7706 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007711 ssl->verify_data_len = hash_len;
7712 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007713#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007714
Paul Bakker5121ce52009-01-03 21:22:43 +00007715 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007716 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7717 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
7719 /*
7720 * In case of session resuming, invert the client and server
7721 * ChangeCipherSpec messages order.
7722 */
Paul Bakker0a597072012-09-25 21:55:46 +00007723 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007726 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007728#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007730 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007732#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007733 }
7734 else
7735 ssl->state++;
7736
Paul Bakker48916f92012-09-16 19:57:18 +00007737 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007738 * Switch to our negotiated transform and session parameters for outbound
7739 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007740 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007744 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007745 {
7746 unsigned char i;
7747
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007748 /* Remember current epoch settings for resending */
7749 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007750 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007751
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007752 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007753 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007754
7755 /* Increment epoch */
7756 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007757 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007758 break;
7759
7760 /* The loop goes to its end iff the counter is wrapping */
7761 if( i == 0 )
7762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7764 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007765 }
7766 }
7767 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007769 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007770
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007771 ssl->transform_out = ssl->transform_negotiate;
7772 ssl->session_out = ssl->session_negotiate;
7773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7775 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7780 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007781 }
7782 }
7783#endif
7784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007786 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007788#endif
7789
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007790 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007791 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007793 return( ret );
7794 }
7795
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007796#if defined(MBEDTLS_SSL_PROTO_DTLS)
7797 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7798 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7799 {
7800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7801 return( ret );
7802 }
7803#endif
7804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007806
7807 return( 0 );
7808}
7809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007811#define SSL_MAX_HASH_LEN 36
7812#else
7813#define SSL_MAX_HASH_LEN 12
7814#endif
7815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007817{
Paul Bakker23986e52011-04-24 08:57:21 +00007818 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007819 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007820 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007823
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007824 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007825
Hanno Becker327c93b2018-08-15 13:56:18 +01007826 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 return( ret );
7830 }
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007835 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7836 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007838 }
7839
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007840 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841#if defined(MBEDTLS_SSL_PROTO_SSL3)
7842 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007843 hash_len = 36;
7844 else
7845#endif
7846 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7849 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007852 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7853 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007855 }
7856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007858 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007861 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7862 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007864 }
7865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007867 ssl->verify_data_len = hash_len;
7868 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007869#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007870
Paul Bakker0a597072012-09-25 21:55:46 +00007871 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007874 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007878 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007880#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007881 }
7882 else
7883 ssl->state++;
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007886 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007888#endif
7889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007891
7892 return( 0 );
7893}
7894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7900 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7901 mbedtls_md5_init( &handshake->fin_md5 );
7902 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007903 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7904 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7907#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007908#if defined(MBEDTLS_USE_PSA_CRYPTO)
7909 handshake->fin_sha256_psa = psa_hash_operation_init();
7910 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7911#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007913 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007914#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007917#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007918 handshake->fin_sha384_psa = psa_hash_operation_init();
7919 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007920#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007922 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007923#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007924#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007925#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007926
7927 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007928
7929#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7930 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7931 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7932#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_DHM_C)
7935 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_ECDH_C)
7938 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007939#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007940#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007941 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007942#if defined(MBEDTLS_SSL_CLI_C)
7943 handshake->ecjpake_cache = NULL;
7944 handshake->ecjpake_cache_len = 0;
7945#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007946#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007947
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007948#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007949 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007950#endif
7951
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007952#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7953 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7954#endif
Hanno Becker75173122019-02-06 16:18:31 +00007955
7956#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7957 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7958 mbedtls_pk_init( &handshake->peer_pubkey );
7959#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007960}
7961
Hanno Beckera18d1322018-01-03 14:27:32 +00007962void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7967 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007968
Hanno Beckerd56ed242018-01-03 15:32:51 +00007969#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 mbedtls_md_init( &transform->md_ctx_enc );
7971 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007972#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007973}
7974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007976{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007978}
7979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007981{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007983 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007985 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007987 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007988 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007989
7990 /*
7991 * Either the pointers are now NULL or cleared properly and can be freed.
7992 * Now allocate missing structures.
7993 */
7994 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007995 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007996 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007997 }
Paul Bakker48916f92012-09-16 19:57:18 +00007998
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007999 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008000 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008001 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008002 }
Paul Bakker48916f92012-09-16 19:57:18 +00008003
Paul Bakker82788fb2014-10-20 13:59:19 +02008004 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008005 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008006 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008007 }
Paul Bakker48916f92012-09-16 19:57:18 +00008008
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008009 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008010 if( ssl->handshake == NULL ||
8011 ssl->transform_negotiate == NULL ||
8012 ssl->session_negotiate == NULL )
8013 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016 mbedtls_free( ssl->handshake );
8017 mbedtls_free( ssl->transform_negotiate );
8018 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008019
8020 ssl->handshake = NULL;
8021 ssl->transform_negotiate = NULL;
8022 ssl->session_negotiate = NULL;
8023
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008024 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008025 }
8026
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008027 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008029 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008030 ssl_handshake_params_init( ssl->handshake );
8031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008033 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8034 {
8035 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008036
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008037 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8038 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8039 else
8040 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008041
8042 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008043 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008044#endif
8045
Paul Bakker48916f92012-09-16 19:57:18 +00008046 return( 0 );
8047}
8048
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008049#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008050/* Dummy cookie callbacks for defaults */
8051static int ssl_cookie_write_dummy( void *ctx,
8052 unsigned char **p, unsigned char *end,
8053 const unsigned char *cli_id, size_t cli_id_len )
8054{
8055 ((void) ctx);
8056 ((void) p);
8057 ((void) end);
8058 ((void) cli_id);
8059 ((void) cli_id_len);
8060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008062}
8063
8064static int ssl_cookie_check_dummy( void *ctx,
8065 const unsigned char *cookie, size_t cookie_len,
8066 const unsigned char *cli_id, size_t cli_id_len )
8067{
8068 ((void) ctx);
8069 ((void) cookie);
8070 ((void) cookie_len);
8071 ((void) cli_id);
8072 ((void) cli_id_len);
8073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008075}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008076#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008077
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008078/* Once ssl->out_hdr as the address of the beginning of the
8079 * next outgoing record is set, deduce the other pointers.
8080 *
8081 * Note: For TLS, we save the implicit record sequence number
8082 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8083 * and the caller has to make sure there's space for this.
8084 */
8085
8086static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8087 mbedtls_ssl_transform *transform )
8088{
8089#if defined(MBEDTLS_SSL_PROTO_DTLS)
8090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8091 {
8092 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008093#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008094 ssl->out_cid = ssl->out_ctr + 8;
8095 ssl->out_len = ssl->out_cid;
8096 if( transform != NULL )
8097 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008098#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008099 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008100#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008101 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008102 }
8103 else
8104#endif
8105 {
8106 ssl->out_ctr = ssl->out_hdr - 8;
8107 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008108#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008109 ssl->out_cid = ssl->out_len;
8110#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008111 ssl->out_iv = ssl->out_hdr + 5;
8112 }
8113
8114 /* Adjust out_msg to make space for explicit IV, if used. */
8115 if( transform != NULL &&
8116 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8117 {
8118 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8119 }
8120 else
8121 ssl->out_msg = ssl->out_iv;
8122}
8123
8124/* Once ssl->in_hdr as the address of the beginning of the
8125 * next incoming record is set, deduce the other pointers.
8126 *
8127 * Note: For TLS, we save the implicit record sequence number
8128 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8129 * and the caller has to make sure there's space for this.
8130 */
8131
Hanno Becker79594fd2019-05-08 09:38:41 +01008132static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008133{
Hanno Becker79594fd2019-05-08 09:38:41 +01008134 /* This function sets the pointers to match the case
8135 * of unprotected TLS/DTLS records, with both ssl->in_iv
8136 * and ssl->in_msg pointing to the beginning of the record
8137 * content.
8138 *
8139 * When decrypting a protected record, ssl->in_msg
8140 * will be shifted to point to the beginning of the
8141 * record plaintext.
8142 */
8143
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008144#if defined(MBEDTLS_SSL_PROTO_DTLS)
8145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8146 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008147 /* This sets the header pointers to match records
8148 * without CID. When we receive a record containing
8149 * a CID, the fields are shifted accordingly in
8150 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008151 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008152#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008153 ssl->in_cid = ssl->in_ctr + 8;
8154 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008155#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008156 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008157#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008158 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008159 }
8160 else
8161#endif
8162 {
8163 ssl->in_ctr = ssl->in_hdr - 8;
8164 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008165#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008166 ssl->in_cid = ssl->in_len;
8167#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168 ssl->in_iv = ssl->in_hdr + 5;
8169 }
8170
Hanno Becker79594fd2019-05-08 09:38:41 +01008171 /* This will be adjusted at record decryption time. */
8172 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008173}
8174
Paul Bakker5121ce52009-01-03 21:22:43 +00008175/*
8176 * Initialize an SSL context
8177 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008178void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8179{
8180 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8181}
8182
8183/*
8184 * Setup an SSL context
8185 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008186
8187static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8188{
8189 /* Set the incoming and outgoing record pointers. */
8190#if defined(MBEDTLS_SSL_PROTO_DTLS)
8191 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8192 {
8193 ssl->out_hdr = ssl->out_buf;
8194 ssl->in_hdr = ssl->in_buf;
8195 }
8196 else
8197#endif /* MBEDTLS_SSL_PROTO_DTLS */
8198 {
8199 ssl->out_hdr = ssl->out_buf + 8;
8200 ssl->in_hdr = ssl->in_buf + 8;
8201 }
8202
8203 /* Derive other internal pointers. */
8204 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008205 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008206}
8207
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008208int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008209 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008210{
Paul Bakker48916f92012-09-16 19:57:18 +00008211 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008212
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008213 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008214
8215 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008216 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008217 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008218
8219 /* Set to NULL in case of an error condition */
8220 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008221
Angus Grattond8213d02016-05-25 20:56:48 +10008222 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8223 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008224 {
Angus Grattond8213d02016-05-25 20:56:48 +10008225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008226 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008227 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008228 }
8229
8230 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8231 if( ssl->out_buf == NULL )
8232 {
8233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008234 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008235 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008236 }
8237
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008238 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008239
Paul Bakker48916f92012-09-16 19:57:18 +00008240 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008241 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008242
8243 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008244
8245error:
8246 mbedtls_free( ssl->in_buf );
8247 mbedtls_free( ssl->out_buf );
8248
8249 ssl->conf = NULL;
8250
8251 ssl->in_buf = NULL;
8252 ssl->out_buf = NULL;
8253
8254 ssl->in_hdr = NULL;
8255 ssl->in_ctr = NULL;
8256 ssl->in_len = NULL;
8257 ssl->in_iv = NULL;
8258 ssl->in_msg = NULL;
8259
8260 ssl->out_hdr = NULL;
8261 ssl->out_ctr = NULL;
8262 ssl->out_len = NULL;
8263 ssl->out_iv = NULL;
8264 ssl->out_msg = NULL;
8265
k-stachowiak9f7798e2018-07-31 16:52:32 +02008266 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008267}
8268
8269/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008270 * Reset an initialized and used SSL context for re-use while retaining
8271 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008272 *
8273 * If partial is non-zero, keep data in the input buffer and client ID.
8274 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008275 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008276static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008277{
Paul Bakker48916f92012-09-16 19:57:18 +00008278 int ret;
8279
Hanno Becker7e772132018-08-10 12:38:21 +01008280#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8281 !defined(MBEDTLS_SSL_SRV_C)
8282 ((void) partial);
8283#endif
8284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008286
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008287 /* Cancel any possibly running timer */
8288 ssl_set_timer( ssl, 0 );
8289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290#if defined(MBEDTLS_SSL_RENEGOTIATION)
8291 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008292 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008293
8294 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8296 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008299
Paul Bakker7eb013f2011-10-06 12:37:39 +00008300 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008301 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008302
8303 ssl->in_msgtype = 0;
8304 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008306 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008307 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008309#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008310 ssl_dtls_replay_reset( ssl );
8311#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008312
8313 ssl->in_hslen = 0;
8314 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008315
8316 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008317
8318 ssl->out_msgtype = 0;
8319 ssl->out_msglen = 0;
8320 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008321#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8322 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008323 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008324#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008325
Hanno Becker19859472018-08-06 09:40:20 +01008326 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8327
Paul Bakker48916f92012-09-16 19:57:18 +00008328 ssl->transform_in = NULL;
8329 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008330
Hanno Becker78640902018-08-13 16:35:15 +01008331 ssl->session_in = NULL;
8332 ssl->session_out = NULL;
8333
Angus Grattond8213d02016-05-25 20:56:48 +10008334 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008335
8336#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008337 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008338#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8339 {
8340 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008341 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008342 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8345 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8348 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8351 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008352 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008353 }
8354#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008355
Paul Bakker48916f92012-09-16 19:57:18 +00008356 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 mbedtls_ssl_transform_free( ssl->transform );
8359 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008360 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008361 }
Paul Bakker48916f92012-09-16 19:57:18 +00008362
Paul Bakkerc0463502013-02-14 11:19:38 +01008363 if( ssl->session )
8364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 mbedtls_ssl_session_free( ssl->session );
8366 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008367 ssl->session = NULL;
8368 }
8369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008371 ssl->alpn_chosen = NULL;
8372#endif
8373
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008374#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008375#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008376 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008377#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008378 {
8379 mbedtls_free( ssl->cli_id );
8380 ssl->cli_id = NULL;
8381 ssl->cli_id_len = 0;
8382 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008383#endif
8384
Paul Bakker48916f92012-09-16 19:57:18 +00008385 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8386 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008387
8388 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008389}
8390
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008391/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008392 * Reset an initialized and used SSL context for re-use while retaining
8393 * all application-set variables, function pointers and data.
8394 */
8395int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8396{
8397 return( ssl_session_reset_int( ssl, 0 ) );
8398}
8399
8400/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008401 * SSL set accessors
8402 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008403void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008404{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008405 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008406}
8407
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008408void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008409{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008410 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008411}
8412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008414void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008415{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008416 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008417}
8418#endif
8419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008421void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008422{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008423 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008424}
8425#endif
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008428
Hanno Becker1841b0a2018-08-24 11:13:57 +01008429void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8430 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008431{
8432 ssl->disable_datagram_packing = !allow_packing;
8433}
8434
8435void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8436 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008437{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008438 conf->hs_timeout_min = min;
8439 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008440}
8441#endif
8442
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008443void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008444{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008445 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008446}
8447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008449void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008450 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008451 void *p_vrfy )
8452{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008453 conf->f_vrfy = f_vrfy;
8454 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008457
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008458void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008459 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008460 void *p_rng )
8461{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008462 conf->f_rng = f_rng;
8463 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008464}
8465
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008466void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008467 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008468 void *p_dbg )
8469{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008470 conf->f_dbg = f_dbg;
8471 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008472}
8473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008474void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008475 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008476 mbedtls_ssl_send_t *f_send,
8477 mbedtls_ssl_recv_t *f_recv,
8478 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008479{
8480 ssl->p_bio = p_bio;
8481 ssl->f_send = f_send;
8482 ssl->f_recv = f_recv;
8483 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008484}
8485
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008486#if defined(MBEDTLS_SSL_PROTO_DTLS)
8487void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8488{
8489 ssl->mtu = mtu;
8490}
8491#endif
8492
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008493void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008494{
8495 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008496}
8497
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008498void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8499 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008500 mbedtls_ssl_set_timer_t *f_set_timer,
8501 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008502{
8503 ssl->p_timer = p_timer;
8504 ssl->f_set_timer = f_set_timer;
8505 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008506
8507 /* Make sure we start with no timer running */
8508 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008509}
8510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008511#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008512void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008513 void *p_cache,
8514 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8515 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008516{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008517 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008518 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008519 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523#if defined(MBEDTLS_SSL_CLI_C)
8524int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008525{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008526 int ret;
8527
8528 if( ssl == NULL ||
8529 session == NULL ||
8530 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008531 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008534 }
8535
Hanno Becker52055ae2019-02-06 14:30:46 +00008536 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8537 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008538 return( ret );
8539
Paul Bakker0a597072012-09-25 21:55:46 +00008540 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008541
8542 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008545
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008546void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008547 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008549 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8550 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8551 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8552 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008553}
8554
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008555void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008556 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008557 int major, int minor )
8558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008560 return;
8561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008563 return;
8564
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008565 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008566}
8567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008569void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008570 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008571{
8572 conf->cert_profile = profile;
8573}
8574
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008575/* Append a new keycert entry to a (possibly empty) list */
8576static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8577 mbedtls_x509_crt *cert,
8578 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008579{
niisato8ee24222018-06-25 19:05:48 +09008580 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008581
niisato8ee24222018-06-25 19:05:48 +09008582 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8583 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008584 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008585
niisato8ee24222018-06-25 19:05:48 +09008586 new_cert->cert = cert;
8587 new_cert->key = key;
8588 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008589
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008590 /* Update head is the list was null, else add to the end */
8591 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008592 {
niisato8ee24222018-06-25 19:05:48 +09008593 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008594 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008595 else
8596 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008597 mbedtls_ssl_key_cert *cur = *head;
8598 while( cur->next != NULL )
8599 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008600 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008601 }
8602
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008603 return( 0 );
8604}
8605
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008606int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008607 mbedtls_x509_crt *own_cert,
8608 mbedtls_pk_context *pk_key )
8609{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008610 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008611}
8612
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008613void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008614 mbedtls_x509_crt *ca_chain,
8615 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008616{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008617 conf->ca_chain = ca_chain;
8618 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008619
8620#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8621 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8622 * cannot be used together. */
8623 conf->f_ca_cb = NULL;
8624 conf->p_ca_cb = NULL;
8625#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008626}
Hanno Becker5adaad92019-03-27 16:54:37 +00008627
8628#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8629void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008630 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008631 void *p_ca_cb )
8632{
8633 conf->f_ca_cb = f_ca_cb;
8634 conf->p_ca_cb = p_ca_cb;
8635
8636 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8637 * cannot be used together. */
8638 conf->ca_chain = NULL;
8639 conf->ca_crl = NULL;
8640}
8641#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008643
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008644#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8645int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8646 mbedtls_x509_crt *own_cert,
8647 mbedtls_pk_context *pk_key )
8648{
8649 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8650 own_cert, pk_key ) );
8651}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008652
8653void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8654 mbedtls_x509_crt *ca_chain,
8655 mbedtls_x509_crl *ca_crl )
8656{
8657 ssl->handshake->sni_ca_chain = ca_chain;
8658 ssl->handshake->sni_ca_crl = ca_crl;
8659}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008660
8661void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8662 int authmode )
8663{
8664 ssl->handshake->sni_authmode = authmode;
8665}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008666#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8667
Hanno Becker8927c832019-04-03 12:52:50 +01008668#if defined(MBEDTLS_X509_CRT_PARSE_C)
8669void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8670 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8671 void *p_vrfy )
8672{
8673 ssl->f_vrfy = f_vrfy;
8674 ssl->p_vrfy = p_vrfy;
8675}
8676#endif
8677
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008678#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008679/*
8680 * Set EC J-PAKE password for current handshake
8681 */
8682int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8683 const unsigned char *pw,
8684 size_t pw_len )
8685{
8686 mbedtls_ecjpake_role role;
8687
Janos Follath8eb64132016-06-03 15:40:57 +01008688 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008689 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8690
8691 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8692 role = MBEDTLS_ECJPAKE_SERVER;
8693 else
8694 role = MBEDTLS_ECJPAKE_CLIENT;
8695
8696 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8697 role,
8698 MBEDTLS_MD_SHA256,
8699 MBEDTLS_ECP_DP_SECP256R1,
8700 pw, pw_len ) );
8701}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008702#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008705
8706static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8707{
8708 /* Remove reference to existing PSK, if any. */
8709#if defined(MBEDTLS_USE_PSA_CRYPTO)
8710 if( conf->psk_opaque != 0 )
8711 {
8712 /* The maintenance of the PSK key slot is the
8713 * user's responsibility. */
8714 conf->psk_opaque = 0;
8715 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008716 /* This and the following branch should never
8717 * be taken simultaenously as we maintain the
8718 * invariant that raw and opaque PSKs are never
8719 * configured simultaneously. As a safeguard,
8720 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008721#endif /* MBEDTLS_USE_PSA_CRYPTO */
8722 if( conf->psk != NULL )
8723 {
8724 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8725
8726 mbedtls_free( conf->psk );
8727 conf->psk = NULL;
8728 conf->psk_len = 0;
8729 }
8730
8731 /* Remove reference to PSK identity, if any. */
8732 if( conf->psk_identity != NULL )
8733 {
8734 mbedtls_free( conf->psk_identity );
8735 conf->psk_identity = NULL;
8736 conf->psk_identity_len = 0;
8737 }
8738}
8739
Hanno Becker7390c712018-11-15 13:33:04 +00008740/* This function assumes that PSK identity in the SSL config is unset.
8741 * It checks that the provided identity is well-formed and attempts
8742 * to make a copy of it in the SSL config.
8743 * On failure, the PSK identity in the config remains unset. */
8744static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8745 unsigned char const *psk_identity,
8746 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008747{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008748 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008749 if( psk_identity == NULL ||
8750 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008751 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008752 {
8753 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8754 }
8755
Hanno Becker7390c712018-11-15 13:33:04 +00008756 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8757 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008758 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008759
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008760 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008761 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008762
8763 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008764}
8765
Hanno Becker7390c712018-11-15 13:33:04 +00008766int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8767 const unsigned char *psk, size_t psk_len,
8768 const unsigned char *psk_identity, size_t psk_identity_len )
8769{
8770 int ret;
8771 /* Remove opaque/raw PSK + PSK Identity */
8772 ssl_conf_remove_psk( conf );
8773
8774 /* Check and set raw PSK */
8775 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8776 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8777 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8778 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8779 conf->psk_len = psk_len;
8780 memcpy( conf->psk, psk, conf->psk_len );
8781
8782 /* Check and set PSK Identity */
8783 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8784 if( ret != 0 )
8785 ssl_conf_remove_psk( conf );
8786
8787 return( ret );
8788}
8789
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008790static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8791{
8792#if defined(MBEDTLS_USE_PSA_CRYPTO)
8793 if( ssl->handshake->psk_opaque != 0 )
8794 {
8795 ssl->handshake->psk_opaque = 0;
8796 }
8797 else
8798#endif /* MBEDTLS_USE_PSA_CRYPTO */
8799 if( ssl->handshake->psk != NULL )
8800 {
8801 mbedtls_platform_zeroize( ssl->handshake->psk,
8802 ssl->handshake->psk_len );
8803 mbedtls_free( ssl->handshake->psk );
8804 ssl->handshake->psk_len = 0;
8805 }
8806}
8807
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008808int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8809 const unsigned char *psk, size_t psk_len )
8810{
8811 if( psk == NULL || ssl->handshake == NULL )
8812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8813
8814 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8816
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008817 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008818
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008819 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008820 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008821
8822 ssl->handshake->psk_len = psk_len;
8823 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8824
8825 return( 0 );
8826}
8827
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008828#if defined(MBEDTLS_USE_PSA_CRYPTO)
8829int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008830 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008831 const unsigned char *psk_identity,
8832 size_t psk_identity_len )
8833{
Hanno Becker7390c712018-11-15 13:33:04 +00008834 int ret;
8835 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008836 ssl_conf_remove_psk( conf );
8837
Hanno Becker7390c712018-11-15 13:33:04 +00008838 /* Check and set opaque PSK */
8839 if( psk_slot == 0 )
8840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008841 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008842
8843 /* Check and set PSK Identity */
8844 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8845 psk_identity_len );
8846 if( ret != 0 )
8847 ssl_conf_remove_psk( conf );
8848
8849 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008850}
8851
8852int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008853 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008854{
8855 if( psk_slot == 0 || ssl->handshake == NULL )
8856 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8857
8858 ssl_remove_psk( ssl );
8859 ssl->handshake->psk_opaque = psk_slot;
8860 return( 0 );
8861}
8862#endif /* MBEDTLS_USE_PSA_CRYPTO */
8863
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008864void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008866 size_t),
8867 void *p_psk )
8868{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008869 conf->f_psk = f_psk;
8870 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008871}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008873
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008874#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008875
8876#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008877int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008878{
8879 int ret;
8880
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008881 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8882 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8883 {
8884 mbedtls_mpi_free( &conf->dhm_P );
8885 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008886 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008887 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008888
8889 return( 0 );
8890}
Hanno Becker470a8c42017-10-04 15:28:46 +01008891#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008892
Hanno Beckera90658f2017-10-04 15:29:08 +01008893int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8894 const unsigned char *dhm_P, size_t P_len,
8895 const unsigned char *dhm_G, size_t G_len )
8896{
8897 int ret;
8898
8899 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8900 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8901 {
8902 mbedtls_mpi_free( &conf->dhm_P );
8903 mbedtls_mpi_free( &conf->dhm_G );
8904 return( ret );
8905 }
8906
8907 return( 0 );
8908}
Paul Bakker5121ce52009-01-03 21:22:43 +00008909
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008910int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008911{
8912 int ret;
8913
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008914 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8915 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8916 {
8917 mbedtls_mpi_free( &conf->dhm_P );
8918 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008919 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008920 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008921
8922 return( 0 );
8923}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008924#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008925
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008926#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8927/*
8928 * Set the minimum length for Diffie-Hellman parameters
8929 */
8930void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8931 unsigned int bitlen )
8932{
8933 conf->dhm_min_bitlen = bitlen;
8934}
8935#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8936
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008937#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008938/*
8939 * Set allowed/preferred hashes for handshake signatures
8940 */
8941void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8942 const int *hashes )
8943{
8944 conf->sig_hashes = hashes;
8945}
Hanno Becker947194e2017-04-07 13:25:49 +01008946#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008947
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008948#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008949/*
8950 * Set the allowed elliptic curves
8951 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008952void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008953 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008954{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008955 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008956}
Hanno Becker947194e2017-04-07 13:25:49 +01008957#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008958
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008959#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008961{
Hanno Becker947194e2017-04-07 13:25:49 +01008962 /* Initialize to suppress unnecessary compiler warning */
8963 size_t hostname_len = 0;
8964
8965 /* Check if new hostname is valid before
8966 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008967 if( hostname != NULL )
8968 {
8969 hostname_len = strlen( hostname );
8970
8971 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8973 }
8974
8975 /* Now it's clear that we will overwrite the old hostname,
8976 * so we can free it safely */
8977
8978 if( ssl->hostname != NULL )
8979 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008980 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008981 mbedtls_free( ssl->hostname );
8982 }
8983
8984 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008985
Paul Bakker5121ce52009-01-03 21:22:43 +00008986 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008987 {
8988 ssl->hostname = NULL;
8989 }
8990 else
8991 {
8992 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008993 if( ssl->hostname == NULL )
8994 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008995
Hanno Becker947194e2017-04-07 13:25:49 +01008996 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008997
Hanno Becker947194e2017-04-07 13:25:49 +01008998 ssl->hostname[hostname_len] = '\0';
8999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009000
9001 return( 0 );
9002}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009003#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009004
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009005#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009006void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009008 const unsigned char *, size_t),
9009 void *p_sni )
9010{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009011 conf->f_sni = f_sni;
9012 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009017int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009018{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009019 size_t cur_len, tot_len;
9020 const char **p;
9021
9022 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009023 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9024 * MUST NOT be truncated."
9025 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009026 */
9027 tot_len = 0;
9028 for( p = protos; *p != NULL; p++ )
9029 {
9030 cur_len = strlen( *p );
9031 tot_len += cur_len;
9032
9033 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009035 }
9036
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009037 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009038
9039 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009040}
9041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009042const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009043{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009044 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009047
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009048void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009049{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009050 conf->max_major_ver = major;
9051 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009052}
9053
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009054void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009055{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009056 conf->min_major_ver = major;
9057 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009058}
9059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009061void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009062{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009063 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009064}
9065#endif
9066
Janos Follath088ce432017-04-10 12:42:31 +01009067#if defined(MBEDTLS_SSL_SRV_C)
9068void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9069 char cert_req_ca_list )
9070{
9071 conf->cert_req_ca_list = cert_req_ca_list;
9072}
9073#endif
9074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009076void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009077{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009078 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009079}
9080#endif
9081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009083void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009084{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009085 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009086}
9087#endif
9088
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009089#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009090void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009091{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009092 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009093}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009094#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009097int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009100 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009103 }
9104
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009105 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009106
9107 return( 0 );
9108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009112void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009114 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009119void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009120{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009121 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009122}
9123#endif
9124
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009125void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009127 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009128}
9129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009130#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009131void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009132{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009133 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009134}
9135
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009136void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009138 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009139}
9140
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009141void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009142 const unsigned char period[8] )
9143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009144 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009146#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009149#if defined(MBEDTLS_SSL_CLI_C)
9150void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009151{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009152 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009153}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009154#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009155
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009156#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009157void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9158 mbedtls_ssl_ticket_write_t *f_ticket_write,
9159 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9160 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009161{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009162 conf->f_ticket_write = f_ticket_write;
9163 conf->f_ticket_parse = f_ticket_parse;
9164 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009165}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009167#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009168
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009169#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9170void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9171 mbedtls_ssl_export_keys_t *f_export_keys,
9172 void *p_export_keys )
9173{
9174 conf->f_export_keys = f_export_keys;
9175 conf->p_export_keys = p_export_keys;
9176}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009177
9178void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9179 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9180 void *p_export_keys )
9181{
9182 conf->f_export_keys_ext = f_export_keys_ext;
9183 conf->p_export_keys = p_export_keys;
9184}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009185#endif
9186
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009187#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009188void mbedtls_ssl_conf_async_private_cb(
9189 mbedtls_ssl_config *conf,
9190 mbedtls_ssl_async_sign_t *f_async_sign,
9191 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9192 mbedtls_ssl_async_resume_t *f_async_resume,
9193 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009194 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009195{
9196 conf->f_async_sign_start = f_async_sign;
9197 conf->f_async_decrypt_start = f_async_decrypt;
9198 conf->f_async_resume = f_async_resume;
9199 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009200 conf->p_async_config_data = async_config_data;
9201}
9202
Gilles Peskine8f97af72018-04-26 11:46:10 +02009203void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9204{
9205 return( conf->p_async_config_data );
9206}
9207
Gilles Peskine1febfef2018-04-30 11:54:39 +02009208void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009209{
9210 if( ssl->handshake == NULL )
9211 return( NULL );
9212 else
9213 return( ssl->handshake->user_async_ctx );
9214}
9215
Gilles Peskine1febfef2018-04-30 11:54:39 +02009216void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009217 void *ctx )
9218{
9219 if( ssl->handshake != NULL )
9220 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009221}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009222#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009223
Paul Bakker5121ce52009-01-03 21:22:43 +00009224/*
9225 * SSL get accessors
9226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009227size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009228{
9229 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9230}
9231
Hanno Becker8b170a02017-10-10 11:51:19 +01009232int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9233{
9234 /*
9235 * Case A: We're currently holding back
9236 * a message for further processing.
9237 */
9238
9239 if( ssl->keep_current_message == 1 )
9240 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009242 return( 1 );
9243 }
9244
9245 /*
9246 * Case B: Further records are pending in the current datagram.
9247 */
9248
9249#if defined(MBEDTLS_SSL_PROTO_DTLS)
9250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9251 ssl->in_left > ssl->next_record_offset )
9252 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009254 return( 1 );
9255 }
9256#endif /* MBEDTLS_SSL_PROTO_DTLS */
9257
9258 /*
9259 * Case C: A handshake message is being processed.
9260 */
9261
Hanno Becker8b170a02017-10-10 11:51:19 +01009262 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9263 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009265 return( 1 );
9266 }
9267
9268 /*
9269 * Case D: An application data message is being processed
9270 */
9271 if( ssl->in_offt != NULL )
9272 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009274 return( 1 );
9275 }
9276
9277 /*
9278 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009279 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009280 * we implement support for multiple alerts in single records.
9281 */
9282
9283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9284 return( 0 );
9285}
9286
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009287uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009288{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009289 if( ssl->session != NULL )
9290 return( ssl->session->verify_result );
9291
9292 if( ssl->session_negotiate != NULL )
9293 return( ssl->session_negotiate->verify_result );
9294
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009295 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009296}
9297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009299{
Paul Bakker926c8e42013-03-06 10:23:34 +01009300 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009301 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009304}
9305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009310 {
9311 switch( ssl->minor_ver )
9312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009314 return( "DTLSv1.0" );
9315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009316 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009317 return( "DTLSv1.2" );
9318
9319 default:
9320 return( "unknown (DTLS)" );
9321 }
9322 }
9323#endif
9324
Paul Bakker43ca69c2011-01-15 17:35:19 +00009325 switch( ssl->minor_ver )
9326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009328 return( "SSLv3.0" );
9329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009331 return( "TLSv1.0" );
9332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009334 return( "TLSv1.1" );
9335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009337 return( "TLSv1.2" );
9338
Paul Bakker43ca69c2011-01-15 17:35:19 +00009339 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009340 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009341 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009342}
9343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009345{
Hanno Becker3136ede2018-08-17 15:28:19 +01009346 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009347 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009348 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009349
Hanno Becker5903de42019-05-03 14:46:38 +01009350 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9351
Hanno Becker78640902018-08-13 16:35:15 +01009352 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009353 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355#if defined(MBEDTLS_ZLIB_SUPPORT)
9356 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9357 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009358#endif
9359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362 case MBEDTLS_MODE_GCM:
9363 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009364 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009366 transform_expansion = transform->minlen;
9367 break;
9368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009370
9371 block_size = mbedtls_cipher_get_block_size(
9372 &transform->cipher_ctx_enc );
9373
Hanno Becker3136ede2018-08-17 15:28:19 +01009374 /* Expansion due to the addition of the MAC. */
9375 transform_expansion += transform->maclen;
9376
9377 /* Expansion due to the addition of CBC padding;
9378 * Theoretically up to 256 bytes, but we never use
9379 * more than the block size of the underlying cipher. */
9380 transform_expansion += block_size;
9381
9382 /* For TLS 1.1 or higher, an explicit IV is added
9383 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009384#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9385 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009386 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009387#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009388
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009389 break;
9390
9391 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009394 }
9395
Hanno Beckera0e20d02019-05-15 14:03:01 +01009396#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009397 if( transform->out_cid_len != 0 )
9398 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009399#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009400
Hanno Becker5903de42019-05-03 14:46:38 +01009401 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009402}
9403
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009404#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9405size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9406{
9407 size_t max_len;
9408
9409 /*
9410 * Assume mfl_code is correct since it was checked when set
9411 */
Angus Grattond8213d02016-05-25 20:56:48 +10009412 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009413
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009414 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009415 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009416 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009417 {
Angus Grattond8213d02016-05-25 20:56:48 +10009418 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009419 }
9420
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009421 /* During a handshake, use the value being negotiated */
9422 if( ssl->session_negotiate != NULL &&
9423 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9424 {
9425 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9426 }
9427
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009428 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009429}
9430#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9431
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009432#if defined(MBEDTLS_SSL_PROTO_DTLS)
9433static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9434{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009435 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9436 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9437 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9438 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9439 return ( 0 );
9440
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009441 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9442 return( ssl->mtu );
9443
9444 if( ssl->mtu == 0 )
9445 return( ssl->handshake->mtu );
9446
9447 return( ssl->mtu < ssl->handshake->mtu ?
9448 ssl->mtu : ssl->handshake->mtu );
9449}
9450#endif /* MBEDTLS_SSL_PROTO_DTLS */
9451
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009452int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9453{
9454 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9455
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009456#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9457 !defined(MBEDTLS_SSL_PROTO_DTLS)
9458 (void) ssl;
9459#endif
9460
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009461#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9462 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9463
9464 if( max_len > mfl )
9465 max_len = mfl;
9466#endif
9467
9468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009469 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009470 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009471 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009472 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9473 const size_t overhead = (size_t) ret;
9474
9475 if( ret < 0 )
9476 return( ret );
9477
9478 if( mtu <= overhead )
9479 {
9480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9481 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9482 }
9483
9484 if( max_len > mtu - overhead )
9485 max_len = mtu - overhead;
9486 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009487#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009488
Hanno Becker0defedb2018-08-10 12:35:02 +01009489#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9490 !defined(MBEDTLS_SSL_PROTO_DTLS)
9491 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009492#endif
9493
9494 return( (int) max_len );
9495}
9496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497#if defined(MBEDTLS_X509_CRT_PARSE_C)
9498const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009499{
9500 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009501 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009502
Hanno Beckere6824572019-02-07 13:18:46 +00009503#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009504 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009505#else
9506 return( NULL );
9507#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009512int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9513 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009514{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009515 if( ssl == NULL ||
9516 dst == NULL ||
9517 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009518 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009521 }
9522
Hanno Becker52055ae2019-02-06 14:30:46 +00009523 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009526
Paul Bakker5121ce52009-01-03 21:22:43 +00009527/*
Paul Bakker1961b702013-01-25 14:49:24 +01009528 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009533
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009534 if( ssl == NULL || ssl->conf == NULL )
9535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009538 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009540#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009542 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009544#endif
9545
Paul Bakker1961b702013-01-25 14:49:24 +01009546 return( ret );
9547}
9548
9549/*
9550 * Perform the SSL handshake
9551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009553{
9554 int ret = 0;
9555
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009556 if( ssl == NULL || ssl->conf == NULL )
9557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009564
9565 if( ret != 0 )
9566 break;
9567 }
9568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009570
9571 return( ret );
9572}
9573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574#if defined(MBEDTLS_SSL_RENEGOTIATION)
9575#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009576/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009577 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009580{
9581 int ret;
9582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009584
9585 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9587 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009588
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009589 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009590 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009591 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009592 return( ret );
9593 }
9594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009596
9597 return( 0 );
9598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009600
9601/*
9602 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 * - any side: calling mbedtls_ssl_renegotiate(),
9604 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9605 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009606 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009607 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009611{
9612 int ret;
9613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009615
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009616 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9617 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009618
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009619 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9620 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009622 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009624 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009625 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009626 ssl->handshake->out_msg_seq = 1;
9627 else
9628 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009629 }
9630#endif
9631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9633 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009638 return( ret );
9639 }
9640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009642
9643 return( 0 );
9644}
9645
9646/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009647 * Renegotiate current connection on client,
9648 * or request renegotiation on server
9649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009651{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009653
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009654 if( ssl == NULL || ssl->conf == NULL )
9655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009658 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009659 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009665
9666 /* Did we already try/start sending HelloRequest? */
9667 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009669
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009670 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009675 /*
9676 * On client, either start the renegotiation process or,
9677 * if already in progress, continue the handshake
9678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009683
9684 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009687 return( ret );
9688 }
9689 }
9690 else
9691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009695 return( ret );
9696 }
9697 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009699
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009700 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009701}
9702
9703/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009704 * Check record counters and renegotiate if they're above the limit.
9705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009707{
Andres AG2196c7f2016-12-15 17:01:16 +00009708 size_t ep_len = ssl_ep_len( ssl );
9709 int in_ctr_cmp;
9710 int out_ctr_cmp;
9711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9713 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009714 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009715 {
9716 return( 0 );
9717 }
9718
Andres AG2196c7f2016-12-15 17:01:16 +00009719 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9720 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009721 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009722 ssl->conf->renego_period + ep_len, 8 - ep_len );
9723
9724 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009725 {
9726 return( 0 );
9727 }
9728
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009731}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009733
9734/*
9735 * Receive application data decrypted from the SSL layer
9736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009738{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009739 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009740 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009741
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009742 if( ssl == NULL || ssl->conf == NULL )
9743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009748 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009751 return( ret );
9752
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009753 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009755 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009756 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009757 return( ret );
9758 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009759 }
9760#endif
9761
Hanno Becker4a810fb2017-05-24 16:27:30 +01009762 /*
9763 * Check if renegotiation is necessary and/or handshake is
9764 * in process. If yes, perform/continue, and fall through
9765 * if an unexpected packet is received while the client
9766 * is waiting for the ServerHello.
9767 *
9768 * (There is no equivalent to the last condition on
9769 * the server-side as it is not treated as within
9770 * a handshake while waiting for the ClientHello
9771 * after a renegotiation request.)
9772 */
9773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009775 ret = ssl_check_ctr_renegotiate( ssl );
9776 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9777 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009780 return( ret );
9781 }
9782#endif
9783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009787 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9788 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009791 return( ret );
9792 }
9793 }
9794
Hanno Beckere41158b2017-10-23 13:30:32 +01009795 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009796 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009797 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009798 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009799 if( ssl->f_get_timer != NULL &&
9800 ssl->f_get_timer( ssl->p_timer ) == -1 )
9801 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009802 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009803 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009804
Hanno Becker327c93b2018-08-15 13:56:18 +01009805 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009806 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009807 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9808 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009809
Hanno Becker4a810fb2017-05-24 16:27:30 +01009810 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9811 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009812 }
9813
9814 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009816 {
9817 /*
9818 * OpenSSL sends empty messages to randomize the IV
9819 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009820 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009823 return( 0 );
9824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009826 return( ret );
9827 }
9828 }
9829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009833
Hanno Becker4a810fb2017-05-24 16:27:30 +01009834 /*
9835 * - For client-side, expect SERVER_HELLO_REQUEST.
9836 * - For server-side, expect CLIENT_HELLO.
9837 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9838 */
9839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009841 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009843 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009846
9847 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009849 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009850 {
9851 continue;
9852 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009855 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009856#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009857
Hanno Becker4a810fb2017-05-24 16:27:30 +01009858#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009859 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009863
9864 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009866 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009867 {
9868 continue;
9869 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009872 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009873#endif /* MBEDTLS_SSL_SRV_C */
9874
Hanno Becker21df7f92017-10-17 11:03:26 +01009875#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009876 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009877 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9878 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9879 ssl->conf->allow_legacy_renegotiation ==
9880 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9881 {
9882 /*
9883 * Accept renegotiation request
9884 */
Paul Bakker48916f92012-09-16 19:57:18 +00009885
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009886 /* DTLS clients need to know renego is server-initiated */
9887#if defined(MBEDTLS_SSL_PROTO_DTLS)
9888 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9889 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9890 {
9891 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9892 }
9893#endif
9894 ret = ssl_start_renegotiation( ssl );
9895 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9896 ret != 0 )
9897 {
9898 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9899 return( ret );
9900 }
9901 }
9902 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009903#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009904 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009905 /*
9906 * Refuse renegotiation
9907 */
9908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911#if defined(MBEDTLS_SSL_PROTO_SSL3)
9912 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009913 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009914 /* SSLv3 does not have a "no_renegotiation" warning, so
9915 we send a fatal alert and abort the connection. */
9916 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9917 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9918 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009919 }
9920 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9922#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9923 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9924 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009926 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9927 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9928 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009929 {
9930 return( ret );
9931 }
Paul Bakker48916f92012-09-16 19:57:18 +00009932 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009933 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9935 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9938 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009939 }
Paul Bakker48916f92012-09-16 19:57:18 +00009940 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009941
Hanno Becker90333da2017-10-10 11:27:13 +01009942 /* At this point, we don't know whether the renegotiation has been
9943 * completed or not. The cases to consider are the following:
9944 * 1) The renegotiation is complete. In this case, no new record
9945 * has been read yet.
9946 * 2) The renegotiation is incomplete because the client received
9947 * an application data record while awaiting the ServerHello.
9948 * 3) The renegotiation is incomplete because the client received
9949 * a non-handshake, non-application data message while awaiting
9950 * the ServerHello.
9951 * In each of these case, looping will be the proper action:
9952 * - For 1), the next iteration will read a new record and check
9953 * if it's application data.
9954 * - For 2), the loop condition isn't satisfied as application data
9955 * is present, hence continue is the same as break
9956 * - For 3), the loop condition is satisfied and read_record
9957 * will re-deliver the message that was held back by the client
9958 * when expecting the ServerHello.
9959 */
9960 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009961 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009962#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009964 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009965 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009966 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009967 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009970 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009972 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009973 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009974 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9978 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009981 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009982 }
9983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9987 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009988 }
9989
9990 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009991
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009992 /* We're going to return something now, cancel timer,
9993 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009995 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009996
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009997#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009998 /* If we requested renego but received AppData, resend HelloRequest.
9999 * Do it now, after setting in_offt, to avoid taking this branch
10000 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010004 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010005 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010008 return( ret );
10009 }
10010 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010012#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010013 }
10014
10015 n = ( len < ssl->in_msglen )
10016 ? len : ssl->in_msglen;
10017
10018 memcpy( buf, ssl->in_offt, n );
10019 ssl->in_msglen -= n;
10020
10021 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010022 {
10023 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010025 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010026 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010027 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010028 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010029 /* more data available */
10030 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010031 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010034
Paul Bakker23986e52011-04-24 08:57:21 +000010035 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010036}
10037
10038/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010039 * Send application data to be encrypted by the SSL layer, taking care of max
10040 * fragment length and buffer size.
10041 *
10042 * According to RFC 5246 Section 6.2.1:
10043 *
10044 * Zero-length fragments of Application data MAY be sent as they are
10045 * potentially useful as a traffic analysis countermeasure.
10046 *
10047 * Therefore, it is possible that the input message length is 0 and the
10048 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010049 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010050static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010051 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010052{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010053 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10054 const size_t max_len = (size_t) ret;
10055
10056 if( ret < 0 )
10057 {
10058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10059 return( ret );
10060 }
10061
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010062 if( len > max_len )
10063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010065 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010068 "maximum fragment length: %d > %d",
10069 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010071 }
10072 else
10073#endif
10074 len = max_len;
10075 }
Paul Bakker887bd502011-06-08 13:10:54 +000010076
Paul Bakker5121ce52009-01-03 21:22:43 +000010077 if( ssl->out_left != 0 )
10078 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010079 /*
10080 * The user has previously tried to send the data and
10081 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10082 * written. In this case, we expect the high-level write function
10083 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010085 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010088 return( ret );
10089 }
10090 }
Paul Bakker887bd502011-06-08 13:10:54 +000010091 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010092 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010093 /*
10094 * The user is trying to send a message the first time, so we need to
10095 * copy the data into the internal buffers and setup the data structure
10096 * to keep track of partial writes
10097 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010098 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010100 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010101
Hanno Becker67bc7c32018-08-06 11:33:50 +010010102 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010105 return( ret );
10106 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010107 }
10108
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010109 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010110}
10111
10112/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010113 * Write application data, doing 1/n-1 splitting if necessary.
10114 *
10115 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010116 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010117 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010120static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010121 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010122{
10123 int ret;
10124
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010125 if( ssl->conf->cbc_record_splitting ==
10126 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010127 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10129 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10130 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010131 {
10132 return( ssl_write_real( ssl, buf, len ) );
10133 }
10134
10135 if( ssl->split_done == 0 )
10136 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010137 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010138 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010139 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010140 }
10141
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010142 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10143 return( ret );
10144 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010145
10146 return( ret + 1 );
10147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010149
10150/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010151 * Write application data (public-facing wrapper)
10152 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010153int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010154{
10155 int ret;
10156
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010158
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010159 if( ssl == NULL || ssl->conf == NULL )
10160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10161
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010162#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010163 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10164 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010165 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010166 return( ret );
10167 }
10168#endif
10169
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010170 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010171 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010172 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010173 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010175 return( ret );
10176 }
10177 }
10178
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010179#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010180 ret = ssl_write_split( ssl, buf, len );
10181#else
10182 ret = ssl_write_real( ssl, buf, len );
10183#endif
10184
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010186
10187 return( ret );
10188}
10189
10190/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010191 * Notify the peer that the connection is being closed
10192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010194{
10195 int ret;
10196
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010197 if( ssl == NULL || ssl->conf == NULL )
10198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010201
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010202 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10208 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10209 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010212 return( ret );
10213 }
10214 }
10215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010217
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010218 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010219}
10220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010222{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010223 if( transform == NULL )
10224 return;
10225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010227 deflateEnd( &transform->ctx_deflate );
10228 inflateEnd( &transform->ctx_inflate );
10229#endif
10230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10232 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010233
Hanno Beckerd56ed242018-01-03 15:32:51 +000010234#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235 mbedtls_md_free( &transform->md_ctx_enc );
10236 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010237#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010238
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010239 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010240}
10241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242#if defined(MBEDTLS_X509_CRT_PARSE_C)
10243static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010246
10247 while( cur != NULL )
10248 {
10249 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010251 cur = next;
10252 }
10253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010254#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010255
Hanno Becker0271f962018-08-16 13:23:47 +010010256#if defined(MBEDTLS_SSL_PROTO_DTLS)
10257
10258static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10259{
10260 unsigned offset;
10261 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10262
10263 if( hs == NULL )
10264 return;
10265
Hanno Becker283f5ef2018-08-24 09:34:47 +010010266 ssl_free_buffered_record( ssl );
10267
Hanno Becker0271f962018-08-16 13:23:47 +010010268 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010269 ssl_buffering_free_slot( ssl, offset );
10270}
10271
10272static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10273 uint8_t slot )
10274{
10275 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10276 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010277
10278 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10279 return;
10280
Hanno Beckere605b192018-08-21 15:59:07 +010010281 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010282 {
Hanno Beckere605b192018-08-21 15:59:07 +010010283 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010284 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010285 mbedtls_free( hs_buf->data );
10286 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010287 }
10288}
10289
10290#endif /* MBEDTLS_SSL_PROTO_DTLS */
10291
Gilles Peskine9b562d52018-04-25 20:32:43 +020010292void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010293{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010294 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10295
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010296 if( handshake == NULL )
10297 return;
10298
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010299#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10300 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10301 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010302 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010303 handshake->async_in_progress = 0;
10304 }
10305#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10306
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010307#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10308 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10309 mbedtls_md5_free( &handshake->fin_md5 );
10310 mbedtls_sha1_free( &handshake->fin_sha1 );
10311#endif
10312#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10313#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010314#if defined(MBEDTLS_USE_PSA_CRYPTO)
10315 psa_hash_abort( &handshake->fin_sha256_psa );
10316#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010317 mbedtls_sha256_free( &handshake->fin_sha256 );
10318#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010319#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010320#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010321#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010322 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010323#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010324 mbedtls_sha512_free( &handshake->fin_sha512 );
10325#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010326#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010327#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329#if defined(MBEDTLS_DHM_C)
10330 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010331#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332#if defined(MBEDTLS_ECDH_C)
10333 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010334#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010335#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010336 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010337#if defined(MBEDTLS_SSL_CLI_C)
10338 mbedtls_free( handshake->ecjpake_cache );
10339 handshake->ecjpake_cache = NULL;
10340 handshake->ecjpake_cache_len = 0;
10341#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010342#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010343
Janos Follath4ae5c292016-02-10 11:27:43 +000010344#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10345 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010346 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010348#endif
10349
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010350#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10351 if( handshake->psk != NULL )
10352 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010353 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010354 mbedtls_free( handshake->psk );
10355 }
10356#endif
10357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10359 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010360 /*
10361 * Free only the linked list wrapper, not the keys themselves
10362 * since the belong to the SNI callback
10363 */
10364 if( handshake->sni_key_cert != NULL )
10365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010367
10368 while( cur != NULL )
10369 {
10370 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010372 cur = next;
10373 }
10374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010376
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010377#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010378 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010379 if( handshake->ecrs_peer_cert != NULL )
10380 {
10381 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10382 mbedtls_free( handshake->ecrs_peer_cert );
10383 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010384#endif
10385
Hanno Becker75173122019-02-06 16:18:31 +000010386#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10387 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10388 mbedtls_pk_free( &handshake->peer_pubkey );
10389#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391#if defined(MBEDTLS_SSL_PROTO_DTLS)
10392 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010393 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010394 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010395#endif
10396
Hanno Becker4a63ed42019-01-08 11:39:35 +000010397#if defined(MBEDTLS_ECDH_C) && \
10398 defined(MBEDTLS_USE_PSA_CRYPTO)
10399 psa_destroy_key( handshake->ecdh_psa_privkey );
10400#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10401
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010402 mbedtls_platform_zeroize( handshake,
10403 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010404}
10405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010407{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010408 if( session == NULL )
10409 return;
10410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010412 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010413#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010415#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010417#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010418
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010419 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010420}
10421
Paul Bakker5121ce52009-01-03 21:22:43 +000010422/*
10423 * Free an SSL context
10424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010426{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010427 if( ssl == NULL )
10428 return;
10429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010431
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010432 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010433 {
Angus Grattond8213d02016-05-25 20:56:48 +100010434 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010436 }
10437
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010438 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010439 {
Angus Grattond8213d02016-05-25 20:56:48 +100010440 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010442 }
10443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010445 if( ssl->compress_buf != NULL )
10446 {
Angus Grattond8213d02016-05-25 20:56:48 +100010447 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010449 }
10450#endif
10451
Paul Bakker48916f92012-09-16 19:57:18 +000010452 if( ssl->transform )
10453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454 mbedtls_ssl_transform_free( ssl->transform );
10455 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010456 }
10457
10458 if( ssl->handshake )
10459 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010460 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10462 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464 mbedtls_free( ssl->handshake );
10465 mbedtls_free( ssl->transform_negotiate );
10466 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010467 }
10468
Paul Bakkerc0463502013-02-14 11:19:38 +010010469 if( ssl->session )
10470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471 mbedtls_ssl_session_free( ssl->session );
10472 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010473 }
10474
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010475#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010476 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010477 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010478 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010480 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010481#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10484 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10487 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010488 }
10489#endif
10490
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010491#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010493#endif
10494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010496
Paul Bakker86f04f42013-02-14 11:20:09 +010010497 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010498 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010499}
10500
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010501/*
10502 * Initialze mbedtls_ssl_config
10503 */
10504void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10505{
10506 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10507}
10508
Simon Butcherc97b6972015-12-27 23:48:17 +000010509#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010510static int ssl_preset_default_hashes[] = {
10511#if defined(MBEDTLS_SHA512_C)
10512 MBEDTLS_MD_SHA512,
10513 MBEDTLS_MD_SHA384,
10514#endif
10515#if defined(MBEDTLS_SHA256_C)
10516 MBEDTLS_MD_SHA256,
10517 MBEDTLS_MD_SHA224,
10518#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010519#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010520 MBEDTLS_MD_SHA1,
10521#endif
10522 MBEDTLS_MD_NONE
10523};
Simon Butcherc97b6972015-12-27 23:48:17 +000010524#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010525
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010526static int ssl_preset_suiteb_ciphersuites[] = {
10527 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10528 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10529 0
10530};
10531
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010532#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010533static int ssl_preset_suiteb_hashes[] = {
10534 MBEDTLS_MD_SHA256,
10535 MBEDTLS_MD_SHA384,
10536 MBEDTLS_MD_NONE
10537};
10538#endif
10539
10540#if defined(MBEDTLS_ECP_C)
10541static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010542#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010543 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010544#endif
10545#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010546 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010547#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010548 MBEDTLS_ECP_DP_NONE
10549};
10550#endif
10551
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010552/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010553 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010554 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010555int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010556 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010557{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010558#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010559 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010560#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010561
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010562 /* Use the functions here so that they are covered in tests,
10563 * but otherwise access member directly for efficiency */
10564 mbedtls_ssl_conf_endpoint( conf, endpoint );
10565 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010566
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010567 /*
10568 * Things that are common to all presets
10569 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010570#if defined(MBEDTLS_SSL_CLI_C)
10571 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10572 {
10573 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10574#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10575 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10576#endif
10577 }
10578#endif
10579
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010580#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010581 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010582#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010583
10584#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10585 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10586#endif
10587
10588#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10589 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10590#endif
10591
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010592#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10593 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10594#endif
10595
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010596#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010597 conf->f_cookie_write = ssl_cookie_write_dummy;
10598 conf->f_cookie_check = ssl_cookie_check_dummy;
10599#endif
10600
10601#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10602 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10603#endif
10604
Janos Follath088ce432017-04-10 12:42:31 +010010605#if defined(MBEDTLS_SSL_SRV_C)
10606 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10607#endif
10608
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010609#if defined(MBEDTLS_SSL_PROTO_DTLS)
10610 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10611 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10612#endif
10613
10614#if defined(MBEDTLS_SSL_RENEGOTIATION)
10615 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010616 memset( conf->renego_period, 0x00, 2 );
10617 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010618#endif
10619
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010620#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10621 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10622 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010623 const unsigned char dhm_p[] =
10624 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10625 const unsigned char dhm_g[] =
10626 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10627
Hanno Beckera90658f2017-10-04 15:29:08 +010010628 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10629 dhm_p, sizeof( dhm_p ),
10630 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010631 {
10632 return( ret );
10633 }
10634 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010635#endif
10636
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010637 /*
10638 * Preset-specific defaults
10639 */
10640 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010641 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010642 /*
10643 * NSA Suite B
10644 */
10645 case MBEDTLS_SSL_PRESET_SUITEB:
10646 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10647 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10648 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10649 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10650
10651 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10652 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10653 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10654 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10655 ssl_preset_suiteb_ciphersuites;
10656
10657#if defined(MBEDTLS_X509_CRT_PARSE_C)
10658 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010659#endif
10660
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010661#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010662 conf->sig_hashes = ssl_preset_suiteb_hashes;
10663#endif
10664
10665#if defined(MBEDTLS_ECP_C)
10666 conf->curve_list = ssl_preset_suiteb_curves;
10667#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010668 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010669
10670 /*
10671 * Default
10672 */
10673 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010674 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10675 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10676 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10677 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10678 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10679 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10680 MBEDTLS_SSL_MIN_MINOR_VERSION :
10681 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010682 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10683 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10684
10685#if defined(MBEDTLS_SSL_PROTO_DTLS)
10686 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10687 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10688#endif
10689
10690 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10691 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10692 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10693 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10694 mbedtls_ssl_list_ciphersuites();
10695
10696#if defined(MBEDTLS_X509_CRT_PARSE_C)
10697 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10698#endif
10699
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010700#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010701 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010702#endif
10703
10704#if defined(MBEDTLS_ECP_C)
10705 conf->curve_list = mbedtls_ecp_grp_id_list();
10706#endif
10707
10708#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10709 conf->dhm_min_bitlen = 1024;
10710#endif
10711 }
10712
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010713 return( 0 );
10714}
10715
10716/*
10717 * Free mbedtls_ssl_config
10718 */
10719void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10720{
10721#if defined(MBEDTLS_DHM_C)
10722 mbedtls_mpi_free( &conf->dhm_P );
10723 mbedtls_mpi_free( &conf->dhm_G );
10724#endif
10725
10726#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10727 if( conf->psk != NULL )
10728 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010729 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010730 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010731 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010732 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010733 }
10734
10735 if( conf->psk_identity != NULL )
10736 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010737 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010738 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010739 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010740 conf->psk_identity_len = 0;
10741 }
10742#endif
10743
10744#if defined(MBEDTLS_X509_CRT_PARSE_C)
10745 ssl_key_cert_free( conf->key_cert );
10746#endif
10747
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010748 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010749}
10750
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010751#if defined(MBEDTLS_PK_C) && \
10752 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010753/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010757{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758#if defined(MBEDTLS_RSA_C)
10759 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10760 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762#if defined(MBEDTLS_ECDSA_C)
10763 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10764 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010765#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010766 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010767}
10768
Hanno Becker7e5437a2017-04-28 17:15:26 +010010769unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10770{
10771 switch( type ) {
10772 case MBEDTLS_PK_RSA:
10773 return( MBEDTLS_SSL_SIG_RSA );
10774 case MBEDTLS_PK_ECDSA:
10775 case MBEDTLS_PK_ECKEY:
10776 return( MBEDTLS_SSL_SIG_ECDSA );
10777 default:
10778 return( MBEDTLS_SSL_SIG_ANON );
10779 }
10780}
10781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010782mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010783{
10784 switch( sig )
10785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786#if defined(MBEDTLS_RSA_C)
10787 case MBEDTLS_SSL_SIG_RSA:
10788 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790#if defined(MBEDTLS_ECDSA_C)
10791 case MBEDTLS_SSL_SIG_ECDSA:
10792 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010793#endif
10794 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010796 }
10797}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010798#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010799
Hanno Becker7e5437a2017-04-28 17:15:26 +010010800#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10801 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10802
10803/* Find an entry in a signature-hash set matching a given hash algorithm. */
10804mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10805 mbedtls_pk_type_t sig_alg )
10806{
10807 switch( sig_alg )
10808 {
10809 case MBEDTLS_PK_RSA:
10810 return( set->rsa );
10811 case MBEDTLS_PK_ECDSA:
10812 return( set->ecdsa );
10813 default:
10814 return( MBEDTLS_MD_NONE );
10815 }
10816}
10817
10818/* Add a signature-hash-pair to a signature-hash set */
10819void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10820 mbedtls_pk_type_t sig_alg,
10821 mbedtls_md_type_t md_alg )
10822{
10823 switch( sig_alg )
10824 {
10825 case MBEDTLS_PK_RSA:
10826 if( set->rsa == MBEDTLS_MD_NONE )
10827 set->rsa = md_alg;
10828 break;
10829
10830 case MBEDTLS_PK_ECDSA:
10831 if( set->ecdsa == MBEDTLS_MD_NONE )
10832 set->ecdsa = md_alg;
10833 break;
10834
10835 default:
10836 break;
10837 }
10838}
10839
10840/* Allow exactly one hash algorithm for each signature. */
10841void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10842 mbedtls_md_type_t md_alg )
10843{
10844 set->rsa = md_alg;
10845 set->ecdsa = md_alg;
10846}
10847
10848#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10849 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10850
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010851/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010852 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010855{
10856 switch( hash )
10857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010858#if defined(MBEDTLS_MD5_C)
10859 case MBEDTLS_SSL_HASH_MD5:
10860 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010861#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010862#if defined(MBEDTLS_SHA1_C)
10863 case MBEDTLS_SSL_HASH_SHA1:
10864 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010865#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010866#if defined(MBEDTLS_SHA256_C)
10867 case MBEDTLS_SSL_HASH_SHA224:
10868 return( MBEDTLS_MD_SHA224 );
10869 case MBEDTLS_SSL_HASH_SHA256:
10870 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872#if defined(MBEDTLS_SHA512_C)
10873 case MBEDTLS_SSL_HASH_SHA384:
10874 return( MBEDTLS_MD_SHA384 );
10875 case MBEDTLS_SSL_HASH_SHA512:
10876 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010877#endif
10878 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010879 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010880 }
10881}
10882
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010883/*
10884 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10885 */
10886unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10887{
10888 switch( md )
10889 {
10890#if defined(MBEDTLS_MD5_C)
10891 case MBEDTLS_MD_MD5:
10892 return( MBEDTLS_SSL_HASH_MD5 );
10893#endif
10894#if defined(MBEDTLS_SHA1_C)
10895 case MBEDTLS_MD_SHA1:
10896 return( MBEDTLS_SSL_HASH_SHA1 );
10897#endif
10898#if defined(MBEDTLS_SHA256_C)
10899 case MBEDTLS_MD_SHA224:
10900 return( MBEDTLS_SSL_HASH_SHA224 );
10901 case MBEDTLS_MD_SHA256:
10902 return( MBEDTLS_SSL_HASH_SHA256 );
10903#endif
10904#if defined(MBEDTLS_SHA512_C)
10905 case MBEDTLS_MD_SHA384:
10906 return( MBEDTLS_SSL_HASH_SHA384 );
10907 case MBEDTLS_MD_SHA512:
10908 return( MBEDTLS_SSL_HASH_SHA512 );
10909#endif
10910 default:
10911 return( MBEDTLS_SSL_HASH_NONE );
10912 }
10913}
10914
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010915#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010916/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010917 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010918 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010919 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010920int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010923
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010924 if( ssl->conf->curve_list == NULL )
10925 return( -1 );
10926
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010927 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010928 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010929 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010930
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010931 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010932}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010933#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010934
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010935#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010936/*
10937 * Check if a hash proposed by the peer is in our list.
10938 * Return 0 if we're willing to use it, -1 otherwise.
10939 */
10940int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10941 mbedtls_md_type_t md )
10942{
10943 const int *cur;
10944
10945 if( ssl->conf->sig_hashes == NULL )
10946 return( -1 );
10947
10948 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10949 if( *cur == (int) md )
10950 return( 0 );
10951
10952 return( -1 );
10953}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010954#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956#if defined(MBEDTLS_X509_CRT_PARSE_C)
10957int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10958 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010959 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010960 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010961{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010962 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010963#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010964 int usage = 0;
10965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010967 const char *ext_oid;
10968 size_t ext_len;
10969#endif
10970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010971#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10972 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010973 ((void) cert);
10974 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010975 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010976#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010978#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10979 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010980 {
10981 /* Server part of the key exchange */
10982 switch( ciphersuite->key_exchange )
10983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010984 case MBEDTLS_KEY_EXCHANGE_RSA:
10985 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010986 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010987 break;
10988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010989 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10990 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10991 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10992 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010993 break;
10994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010995 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10996 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010997 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010998 break;
10999
11000 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001 case MBEDTLS_KEY_EXCHANGE_NONE:
11002 case MBEDTLS_KEY_EXCHANGE_PSK:
11003 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11004 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011005 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011006 usage = 0;
11007 }
11008 }
11009 else
11010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011011 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11012 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011013 }
11014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011016 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011017 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011018 ret = -1;
11019 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011020#else
11021 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11025 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11028 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011029 }
11030 else
11031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11033 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011034 }
11035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011037 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011038 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011039 ret = -1;
11040 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011041#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011042
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011043 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011046
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011047/*
11048 * Convert version numbers to/from wire format
11049 * and, for DTLS, to/from TLS equivalent.
11050 *
11051 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011052 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011053 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11054 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011056void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011057 unsigned char ver[2] )
11058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011059#if defined(MBEDTLS_SSL_PROTO_DTLS)
11060 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011063 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11064
11065 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11066 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11067 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011068 else
11069#else
11070 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011071#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011072 {
11073 ver[0] = (unsigned char) major;
11074 ver[1] = (unsigned char) minor;
11075 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011076}
11077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011078void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011079 const unsigned char ver[2] )
11080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011081#if defined(MBEDTLS_SSL_PROTO_DTLS)
11082 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011083 {
11084 *major = 255 - ver[0] + 2;
11085 *minor = 255 - ver[1] + 1;
11086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011087 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011088 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11089 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011090 else
11091#else
11092 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011093#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011094 {
11095 *major = ver[0];
11096 *minor = ver[1];
11097 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011098}
11099
Simon Butcher99000142016-10-13 17:21:01 +010011100int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11101{
11102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11103 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11104 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11105
11106 switch( md )
11107 {
11108#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11109#if defined(MBEDTLS_MD5_C)
11110 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011111 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011112#endif
11113#if defined(MBEDTLS_SHA1_C)
11114 case MBEDTLS_SSL_HASH_SHA1:
11115 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11116 break;
11117#endif
11118#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11119#if defined(MBEDTLS_SHA512_C)
11120 case MBEDTLS_SSL_HASH_SHA384:
11121 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11122 break;
11123#endif
11124#if defined(MBEDTLS_SHA256_C)
11125 case MBEDTLS_SSL_HASH_SHA256:
11126 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11127 break;
11128#endif
11129 default:
11130 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11131 }
11132
11133 return 0;
11134#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11135 (void) ssl;
11136 (void) md;
11137
11138 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11139#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11140}
11141
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011142#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11143 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11144int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11145 unsigned char *output,
11146 unsigned char *data, size_t data_len )
11147{
11148 int ret = 0;
11149 mbedtls_md5_context mbedtls_md5;
11150 mbedtls_sha1_context mbedtls_sha1;
11151
11152 mbedtls_md5_init( &mbedtls_md5 );
11153 mbedtls_sha1_init( &mbedtls_sha1 );
11154
11155 /*
11156 * digitally-signed struct {
11157 * opaque md5_hash[16];
11158 * opaque sha_hash[20];
11159 * };
11160 *
11161 * md5_hash
11162 * MD5(ClientHello.random + ServerHello.random
11163 * + ServerParams);
11164 * sha_hash
11165 * SHA(ClientHello.random + ServerHello.random
11166 * + ServerParams);
11167 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011168 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011169 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011171 goto exit;
11172 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011173 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011174 ssl->handshake->randbytes, 64 ) ) != 0 )
11175 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 goto exit;
11178 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011179 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011180 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011182 goto exit;
11183 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011184 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011185 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011187 goto exit;
11188 }
11189
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011190 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011191 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011193 goto exit;
11194 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011195 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011196 ssl->handshake->randbytes, 64 ) ) != 0 )
11197 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011198 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011199 goto exit;
11200 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011201 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011202 data_len ) ) != 0 )
11203 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011205 goto exit;
11206 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011207 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011208 output + 16 ) ) != 0 )
11209 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011211 goto exit;
11212 }
11213
11214exit:
11215 mbedtls_md5_free( &mbedtls_md5 );
11216 mbedtls_sha1_free( &mbedtls_sha1 );
11217
11218 if( ret != 0 )
11219 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11220 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11221
11222 return( ret );
11223
11224}
11225#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11226 MBEDTLS_SSL_PROTO_TLS1_1 */
11227
11228#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11229 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011230
11231#if defined(MBEDTLS_USE_PSA_CRYPTO)
11232int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11233 unsigned char *hash, size_t *hashlen,
11234 unsigned char *data, size_t data_len,
11235 mbedtls_md_type_t md_alg )
11236{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011237 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011238 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011239 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11240
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011241 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011242
11243 if( ( status = psa_hash_setup( &hash_operation,
11244 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011245 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011246 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011247 goto exit;
11248 }
11249
Andrzej Kurek814feff2019-01-14 04:35:19 -050011250 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11251 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011252 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011253 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011254 goto exit;
11255 }
11256
Andrzej Kurek814feff2019-01-14 04:35:19 -050011257 if( ( status = psa_hash_update( &hash_operation,
11258 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011259 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011260 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011261 goto exit;
11262 }
11263
Andrzej Kurek814feff2019-01-14 04:35:19 -050011264 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11265 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011266 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011267 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011268 goto exit;
11269 }
11270
11271exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011272 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011273 {
11274 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11275 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011276 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011277 {
11278 case PSA_ERROR_NOT_SUPPORTED:
11279 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011280 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011281 case PSA_ERROR_BUFFER_TOO_SMALL:
11282 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11283 case PSA_ERROR_INSUFFICIENT_MEMORY:
11284 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11285 default:
11286 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11287 }
11288 }
11289 return( 0 );
11290}
11291
11292#else
11293
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011294int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011295 unsigned char *hash, size_t *hashlen,
11296 unsigned char *data, size_t data_len,
11297 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011298{
11299 int ret = 0;
11300 mbedtls_md_context_t ctx;
11301 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011302 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011303
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011304 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011305
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011306 mbedtls_md_init( &ctx );
11307
11308 /*
11309 * digitally-signed struct {
11310 * opaque client_random[32];
11311 * opaque server_random[32];
11312 * ServerDHParams params;
11313 * };
11314 */
11315 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11316 {
11317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11318 goto exit;
11319 }
11320 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11321 {
11322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11323 goto exit;
11324 }
11325 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11326 {
11327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11328 goto exit;
11329 }
11330 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11331 {
11332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11333 goto exit;
11334 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011335 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011336 {
11337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11338 goto exit;
11339 }
11340
11341exit:
11342 mbedtls_md_free( &ctx );
11343
11344 if( ret != 0 )
11345 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11346 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11347
11348 return( ret );
11349}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011350#endif /* MBEDTLS_USE_PSA_CRYPTO */
11351
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011352#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11353 MBEDTLS_SSL_PROTO_TLS1_2 */
11354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011355#endif /* MBEDTLS_SSL_TLS_C */