blob: 0386ea0e892948cfac36ab8c6a22e76dffa78a79 [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( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002576 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2579 }
2580 if( rec == NULL ||
2581 rec->buf == NULL ||
2582 rec->buf_len < rec->data_offset ||
2583 rec->buf_len - rec->data_offset < rec->data_len )
2584 {
2585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002587 }
2588
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002589 data = rec->buf + rec->data_offset;
2590 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Hanno Beckera0e20d02019-05-15 14:03:01 +01002592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002593 /*
2594 * Match record's CID with incoming CID.
2595 */
Hanno Becker938489a2019-05-08 13:02:22 +01002596 if( rec->cid_len != transform->in_cid_len ||
2597 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2598 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002599 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002600 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002601#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2604 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002605 {
2606 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002607 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2608 transform->iv_dec,
2609 transform->ivlen,
2610 data, rec->data_len,
2611 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002614 return( ret );
2615 }
2616
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2620 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002621 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 }
Paul Bakker68884e32013-01-07 18:20:04 +01002623 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002625#if defined(MBEDTLS_GCM_C) || \
2626 defined(MBEDTLS_CCM_C) || \
2627 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 mode == MBEDTLS_MODE_CCM ||
2630 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002631 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002634
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002635 /*
2636 * Compute and update sizes
2637 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002641 "+ taglen (%d)", rec->data_len,
2642 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002644 }
Paul Bakker68884e32013-01-07 18:20:04 +01002645
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002646 /*
2647 * Prepare IV
2648 */
2649 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2650 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002651 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002652 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002653 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002654
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002655 }
2656 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2657 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002658 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002659 unsigned char i;
2660
2661 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2662
2663 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002665 }
2666 else
2667 {
2668 /* Reminder if we ever add an AEAD mode with a different size */
2669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2671 }
2672
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 data += explicit_iv_len;
2674 rec->data_offset += explicit_iv_len;
2675 rec->data_len -= explicit_iv_len + transform->taglen;
2676
Hanno Beckercab87e62019-04-29 13:52:53 +01002677 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002679 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002680
2681 memcpy( transform->iv_dec + transform->fixed_ivlen,
2682 data - explicit_iv_len, explicit_iv_len );
2683
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002684 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002685 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002686 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002687
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002689 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002690 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002691 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002692 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2693 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002694 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002695 data, rec->data_len,
2696 data, &olen,
2697 data + rec->data_len,
2698 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2703 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002704
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002705 return( ret );
2706 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002707 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002708
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002709 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002713 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002714 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2717#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002718 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002721 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002722
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 /*
Paul Bakker45829992013-01-03 14:52:21 +01002724 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002727 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2728 {
2729 /* The ciphertext is prefixed with the CBC IV. */
2730 minlen += transform->ivlen;
2731 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002732#endif
Paul Bakker45829992013-01-03 14:52:21 +01002733
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002734 /* Size considerations:
2735 *
2736 * - The CBC cipher text must not be empty and hence
2737 * at least of size transform->ivlen.
2738 *
2739 * Together with the potential IV-prefix, this explains
2740 * the first of the two checks below.
2741 *
2742 * - The record must contain a MAC, either in plain or
2743 * encrypted, depending on whether Encrypt-then-MAC
2744 * is used or not.
2745 * - If it is, the message contains the IV-prefix,
2746 * the CBC ciphertext, and the MAC.
2747 * - If it is not, the padded plaintext, and hence
2748 * the CBC ciphertext, has at least length maclen + 1
2749 * because there is at least the padding length byte.
2750 *
2751 * As the CBC ciphertext is not empty, both cases give the
2752 * lower bound minlen + maclen + 1 on the record size, which
2753 * we test for in the second check below.
2754 */
2755 if( rec->data_len < minlen + transform->ivlen ||
2756 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 "+ 1 ) ( + expl IV )", rec->data_len,
2760 transform->ivlen,
2761 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002763 }
2764
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002765 /*
2766 * Authenticate before decrypt if enabled
2767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002769 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002770 {
Hanno Becker992b6872017-11-09 18:57:39 +00002771 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002774
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2776 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777
Hanno Beckercab87e62019-04-29 13:52:53 +01002778 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002779
Hanno Beckercab87e62019-04-29 13:52:53 +01002780 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2781 add_data_len );
2782 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2783 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002784 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2785 data, rec->data_len );
2786 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2787 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002788
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2790 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002791 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002792 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002793
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2795 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002799 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002800 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002801 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002803
2804 /*
2805 * Check length sanity
2806 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002807 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002810 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002812 }
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002819 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002820 /* This is safe because data_len >= minlen + maclen + 1 initially,
2821 * and at this point we have at most subtracted maclen (note that
2822 * minlen == transform->ivlen here). */
2823 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002824
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 data += transform->ivlen;
2826 rec->data_offset += transform->ivlen;
2827 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002828 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002830
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2832 transform->iv_dec, transform->ivlen,
2833 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002836 return( ret );
2837 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002838
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002843 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002846 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002847 {
2848 /*
2849 * Save IV in SSL3 and TLS1
2850 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2852 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 /* Safe since data_len >= minlen + maclen + 1, so after having
2857 * subtracted at most minlen and maclen up to this point,
2858 * data_len > 0. */
2859 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002860
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002861 if( auth_done == 1 )
2862 {
2863 correct *= ( rec->data_len >= padlen + 1 );
2864 padlen *= ( rec->data_len >= padlen + 1 );
2865 }
2866 else
Paul Bakker45829992013-01-03 14:52:21 +01002867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 if( rec->data_len < transform->maclen + padlen + 1 )
2870 {
2871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2872 rec->data_len,
2873 transform->maclen,
2874 padlen + 1 ) );
2875 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002876#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002877
2878 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2879 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 padlen++;
2883
2884 /* Regardless of the validity of the padding,
2885 * we have data_len >= padlen here. */
2886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002888 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#if defined(MBEDTLS_SSL_DEBUG_ALL)
2893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 "should be no more than %d",
2895 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002896#endif
Paul Bakker45829992013-01-03 14:52:21 +01002897 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 }
2899 }
2900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2902#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2903 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002904 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002906 /* The padding check involves a series of up to 256
2907 * consecutive memory reads at the end of the record
2908 * plaintext buffer. In order to hide the length and
2909 * validity of the padding, always perform exactly
2910 * `min(256,plaintext_len)` reads (but take into account
2911 * only the last `padlen` bytes for the padding check). */
2912 size_t pad_count = 0;
2913 size_t real_count = 0;
2914 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002915
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 /* Index of first padding byte; it has been ensured above
2917 * that the subtraction is safe. */
2918 size_t const padding_idx = rec->data_len - padlen;
2919 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2920 size_t const start_idx = rec->data_len - num_checks;
2921 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002922
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002923 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002924 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002925 real_count |= ( idx >= padding_idx );
2926 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002927 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002928 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002931 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002933#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002934 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2938 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002942 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002943
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002944 /* If the padding was found to be invalid, padlen == 0
2945 * and the subtraction is safe. If the padding was found valid,
2946 * padlen hasn't been changed and the previous assertion
2947 * data_len >= padlen still holds. */
2948 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002950 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002952 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002958#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002960 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002961#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
2963 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002964 * Authenticate if not done yet.
2965 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 */
Hanno Becker52344c22018-01-03 15:24:20 +00002967#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002968 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002969 {
Hanno Becker992b6872017-11-09 18:57:39 +00002970 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002971
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002972 /* If the initial value of padlen was such that
2973 * data_len < maclen + padlen + 1, then padlen
2974 * got reset to 1, and the initial check
2975 * data_len >= minlen + maclen + 1
2976 * guarantees that at this point we still
2977 * have at least data_len >= maclen.
2978 *
2979 * If the initial value of padlen was such that
2980 * data_len >= maclen + padlen + 1, then we have
2981 * subtracted either padlen + 1 (if the padding was correct)
2982 * or 0 (if the padding was incorrect) since then,
2983 * hence data_len >= maclen in any case.
2984 */
2985 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Hanno Beckercab87e62019-04-29 13:52:53 +01002987 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002991 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002992 ssl_mac( &transform->md_ctx_dec,
2993 transform->mac_dec,
2994 data, rec->data_len,
2995 rec->ctr, rec->type,
2996 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002997 }
2998 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3000#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3001 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003002 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003003 {
3004 /*
3005 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003006 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003007 *
3008 * Known timing attacks:
3009 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3010 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003011 * To compensate for different timings for the MAC calculation
3012 * depending on how much padding was removed (which is determined
3013 * by padlen), process extra_run more blocks through the hash
3014 * function.
3015 *
3016 * The formula in the paper is
3017 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3018 * where L1 is the size of the header plus the decrypted message
3019 * plus CBC padding and L2 is the size of the header plus the
3020 * decrypted message. This is for an underlying hash function
3021 * with 64-byte blocks.
3022 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3023 * correctly. We round down instead of up, so -56 is the correct
3024 * value for our calculations instead of -55.
3025 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003026 * Repeat the formula rather than defining a block_size variable.
3027 * This avoids requiring division by a variable at runtime
3028 * (which would be marginally less efficient and would require
3029 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003030 */
3031 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003032 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003033
3034 /*
3035 * The next two sizes are the minimum and maximum values of
3036 * in_msglen over all padlen values.
3037 *
3038 * They're independent of padlen, since we previously did
3039 * in_msglen -= padlen.
3040 *
3041 * Note that max_len + maclen is never more than the buffer
3042 * length, as we previously did in_msglen -= maclen too.
3043 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003044 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003045 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3046
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003047 memset( tmp, 0, sizeof( tmp ) );
3048
3049 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003050 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003051#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3052 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003053 case MBEDTLS_MD_MD5:
3054 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003055 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003056 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003057 extra_run =
3058 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3059 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003060 break;
3061#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003062#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003063 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003064 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003065 extra_run =
3066 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3067 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003068 break;
3069#endif
3070 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3073 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003074
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003075 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003076
Hanno Beckercab87e62019-04-29 13:52:53 +01003077 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3078 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3080 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003081 /* Make sure we access everything even when padlen > 0. This
3082 * makes the synchronisation requirements for just-in-time
3083 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003084 ssl_read_memory( data + rec->data_len, padlen );
3085 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003086
3087 /* Call mbedtls_md_process at least once due to cache attacks
3088 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003089 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003091
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003092 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003093
3094 /* Make sure we access all the memory that could contain the MAC,
3095 * before we check it in the next code block. This makes the
3096 * synchronisation requirements for just-in-time Prime+Probe
3097 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003098 ssl_read_memory( data + min_len,
3099 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003100 }
3101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3103 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3106 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003107 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003108
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003109#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003110 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3111 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003112#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003114 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3115 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_DEBUG_ALL)
3118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003119#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003120 correct = 0;
3121 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003122 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003123 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003124
3125 /*
3126 * Finally check the correct flag
3127 */
3128 if( correct == 0 )
3129 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003130#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003131
3132 /* Make extra sure authentication was performed, exactly once */
3133 if( auth_done != 1 )
3134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Hanno Beckera0e20d02019-05-15 14:03:01 +01003139#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003140 if( rec->cid_len != 0 )
3141 {
3142 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3143 &rec->type );
3144 if( ret != 0 )
3145 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3146 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003147#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
3151 return( 0 );
3152}
3153
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003154#undef MAC_NONE
3155#undef MAC_PLAINTEXT
3156#undef MAC_CIPHERTEXT
3157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003159/*
3160 * Compression/decompression functions
3161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003163{
3164 int ret;
3165 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003166 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003168 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003171
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003172 if( len_pre == 0 )
3173 return( 0 );
3174
Paul Bakker2770fbd2012-07-03 13:30:23 +00003175 memcpy( msg_pre, ssl->out_msg, len_pre );
3176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003178 ssl->out_msglen ) );
3179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003181 ssl->out_msg, ssl->out_msglen );
3182
Paul Bakker48916f92012-09-16 19:57:18 +00003183 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3184 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3185 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003186 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003187
Paul Bakker48916f92012-09-16 19:57:18 +00003188 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189 if( ret != Z_OK )
3190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3192 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193 }
3194
Angus Grattond8213d02016-05-25 20:56:48 +10003195 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003196 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003199 ssl->out_msglen ) );
3200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 ssl->out_msg, ssl->out_msglen );
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003205
3206 return( 0 );
3207}
3208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003210{
3211 int ret;
3212 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003213 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003215 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003218
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003219 if( len_pre == 0 )
3220 return( 0 );
3221
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222 memcpy( msg_pre, ssl->in_msg, len_pre );
3223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225 ssl->in_msglen ) );
3226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228 ssl->in_msg, ssl->in_msglen );
3229
Paul Bakker48916f92012-09-16 19:57:18 +00003230 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3231 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3232 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003233 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003234 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003235
Paul Bakker48916f92012-09-16 19:57:18 +00003236 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237 if( ret != Z_OK )
3238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3240 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003241 }
3242
Angus Grattond8213d02016-05-25 20:56:48 +10003243 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003244 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003247 ssl->in_msglen ) );
3248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003250 ssl->in_msg, ssl->in_msglen );
3251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003253
3254 return( 0 );
3255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3259static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#if defined(MBEDTLS_SSL_PROTO_DTLS)
3262static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003263{
3264 /* If renegotiation is not enforced, retransmit until we would reach max
3265 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003266 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003267 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003268 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003269 unsigned char doublings = 1;
3270
3271 while( ratio != 0 )
3272 {
3273 ++doublings;
3274 ratio >>= 1;
3275 }
3276
3277 if( ++ssl->renego_records_seen > doublings )
3278 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003280 return( 0 );
3281 }
3282 }
3283
3284 return( ssl_write_hello_request( ssl ) );
3285}
3286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003288
Paul Bakker5121ce52009-01-03 21:22:43 +00003289/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003290 * Fill the input message buffer by appending data to it.
3291 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003292 *
3293 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3294 * available (from this read and/or a previous one). Otherwise, an error code
3295 * is returned (possibly EOF or WANT_READ).
3296 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003297 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3298 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3299 * since we always read a whole datagram at once.
3300 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003301 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003302 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305{
Paul Bakker23986e52011-04-24 08:57:21 +00003306 int ret;
3307 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003310
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003311 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003314 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003316 }
3317
Angus Grattond8213d02016-05-25 20:56:48 +10003318 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003322 }
3323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003325 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003327 uint32_t timeout;
3328
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003329 /* Just to be sure */
3330 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3331 {
3332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3333 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3335 }
3336
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003337 /*
3338 * The point is, we need to always read a full datagram at once, so we
3339 * sometimes read more then requested, and handle the additional data.
3340 * It could be the rest of the current record (while fetching the
3341 * header) and/or some other records in the same datagram.
3342 */
3343
3344 /*
3345 * Move to the next record in the already read datagram if applicable
3346 */
3347 if( ssl->next_record_offset != 0 )
3348 {
3349 if( ssl->in_left < ssl->next_record_offset )
3350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3352 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003353 }
3354
3355 ssl->in_left -= ssl->next_record_offset;
3356
3357 if( ssl->in_left != 0 )
3358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003360 ssl->next_record_offset ) );
3361 memmove( ssl->in_hdr,
3362 ssl->in_hdr + ssl->next_record_offset,
3363 ssl->in_left );
3364 }
3365
3366 ssl->next_record_offset = 0;
3367 }
3368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003371
3372 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003373 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003374 */
3375 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003378 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003379 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003380
3381 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003382 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003383 * are not at the beginning of a new record, the caller did something
3384 * wrong.
3385 */
3386 if( ssl->in_left != 0 )
3387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003390 }
3391
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003392 /*
3393 * Don't even try to read if time's out already.
3394 * This avoids by-passing the timer when repeatedly receiving messages
3395 * that will end up being dropped.
3396 */
3397 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003398 {
3399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003400 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003401 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003402 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003403 {
Angus Grattond8213d02016-05-25 20:56:48 +10003404 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003407 timeout = ssl->handshake->retransmit_timeout;
3408 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003409 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003412
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003413 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003414 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3415 timeout );
3416 else
3417 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003420
3421 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003423 }
3424
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003425 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003428 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003431 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003432 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003435 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003436 }
3437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003441 return( ret );
3442 }
3443
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003444 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003447 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003449 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003450 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003453 return( ret );
3454 }
3455
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003456 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003459 }
3460
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 if( ret < 0 )
3462 return( ret );
3463
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003464 ssl->in_left = ret;
3465 }
3466 else
3467#endif
3468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003470 ssl->in_left, nb_want ) );
3471
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003472 while( ssl->in_left < nb_want )
3473 {
3474 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003475
3476 if( ssl_check_timer( ssl ) != 0 )
3477 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3478 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003479 {
3480 if( ssl->f_recv_timeout != NULL )
3481 {
3482 ret = ssl->f_recv_timeout( ssl->p_bio,
3483 ssl->in_hdr + ssl->in_left, len,
3484 ssl->conf->read_timeout );
3485 }
3486 else
3487 {
3488 ret = ssl->f_recv( ssl->p_bio,
3489 ssl->in_hdr + ssl->in_left, len );
3490 }
3491 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003494 ssl->in_left, nb_want ) );
3495 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003496
3497 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003499
3500 if( ret < 0 )
3501 return( ret );
3502
mohammad160352aecb92018-03-28 23:41:40 -07003503 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003504 {
Darryl Green11999bb2018-03-13 15:22:58 +00003505 MBEDTLS_SSL_DEBUG_MSG( 1,
3506 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003507 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3509 }
3510
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003511 ssl->in_left += ret;
3512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 }
3514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
3517 return( 0 );
3518}
3519
3520/*
3521 * Flush any data not yet written
3522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003524{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003525 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003526 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003530 if( ssl->f_send == NULL )
3531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003533 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003535 }
3536
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003537 /* Avoid incrementing counter if data is flushed */
3538 if( ssl->out_left == 0 )
3539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003541 return( 0 );
3542 }
3543
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 while( ssl->out_left > 0 )
3545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003547 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003548
Hanno Becker2b1e3542018-08-06 11:19:13 +01003549 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003550 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
3554 if( ret <= 0 )
3555 return( ret );
3556
mohammad160352aecb92018-03-28 23:41:40 -07003557 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003558 {
Darryl Green11999bb2018-03-13 15:22:58 +00003559 MBEDTLS_SSL_DEBUG_MSG( 1,
3560 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003561 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3563 }
3564
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 ssl->out_left -= ret;
3566 }
3567
Hanno Becker2b1e3542018-08-06 11:19:13 +01003568#if defined(MBEDTLS_SSL_PROTO_DTLS)
3569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003570 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003571 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003572 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003573 else
3574#endif
3575 {
3576 ssl->out_hdr = ssl->out_buf + 8;
3577 }
3578 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003581
3582 return( 0 );
3583}
3584
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003585/*
3586 * Functions to handle the DTLS retransmission state machine
3587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003589/*
3590 * Append current handshake message to current outgoing flight
3591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3596 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3597 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598
3599 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003600 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003601 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003604 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003605 }
3606
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003607 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003608 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003611 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003612 }
3613
3614 /* Copy current handshake message with headers */
3615 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3616 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003617 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618 msg->next = NULL;
3619
3620 /* Append to the current flight */
3621 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003622 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003623 else
3624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003626 while( cur->next != NULL )
3627 cur = cur->next;
3628 cur->next = msg;
3629 }
3630
Hanno Becker3b235902018-08-06 09:54:53 +01003631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003632 return( 0 );
3633}
3634
3635/*
3636 * Free the current flight of handshake messages
3637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 mbedtls_ssl_flight_item *cur = flight;
3641 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003642
3643 while( cur != NULL )
3644 {
3645 next = cur->next;
3646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 mbedtls_free( cur->p );
3648 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649
3650 cur = next;
3651 }
3652}
3653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3655static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003656#endif
3657
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003658/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003659 * Swap transform_out and out_ctr with the alternative ones
3660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003664 unsigned char tmp_out_ctr[8];
3665
3666 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003669 return;
3670 }
3671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003673
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003674 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003675 tmp_transform = ssl->transform_out;
3676 ssl->transform_out = ssl->handshake->alt_transform_out;
3677 ssl->handshake->alt_transform_out = tmp_transform;
3678
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003679 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003680 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3681 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003682 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003683
3684 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003685 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3688 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3693 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003694 }
3695 }
3696#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003697}
3698
3699/*
3700 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003701 */
3702int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3703{
3704 int ret = 0;
3705
3706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3707
3708 ret = mbedtls_ssl_flight_transmit( ssl );
3709
3710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3711
3712 return( ret );
3713}
3714
3715/*
3716 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003717 *
3718 * Need to remember the current message in case flush_output returns
3719 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003720 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003722int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003724 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003728 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003730
3731 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003732 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003733 ssl_swap_epochs( ssl );
3734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003736 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003737
3738 while( ssl->handshake->cur_msg != NULL )
3739 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003740 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003741 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003742
Hanno Beckere1dcb032018-08-17 16:47:58 +01003743 int const is_finished =
3744 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3745 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3746
Hanno Becker04da1892018-08-14 13:22:10 +01003747 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3748 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3749
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003750 /* Swap epochs before sending Finished: we can't do it after
3751 * sending ChangeCipherSpec, in case write returns WANT_READ.
3752 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003753 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003754 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003756 ssl_swap_epochs( ssl );
3757 }
3758
Hanno Becker67bc7c32018-08-06 11:33:50 +01003759 ret = ssl_get_remaining_payload_in_datagram( ssl );
3760 if( ret < 0 )
3761 return( ret );
3762 max_frag_len = (size_t) ret;
3763
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003764 /* CCS is copied as is, while HS messages may need fragmentation */
3765 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3766 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003767 if( max_frag_len == 0 )
3768 {
3769 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3770 return( ret );
3771
3772 continue;
3773 }
3774
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003775 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003776 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003777 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003778
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003779 /* Update position inside current message */
3780 ssl->handshake->cur_msg_p += cur->len;
3781 }
3782 else
3783 {
3784 const unsigned char * const p = ssl->handshake->cur_msg_p;
3785 const size_t hs_len = cur->len - 12;
3786 const size_t frag_off = p - ( cur->p + 12 );
3787 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003788 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003789
Hanno Beckere1dcb032018-08-17 16:47:58 +01003790 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003791 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003792 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003793 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003794
Hanno Becker67bc7c32018-08-06 11:33:50 +01003795 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3796 return( ret );
3797
3798 continue;
3799 }
3800 max_hs_frag_len = max_frag_len - 12;
3801
3802 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3803 max_hs_frag_len : rem_len;
3804
3805 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003806 {
3807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 (unsigned) cur_hs_frag_len,
3809 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003810 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003811
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003812 /* Messages are stored with handshake headers as if not fragmented,
3813 * copy beginning of headers then fill fragmentation fields.
3814 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3815 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003816
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003817 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3818 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3819 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3820
Hanno Becker67bc7c32018-08-06 11:33:50 +01003821 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3822 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3823 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003824
3825 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3826
Hanno Becker3f7b9732018-08-28 09:53:25 +01003827 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003828 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3829 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003830 ssl->out_msgtype = cur->type;
3831
3832 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003833 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003834 }
3835
3836 /* If done with the current message move to the next one if any */
3837 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3838 {
3839 if( cur->next != NULL )
3840 {
3841 ssl->handshake->cur_msg = cur->next;
3842 ssl->handshake->cur_msg_p = cur->next->p + 12;
3843 }
3844 else
3845 {
3846 ssl->handshake->cur_msg = NULL;
3847 ssl->handshake->cur_msg_p = NULL;
3848 }
3849 }
3850
3851 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003852 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003855 return( ret );
3856 }
3857 }
3858
Hanno Becker67bc7c32018-08-06 11:33:50 +01003859 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3860 return( ret );
3861
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003862 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3864 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003865 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003868 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3869 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003870
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003872
3873 return( 0 );
3874}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003875
3876/*
3877 * To be called when the last message of an incoming flight is received.
3878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003880{
3881 /* We won't need to resend that one any more */
3882 ssl_flight_free( ssl->handshake->flight );
3883 ssl->handshake->flight = NULL;
3884 ssl->handshake->cur_msg = NULL;
3885
3886 /* The next incoming flight will start with this msg_seq */
3887 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3888
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003889 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003890 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003891
Hanno Becker0271f962018-08-16 13:23:47 +01003892 /* Clear future message buffering structure. */
3893 ssl_buffering_free( ssl );
3894
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003895 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003896 ssl_set_timer( ssl, 0 );
3897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3899 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003902 }
3903 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003905}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003906
3907/*
3908 * To be called when the last message of an outgoing flight is send.
3909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003911{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003912 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003913 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3916 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003919 }
3920 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003922}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003924
Paul Bakker5121ce52009-01-03 21:22:43 +00003925/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003926 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928
3929/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003930 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003931 *
3932 * - fill in handshake headers
3933 * - update handshake checksum
3934 * - DTLS: save message for resending
3935 * - then pass to the record layer
3936 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003937 * DTLS: except for HelloRequest, messages are only queued, and will only be
3938 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003939 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003940 * Inputs:
3941 * - ssl->out_msglen: 4 + actual handshake message len
3942 * (4 is the size of handshake headers for TLS)
3943 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3944 * - ssl->out_msg + 4: the handshake message body
3945 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003946 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003947 * - ssl->out_msglen: the length of the record contents
3948 * (including handshake headers but excluding record headers)
3949 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003950 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003951int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003952{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003953 int ret;
3954 const size_t hs_len = ssl->out_msglen - 4;
3955 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3958
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003959 /*
3960 * Sanity checks
3961 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003962 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003963 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3964 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003965 /* In SSLv3, the client might send a NoCertificate alert. */
3966#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3967 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3968 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3969 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3970#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3974 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003976
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003977 /* Whenever we send anything different from a
3978 * HelloRequest we should be in a handshake - double check. */
3979 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3980 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003981 ssl->handshake == NULL )
3982 {
3983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3985 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003989 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003991 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003994 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003995#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003996
Hanno Beckerb50a2532018-08-06 11:52:54 +01003997 /* Double-check that we did not exceed the bounds
3998 * of the outgoing record buffer.
3999 * This should never fail as the various message
4000 * writing functions must obey the bounds of the
4001 * outgoing record buffer, but better be safe.
4002 *
4003 * Note: We deliberately do not check for the MTU or MFL here.
4004 */
4005 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4006 {
4007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4008 "size %u, maximum %u",
4009 (unsigned) ssl->out_msglen,
4010 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4012 }
4013
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004014 /*
4015 * Fill handshake headers
4016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004019 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4020 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4021 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004022
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004023 /*
4024 * DTLS has additional fields in the Handshake layer,
4025 * between the length field and the actual payload:
4026 * uint16 message_seq;
4027 * uint24 fragment_offset;
4028 * uint24 fragment_length;
4029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004032 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004033 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004034 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004035 {
4036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4037 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004038 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004039 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4041 }
4042
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004043 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004044 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004045
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004046 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004047 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004048 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004049 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4050 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4051 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004052 }
4053 else
4054 {
4055 ssl->out_msg[4] = 0;
4056 ssl->out_msg[5] = 0;
4057 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004058
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004059 /* Handshake hashes are computed without fragmentation,
4060 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004061 memset( ssl->out_msg + 6, 0x00, 3 );
4062 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004063 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004065
Hanno Becker0207e532018-08-28 10:28:28 +01004066 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004067 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4068 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 }
4070
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004074 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4075 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004076 {
4077 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004080 return( ret );
4081 }
4082 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004083 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004084#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004085 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004086 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004087 {
4088 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4089 return( ret );
4090 }
4091 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004092
4093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4094
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004095 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004096}
4097
4098/*
4099 * Record layer functions
4100 */
4101
4102/*
4103 * Write current record.
4104 *
4105 * Uses:
4106 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4107 * - ssl->out_msglen: length of the record content (excl headers)
4108 * - ssl->out_msg: record content
4109 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004110int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004111{
4112 int ret, done = 0;
4113 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004114 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004115
4116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004119 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004121 {
4122 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004125 return( ret );
4126 }
4127
4128 len = ssl->out_msglen;
4129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4133 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 ret = mbedtls_ssl_hw_record_write( ssl );
4138 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4141 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004142 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004143
4144 if( ret == 0 )
4145 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004148 if( !done )
4149 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004150 unsigned i;
4151 size_t protected_record_size;
4152
Hanno Becker6430faf2019-05-08 11:57:13 +01004153 /* Skip writing the record content type to after the encryption,
4154 * as it may change when using the CID extension. */
4155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004157 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004158
Hanno Becker19859472018-08-06 09:40:20 +01004159 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004160 ssl->out_len[0] = (unsigned char)( len >> 8 );
4161 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004162
Paul Bakker48916f92012-09-16 19:57:18 +00004163 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004164 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004165 mbedtls_record rec;
4166
4167 rec.buf = ssl->out_iv;
4168 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4169 ( ssl->out_iv - ssl->out_buf );
4170 rec.data_len = ssl->out_msglen;
4171 rec.data_offset = ssl->out_msg - rec.buf;
4172
4173 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4174 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4175 ssl->conf->transport, rec.ver );
4176 rec.type = ssl->out_msgtype;
4177
Hanno Beckera0e20d02019-05-15 14:03:01 +01004178#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004179 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004180 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004181#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004182
Hanno Beckera18d1322018-01-03 14:27:32 +00004183 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004184 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004187 return( ret );
4188 }
4189
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004190 if( rec.data_offset != 0 )
4191 {
4192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4194 }
4195
Hanno Becker6430faf2019-05-08 11:57:13 +01004196 /* Update the record content type and CID. */
4197 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004198#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004199 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004201 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004202 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4203 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004204 }
4205
Hanno Becker5903de42019-05-03 14:46:38 +01004206 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004207
4208#if defined(MBEDTLS_SSL_PROTO_DTLS)
4209 /* In case of DTLS, double-check that we don't exceed
4210 * the remaining space in the datagram. */
4211 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4212 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004213 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004214 if( ret < 0 )
4215 return( ret );
4216
4217 if( protected_record_size > (size_t) ret )
4218 {
4219 /* Should never happen */
4220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4221 }
4222 }
4223#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004224
Hanno Becker6430faf2019-05-08 11:57:13 +01004225 /* Now write the potentially updated record content type. */
4226 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004229 "version = [%d:%d], msglen = %d",
4230 ssl->out_hdr[0], ssl->out_hdr[1],
4231 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004234 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004235
4236 ssl->out_left += protected_record_size;
4237 ssl->out_hdr += protected_record_size;
4238 ssl_update_out_pointers( ssl, ssl->transform_out );
4239
Hanno Becker04484622018-08-06 09:49:38 +01004240 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4241 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4242 break;
4243
4244 /* The loop goes to its end iff the counter is wrapping */
4245 if( i == ssl_ep_len( ssl ) )
4246 {
4247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4248 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 }
4251
Hanno Becker67bc7c32018-08-06 11:33:50 +01004252#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004253 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4254 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004255 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004256 size_t remaining;
4257 ret = ssl_get_remaining_payload_in_datagram( ssl );
4258 if( ret < 0 )
4259 {
4260 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4261 ret );
4262 return( ret );
4263 }
4264
4265 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004266 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004267 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004268 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004269 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004270 else
4271 {
Hanno Becker513815a2018-08-20 11:56:09 +01004272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004273 }
4274 }
4275#endif /* MBEDTLS_SSL_PROTO_DTLS */
4276
4277 if( ( flush == SSL_FORCE_FLUSH ) &&
4278 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004281 return( ret );
4282 }
4283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004285
4286 return( 0 );
4287}
4288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004290
4291static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4292{
4293 if( ssl->in_msglen < ssl->in_hslen ||
4294 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4295 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4296 {
4297 return( 1 );
4298 }
4299 return( 0 );
4300}
Hanno Becker44650b72018-08-16 12:51:11 +01004301
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004302static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004303{
4304 return( ( ssl->in_msg[9] << 16 ) |
4305 ( ssl->in_msg[10] << 8 ) |
4306 ssl->in_msg[11] );
4307}
4308
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004309static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004310{
4311 return( ( ssl->in_msg[6] << 16 ) |
4312 ( ssl->in_msg[7] << 8 ) |
4313 ssl->in_msg[8] );
4314}
4315
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004316static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004317{
4318 uint32_t msg_len, frag_off, frag_len;
4319
4320 msg_len = ssl_get_hs_total_len( ssl );
4321 frag_off = ssl_get_hs_frag_off( ssl );
4322 frag_len = ssl_get_hs_frag_len( ssl );
4323
4324 if( frag_off > msg_len )
4325 return( -1 );
4326
4327 if( frag_len > msg_len - frag_off )
4328 return( -1 );
4329
4330 if( frag_len + 12 > ssl->in_msglen )
4331 return( -1 );
4332
4333 return( 0 );
4334}
4335
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004336/*
4337 * Mark bits in bitmask (used for DTLS HS reassembly)
4338 */
4339static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4340{
4341 unsigned int start_bits, end_bits;
4342
4343 start_bits = 8 - ( offset % 8 );
4344 if( start_bits != 8 )
4345 {
4346 size_t first_byte_idx = offset / 8;
4347
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004348 /* Special case */
4349 if( len <= start_bits )
4350 {
4351 for( ; len != 0; len-- )
4352 mask[first_byte_idx] |= 1 << ( start_bits - len );
4353
4354 /* Avoid potential issues with offset or len becoming invalid */
4355 return;
4356 }
4357
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004358 offset += start_bits; /* Now offset % 8 == 0 */
4359 len -= start_bits;
4360
4361 for( ; start_bits != 0; start_bits-- )
4362 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4363 }
4364
4365 end_bits = len % 8;
4366 if( end_bits != 0 )
4367 {
4368 size_t last_byte_idx = ( offset + len ) / 8;
4369
4370 len -= end_bits; /* Now len % 8 == 0 */
4371
4372 for( ; end_bits != 0; end_bits-- )
4373 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4374 }
4375
4376 memset( mask + offset / 8, 0xFF, len / 8 );
4377}
4378
4379/*
4380 * Check that bitmask is full
4381 */
4382static int ssl_bitmask_check( unsigned char *mask, size_t len )
4383{
4384 size_t i;
4385
4386 for( i = 0; i < len / 8; i++ )
4387 if( mask[i] != 0xFF )
4388 return( -1 );
4389
4390 for( i = 0; i < len % 8; i++ )
4391 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4392 return( -1 );
4393
4394 return( 0 );
4395}
4396
Hanno Becker56e205e2018-08-16 09:06:12 +01004397/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004398static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004399 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004400{
Hanno Becker56e205e2018-08-16 09:06:12 +01004401 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004402
Hanno Becker56e205e2018-08-16 09:06:12 +01004403 alloc_len = 12; /* Handshake header */
4404 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004405
Hanno Beckerd07df862018-08-16 09:14:58 +01004406 if( add_bitmap )
4407 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004408
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004409 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004410}
Hanno Becker56e205e2018-08-16 09:06:12 +01004411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004413
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004414static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004415{
4416 return( ( ssl->in_msg[1] << 16 ) |
4417 ( ssl->in_msg[2] << 8 ) |
4418 ssl->in_msg[3] );
4419}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004420
Simon Butcher99000142016-10-13 17:21:01 +01004421int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004426 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004428 }
4429
Hanno Becker12555c62018-08-16 12:47:53 +01004430 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004433 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004434 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004437 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004438 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004439 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004440 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004441
Hanno Becker44650b72018-08-16 12:51:11 +01004442 if( ssl_check_hs_header( ssl ) != 0 )
4443 {
4444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4445 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4446 }
4447
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004448 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004449 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4450 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4451 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4452 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004453 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004454 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4455 {
4456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4457 recv_msg_seq,
4458 ssl->handshake->in_msg_seq ) );
4459 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4460 }
4461
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004462 /* Retransmit only on last message from previous flight, to avoid
4463 * too many retransmissions.
4464 * Besides, No sane server ever retransmits HelloVerifyRequest */
4465 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004469 "message_seq = %d, start_of_flight = %d",
4470 recv_msg_seq,
4471 ssl->handshake->in_flight_start_seq ) );
4472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004476 return( ret );
4477 }
4478 }
4479 else
4480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004482 "message_seq = %d, expected = %d",
4483 recv_msg_seq,
4484 ssl->handshake->in_msg_seq ) );
4485 }
4486
Hanno Becker90333da2017-10-10 11:27:13 +01004487 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004488 }
4489 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004490
Hanno Becker6d97ef52018-08-16 13:09:04 +01004491 /* Message reassembly is handled alongside buffering of future
4492 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004493 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004494 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004495 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004498 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004499 }
4500 }
4501 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004503 /* With TLS we don't handle fragmentation (for now) */
4504 if( ssl->in_msglen < ssl->in_hslen )
4505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4507 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004508 }
4509
Simon Butcher99000142016-10-13 17:21:01 +01004510 return( 0 );
4511}
4512
4513void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4514{
Hanno Becker0271f962018-08-16 13:23:47 +01004515 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004516
Hanno Becker0271f962018-08-16 13:23:47 +01004517 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004518 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004519 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004520 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004521
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004522 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004524 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004525 ssl->handshake != NULL )
4526 {
Hanno Becker0271f962018-08-16 13:23:47 +01004527 unsigned offset;
4528 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004529
Hanno Becker0271f962018-08-16 13:23:47 +01004530 /* Increment handshake sequence number */
4531 hs->in_msg_seq++;
4532
4533 /*
4534 * Clear up handshake buffering and reassembly structure.
4535 */
4536
4537 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004538 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004539
4540 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004541 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4542 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004543 offset++, hs_buf++ )
4544 {
4545 *hs_buf = *(hs_buf + 1);
4546 }
4547
4548 /* Create a fresh last entry */
4549 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004550 }
4551#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004552}
4553
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004554/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004555 * DTLS anti-replay: RFC 6347 4.1.2.6
4556 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004557 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4558 * Bit n is set iff record number in_window_top - n has been seen.
4559 *
4560 * Usually, in_window_top is the last record number seen and the lsb of
4561 * in_window is set. The only exception is the initial state (record number 0
4562 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4565static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004566{
4567 ssl->in_window_top = 0;
4568 ssl->in_window = 0;
4569}
4570
4571static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4572{
4573 return( ( (uint64_t) buf[0] << 40 ) |
4574 ( (uint64_t) buf[1] << 32 ) |
4575 ( (uint64_t) buf[2] << 24 ) |
4576 ( (uint64_t) buf[3] << 16 ) |
4577 ( (uint64_t) buf[4] << 8 ) |
4578 ( (uint64_t) buf[5] ) );
4579}
4580
4581/*
4582 * Return 0 if sequence number is acceptable, -1 otherwise
4583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004585{
4586 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4587 uint64_t bit;
4588
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004589 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004590 return( 0 );
4591
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004592 if( rec_seqnum > ssl->in_window_top )
4593 return( 0 );
4594
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004595 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004596
4597 if( bit >= 64 )
4598 return( -1 );
4599
4600 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4601 return( -1 );
4602
4603 return( 0 );
4604}
4605
4606/*
4607 * Update replay window on new validated record
4608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004610{
4611 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4612
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004613 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004614 return;
4615
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004616 if( rec_seqnum > ssl->in_window_top )
4617 {
4618 /* Update window_top and the contents of the window */
4619 uint64_t shift = rec_seqnum - ssl->in_window_top;
4620
4621 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004622 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004623 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004624 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004625 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004626 ssl->in_window |= 1;
4627 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004628
4629 ssl->in_window_top = rec_seqnum;
4630 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004631 else
4632 {
4633 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004634 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004635
4636 if( bit < 64 ) /* Always true, but be extra sure */
4637 ssl->in_window |= (uint64_t) 1 << bit;
4638 }
4639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004641
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004642#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004643/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004644static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4645
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004646/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004647 * Without any SSL context, check if a datagram looks like a ClientHello with
4648 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004649 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004650 *
4651 * - if cookie is valid, return 0
4652 * - if ClientHello looks superficially valid but cookie is not,
4653 * fill obuf and set olen, then
4654 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4655 * - otherwise return a specific error code
4656 */
4657static int ssl_check_dtls_clihlo_cookie(
4658 mbedtls_ssl_cookie_write_t *f_cookie_write,
4659 mbedtls_ssl_cookie_check_t *f_cookie_check,
4660 void *p_cookie,
4661 const unsigned char *cli_id, size_t cli_id_len,
4662 const unsigned char *in, size_t in_len,
4663 unsigned char *obuf, size_t buf_len, size_t *olen )
4664{
4665 size_t sid_len, cookie_len;
4666 unsigned char *p;
4667
4668 if( f_cookie_write == NULL || f_cookie_check == NULL )
4669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4670
4671 /*
4672 * Structure of ClientHello with record and handshake headers,
4673 * and expected values. We don't need to check a lot, more checks will be
4674 * done when actually parsing the ClientHello - skipping those checks
4675 * avoids code duplication and does not make cookie forging any easier.
4676 *
4677 * 0-0 ContentType type; copied, must be handshake
4678 * 1-2 ProtocolVersion version; copied
4679 * 3-4 uint16 epoch; copied, must be 0
4680 * 5-10 uint48 sequence_number; copied
4681 * 11-12 uint16 length; (ignored)
4682 *
4683 * 13-13 HandshakeType msg_type; (ignored)
4684 * 14-16 uint24 length; (ignored)
4685 * 17-18 uint16 message_seq; copied
4686 * 19-21 uint24 fragment_offset; copied, must be 0
4687 * 22-24 uint24 fragment_length; (ignored)
4688 *
4689 * 25-26 ProtocolVersion client_version; (ignored)
4690 * 27-58 Random random; (ignored)
4691 * 59-xx SessionID session_id; 1 byte len + sid_len content
4692 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4693 * ...
4694 *
4695 * Minimum length is 61 bytes.
4696 */
4697 if( in_len < 61 ||
4698 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4699 in[3] != 0 || in[4] != 0 ||
4700 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4701 {
4702 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4703 }
4704
4705 sid_len = in[59];
4706 if( sid_len > in_len - 61 )
4707 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4708
4709 cookie_len = in[60 + sid_len];
4710 if( cookie_len > in_len - 60 )
4711 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4712
4713 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4714 cli_id, cli_id_len ) == 0 )
4715 {
4716 /* Valid cookie */
4717 return( 0 );
4718 }
4719
4720 /*
4721 * If we get here, we've got an invalid cookie, let's prepare HVR.
4722 *
4723 * 0-0 ContentType type; copied
4724 * 1-2 ProtocolVersion version; copied
4725 * 3-4 uint16 epoch; copied
4726 * 5-10 uint48 sequence_number; copied
4727 * 11-12 uint16 length; olen - 13
4728 *
4729 * 13-13 HandshakeType msg_type; hello_verify_request
4730 * 14-16 uint24 length; olen - 25
4731 * 17-18 uint16 message_seq; copied
4732 * 19-21 uint24 fragment_offset; copied
4733 * 22-24 uint24 fragment_length; olen - 25
4734 *
4735 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4736 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4737 *
4738 * Minimum length is 28.
4739 */
4740 if( buf_len < 28 )
4741 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4742
4743 /* Copy most fields and adapt others */
4744 memcpy( obuf, in, 25 );
4745 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4746 obuf[25] = 0xfe;
4747 obuf[26] = 0xff;
4748
4749 /* Generate and write actual cookie */
4750 p = obuf + 28;
4751 if( f_cookie_write( p_cookie,
4752 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4753 {
4754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4755 }
4756
4757 *olen = p - obuf;
4758
4759 /* Go back and fill length fields */
4760 obuf[27] = (unsigned char)( *olen - 28 );
4761
4762 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4763 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4764 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4765
4766 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4767 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4768
4769 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4770}
4771
4772/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004773 * Handle possible client reconnect with the same UDP quadruplet
4774 * (RFC 6347 Section 4.2.8).
4775 *
4776 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4777 * that looks like a ClientHello.
4778 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004779 * - if the input looks like a ClientHello without cookies,
4780 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004781 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004782 * - if the input looks like a ClientHello with a valid cookie,
4783 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004784 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004785 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004786 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004787 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004788 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4789 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004790 */
4791static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4792{
4793 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004794 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004795
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004796 ret = ssl_check_dtls_clihlo_cookie(
4797 ssl->conf->f_cookie_write,
4798 ssl->conf->f_cookie_check,
4799 ssl->conf->p_cookie,
4800 ssl->cli_id, ssl->cli_id_len,
4801 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004802 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004803
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004804 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4805
4806 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004807 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004808 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004809 * If the error is permanent we'll catch it later,
4810 * if it's not, then hopefully it'll work next time. */
4811 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4812
4813 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004814 }
4815
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004816 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004817 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004818 /* Got a valid cookie, partially reset context */
4819 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4820 {
4821 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4822 return( ret );
4823 }
4824
4825 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004826 }
4827
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004828 return( ret );
4829}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004830#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004831
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004832static int ssl_check_record_type( uint8_t record_type )
4833{
4834 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4835 record_type != MBEDTLS_SSL_MSG_ALERT &&
4836 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4837 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4838 {
4839 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4840 }
4841
4842 return( 0 );
4843}
4844
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004845/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004846 * ContentType type;
4847 * ProtocolVersion version;
4848 * uint16 epoch; // DTLS only
4849 * uint48 sequence_number; // DTLS only
4850 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004851 *
4852 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004853 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004854 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4855 *
4856 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004857 * 1. proceed with the record if this function returns 0
4858 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4859 * 3. return CLIENT_RECONNECT if this function return that value
4860 * 4. drop the whole datagram if this function returns anything else.
4861 * Point 2 is needed when the peer is resending, and we have already received
4862 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004865{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004866 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004867 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004868
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004869 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004870
Paul Bakker5121ce52009-01-03 21:22:43 +00004871 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004872 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004873
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004874 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004875#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4877 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4878 ssl->conf->cid_len != 0 )
4879 {
4880 /* Shift pointers to account for record header including CID
4881 * struct {
4882 * ContentType special_type = tls12_cid;
4883 * ProtocolVersion version;
4884 * uint16 epoch;
4885 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004886 * opaque cid[cid_length]; // Additional field compared to
4887 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004888 * uint16 length;
4889 * opaque enc_content[DTLSCiphertext.length];
4890 * } DTLSCiphertext;
4891 */
4892
4893 /* So far, we only support static CID lengths
4894 * fixed in the configuration. */
4895 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4896 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4897 }
4898 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004899#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004900 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004903
4904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004905 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4906 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004907 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4908#endif /* MBEDTLS_SSL_PROTO_DTLS */
4909 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4910 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004913 }
4914
4915 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004916 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4919 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004920 }
4921
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004922 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4925 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004926 }
4927
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004928 /* Now that the total length of the record header is known, ensure
4929 * that the current datagram is large enough to hold it.
4930 * This would fail, for example, if we received a datagram of
4931 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4932 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4933 if( ret != 0 )
4934 {
4935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4936 return( ret );
4937 }
4938 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4939
4940 /* Parse and validate record length
4941 * This must happen after the CID parsing because
4942 * its position in the record header depends on
4943 * the presence of a CID. */
4944
4945 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004946 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004947 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4950 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004951 }
4952
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004954 "version = [%d:%d], msglen = %d",
4955 ssl->in_msgtype,
4956 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004957
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004958 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004959 * DTLS-related tests.
4960 * Check epoch before checking length constraint because
4961 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4962 * message gets duplicated before the corresponding Finished message,
4963 * the second ChangeCipherSpec should be discarded because it belongs
4964 * to an old epoch, but not because its length is shorter than
4965 * the minimum record length for packets using the new record transform.
4966 * Note that these two kinds of failures are handled differently,
4967 * as an unexpected record is silently skipped but an invalid
4968 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004969 */
4970#if defined(MBEDTLS_SSL_PROTO_DTLS)
4971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4972 {
4973 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4974
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004975 /* Check epoch (and sequence number) with DTLS */
4976 if( rec_epoch != ssl->in_epoch )
4977 {
4978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4979 "expected %d, received %d",
4980 ssl->in_epoch, rec_epoch ) );
4981
4982#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4983 /*
4984 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4985 * access the first byte of record content (handshake type), as we
4986 * have an active transform (possibly iv_len != 0), so use the
4987 * fact that the record header len is 13 instead.
4988 */
4989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4990 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4991 rec_epoch == 0 &&
4992 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4993 ssl->in_left > 13 &&
4994 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4995 {
4996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4997 "from the same port" ) );
4998 return( ssl_handle_possible_reconnect( ssl ) );
4999 }
5000 else
5001#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01005002 {
5003 /* Consider buffering the record. */
5004 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
5005 {
5006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5007 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5008 }
5009
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005010 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01005011 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005012 }
5013
5014#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5015 /* Replay detection only works for the current epoch */
5016 if( rec_epoch == ssl->in_epoch &&
5017 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5018 {
5019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5020 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5021 }
5022#endif
5023 }
5024#endif /* MBEDTLS_SSL_PROTO_DTLS */
5025
Hanno Becker52c6dc62017-05-26 16:07:36 +01005026
5027 /* Check length against bounds of the current transform and version */
5028 if( ssl->transform_in == NULL )
5029 {
5030 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005031 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005032 {
5033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5034 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5035 }
5036 }
5037 else
5038 {
5039 if( ssl->in_msglen < ssl->transform_in->minlen )
5040 {
5041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5042 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5043 }
5044
5045#if defined(MBEDTLS_SSL_PROTO_SSL3)
5046 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005047 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005048 {
5049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5050 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5051 }
5052#endif
5053#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5054 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5055 /*
5056 * TLS encrypted messages can have up to 256 bytes of padding
5057 */
5058 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5059 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005060 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005061 {
5062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5063 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5064 }
5065#endif
5066 }
5067
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005068 return( 0 );
5069}
Paul Bakker5121ce52009-01-03 21:22:43 +00005070
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005071/*
5072 * If applicable, decrypt (and decompress) record content
5073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005075{
5076 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005079 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5082 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 ret = mbedtls_ssl_hw_record_read( ssl );
5087 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005091 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005092
5093 if( ret == 0 )
5094 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005095 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005097 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005099 mbedtls_record rec;
5100
5101 rec.buf = ssl->in_iv;
5102 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5103 - ( ssl->in_iv - ssl->in_buf );
5104 rec.data_len = ssl->in_msglen;
5105 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005106#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005107 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005108 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005109#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005110
5111 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5112 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5113 ssl->conf->transport, rec.ver );
5114 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005115 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5116 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005119
Hanno Beckera0e20d02019-05-15 14:03:01 +01005120#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005121 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5122 ssl->conf->ignore_unexpected_cid
5123 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5124 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005125 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005126 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005127 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005128#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005129
Paul Bakker5121ce52009-01-03 21:22:43 +00005130 return( ret );
5131 }
5132
Hanno Becker6430faf2019-05-08 11:57:13 +01005133 if( ssl->in_msgtype != rec.type )
5134 {
5135 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5136 ssl->in_msgtype, rec.type ) );
5137 }
5138
5139 /* The record content type may change during decryption,
5140 * so re-read it. */
5141 ssl->in_msgtype = rec.type;
5142 /* Also update the input buffer, because unfortunately
5143 * the server-side ssl_parse_client_hello() reparses the
5144 * record header when receiving a ClientHello initiating
5145 * a renegotiation. */
5146 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005147 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005148 ssl->in_msglen = rec.data_len;
5149 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5150 ssl->in_len[1] = (unsigned char)( rec.data_len );
5151
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005152 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5153 ssl->in_msg, ssl->in_msglen );
5154
Hanno Beckera0e20d02019-05-15 14:03:01 +01005155#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005156 /* We have already checked the record content type
5157 * in ssl_parse_record_header(), failing or silently
5158 * dropping the record in the case of an unknown type.
5159 *
5160 * Since with the use of CIDs, the record content type
5161 * might change during decryption, re-check the record
5162 * content type, but treat a failure as fatal this time. */
5163 if( ssl_check_record_type( ssl->in_msgtype ) )
5164 {
5165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5166 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5167 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005168#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005169
Angus Grattond8213d02016-05-25 20:56:48 +10005170 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5173 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005174 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005175 else if( ssl->in_msglen == 0 )
5176 {
5177#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5178 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5179 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5180 {
5181 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5183 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5184 }
5185#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5186
5187 ssl->nb_zero++;
5188
5189 /*
5190 * Three or more empty messages may be a DoS attack
5191 * (excessive CPU consumption).
5192 */
5193 if( ssl->nb_zero > 3 )
5194 {
5195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005196 "messages, possible DoS attack" ) );
5197 /* Treat the records as if they were not properly authenticated,
5198 * thereby failing the connection if we see more than allowed
5199 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005200 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5201 }
5202 }
5203 else
5204 ssl->nb_zero = 0;
5205
5206#if defined(MBEDTLS_SSL_PROTO_DTLS)
5207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5208 {
5209 ; /* in_ctr read from peer, not maintained internally */
5210 }
5211 else
5212#endif
5213 {
5214 unsigned i;
5215 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5216 if( ++ssl->in_ctr[i - 1] != 0 )
5217 break;
5218
5219 /* The loop goes to its end iff the counter is wrapping */
5220 if( i == ssl_ep_len( ssl ) )
5221 {
5222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5223 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5224 }
5225 }
5226
Paul Bakker5121ce52009-01-03 21:22:43 +00005227 }
5228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005230 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005232 {
5233 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005236 return( ret );
5237 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005238 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005239#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005241#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005242 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005245 }
5246#endif
5247
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005248 return( 0 );
5249}
5250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005251static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005252
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005253/*
5254 * Read a record.
5255 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005256 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5257 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5258 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005259 */
Hanno Becker1097b342018-08-15 14:09:41 +01005260
5261/* Helper functions for mbedtls_ssl_read_record(). */
5262static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005263static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5264static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005265
Hanno Becker327c93b2018-08-15 13:56:18 +01005266int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005267 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005268{
5269 int ret;
5270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005272
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005273 if( ssl->keep_current_message == 0 )
5274 {
5275 do {
Simon Butcher99000142016-10-13 17:21:01 +01005276
Hanno Becker26994592018-08-15 14:14:59 +01005277 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005278 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005279 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005280
Hanno Beckere74d5562018-08-15 14:26:08 +01005281 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005282 {
Hanno Becker40f50842018-08-15 14:48:01 +01005283#if defined(MBEDTLS_SSL_PROTO_DTLS)
5284 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005285
Hanno Becker40f50842018-08-15 14:48:01 +01005286 /* We only check for buffered messages if the
5287 * current datagram is fully consumed. */
5288 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005289 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005290 {
Hanno Becker40f50842018-08-15 14:48:01 +01005291 if( ssl_load_buffered_message( ssl ) == 0 )
5292 have_buffered = 1;
5293 }
5294
5295 if( have_buffered == 0 )
5296#endif /* MBEDTLS_SSL_PROTO_DTLS */
5297 {
5298 ret = ssl_get_next_record( ssl );
5299 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5300 continue;
5301
5302 if( ret != 0 )
5303 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005304 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005305 return( ret );
5306 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005307 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005308 }
5309
5310 ret = mbedtls_ssl_handle_message_type( ssl );
5311
Hanno Becker40f50842018-08-15 14:48:01 +01005312#if defined(MBEDTLS_SSL_PROTO_DTLS)
5313 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5314 {
5315 /* Buffer future message */
5316 ret = ssl_buffer_message( ssl );
5317 if( ret != 0 )
5318 return( ret );
5319
5320 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5321 }
5322#endif /* MBEDTLS_SSL_PROTO_DTLS */
5323
Hanno Becker90333da2017-10-10 11:27:13 +01005324 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5325 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005326
5327 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005328 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005329 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005330 return( ret );
5331 }
5332
Hanno Becker327c93b2018-08-15 13:56:18 +01005333 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005334 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005335 {
5336 mbedtls_ssl_update_handshake_status( ssl );
5337 }
Simon Butcher99000142016-10-13 17:21:01 +01005338 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005339 else
Simon Butcher99000142016-10-13 17:21:01 +01005340 {
Hanno Becker02f59072018-08-15 14:00:24 +01005341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005342 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005343 }
5344
5345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5346
5347 return( 0 );
5348}
5349
Hanno Becker40f50842018-08-15 14:48:01 +01005350#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005351static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005352{
Hanno Becker40f50842018-08-15 14:48:01 +01005353 if( ssl->in_left > ssl->next_record_offset )
5354 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005355
Hanno Becker40f50842018-08-15 14:48:01 +01005356 return( 0 );
5357}
5358
5359static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5360{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005361 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005362 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005363 int ret = 0;
5364
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005365 if( hs == NULL )
5366 return( -1 );
5367
Hanno Beckere00ae372018-08-20 09:39:42 +01005368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5369
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005370 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5371 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5372 {
5373 /* Check if we have seen a ChangeCipherSpec before.
5374 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005375 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005376 {
5377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5378 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005379 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005380 }
5381
Hanno Becker39b8bc92018-08-28 17:17:13 +01005382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005383 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5384 ssl->in_msglen = 1;
5385 ssl->in_msg[0] = 1;
5386
5387 /* As long as they are equal, the exact value doesn't matter. */
5388 ssl->in_left = 0;
5389 ssl->next_record_offset = 0;
5390
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005391 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005392 goto exit;
5393 }
Hanno Becker37f95322018-08-16 13:55:32 +01005394
Hanno Beckerb8f50142018-08-28 10:01:34 +01005395#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005396 /* Debug only */
5397 {
5398 unsigned offset;
5399 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5400 {
5401 hs_buf = &hs->buffering.hs[offset];
5402 if( hs_buf->is_valid == 1 )
5403 {
5404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5405 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005406 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005407 }
5408 }
5409 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005410#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005411
5412 /* Check if we have buffered and/or fully reassembled the
5413 * next handshake message. */
5414 hs_buf = &hs->buffering.hs[0];
5415 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5416 {
5417 /* Synthesize a record containing the buffered HS message. */
5418 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5419 ( hs_buf->data[2] << 8 ) |
5420 hs_buf->data[3];
5421
5422 /* Double-check that we haven't accidentally buffered
5423 * a message that doesn't fit into the input buffer. */
5424 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5425 {
5426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5427 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5428 }
5429
5430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5431 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5432 hs_buf->data, msg_len + 12 );
5433
5434 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5435 ssl->in_hslen = msg_len + 12;
5436 ssl->in_msglen = msg_len + 12;
5437 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5438
5439 ret = 0;
5440 goto exit;
5441 }
5442 else
5443 {
5444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5445 hs->in_msg_seq ) );
5446 }
5447
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005448 ret = -1;
5449
5450exit:
5451
5452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5453 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005454}
5455
Hanno Beckera02b0b42018-08-21 17:20:27 +01005456static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5457 size_t desired )
5458{
5459 int offset;
5460 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5462 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005463
Hanno Becker01315ea2018-08-21 17:22:17 +01005464 /* Get rid of future records epoch first, if such exist. */
5465 ssl_free_buffered_record( ssl );
5466
5467 /* Check if we have enough space available now. */
5468 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5469 hs->buffering.total_bytes_buffered ) )
5470 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005472 return( 0 );
5473 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005474
Hanno Becker4f432ad2018-08-28 10:02:32 +01005475 /* We don't have enough space to buffer the next expected handshake
5476 * message. Remove buffers used for future messages to gain space,
5477 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005478 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5479 offset >= 0; offset-- )
5480 {
5481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5482 offset ) );
5483
Hanno Beckerb309b922018-08-23 13:18:05 +01005484 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005485
5486 /* Check if we have enough space available now. */
5487 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5488 hs->buffering.total_bytes_buffered ) )
5489 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005491 return( 0 );
5492 }
5493 }
5494
5495 return( -1 );
5496}
5497
Hanno Becker40f50842018-08-15 14:48:01 +01005498static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5499{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005500 int ret = 0;
5501 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5502
5503 if( hs == NULL )
5504 return( 0 );
5505
5506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5507
5508 switch( ssl->in_msgtype )
5509 {
5510 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005512
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005513 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005514 break;
5515
5516 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005517 {
5518 unsigned recv_msg_seq_offset;
5519 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5520 mbedtls_ssl_hs_buffer *hs_buf;
5521 size_t msg_len = ssl->in_hslen - 12;
5522
5523 /* We should never receive an old handshake
5524 * message - double-check nonetheless. */
5525 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5526 {
5527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5529 }
5530
5531 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5532 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5533 {
5534 /* Silently ignore -- message too far in the future */
5535 MBEDTLS_SSL_DEBUG_MSG( 2,
5536 ( "Ignore future HS message with sequence number %u, "
5537 "buffering window %u - %u",
5538 recv_msg_seq, ssl->handshake->in_msg_seq,
5539 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5540
5541 goto exit;
5542 }
5543
5544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5545 recv_msg_seq, recv_msg_seq_offset ) );
5546
5547 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5548
5549 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005550 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005551 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005552 size_t reassembly_buf_sz;
5553
Hanno Becker37f95322018-08-16 13:55:32 +01005554 hs_buf->is_fragmented =
5555 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5556
5557 /* We copy the message back into the input buffer
5558 * after reassembly, so check that it's not too large.
5559 * This is an implementation-specific limitation
5560 * and not one from the standard, hence it is not
5561 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005562 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005563 {
5564 /* Ignore message */
5565 goto exit;
5566 }
5567
Hanno Beckere0b150f2018-08-21 15:51:03 +01005568 /* Check if we have enough space to buffer the message. */
5569 if( hs->buffering.total_bytes_buffered >
5570 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5571 {
5572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5574 }
5575
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005576 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5577 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005578
5579 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5580 hs->buffering.total_bytes_buffered ) )
5581 {
5582 if( recv_msg_seq_offset > 0 )
5583 {
5584 /* If we can't buffer a future message because
5585 * of space limitations -- ignore. */
5586 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",
5587 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5588 (unsigned) hs->buffering.total_bytes_buffered ) );
5589 goto exit;
5590 }
Hanno Beckere1801392018-08-21 16:51:05 +01005591 else
5592 {
5593 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",
5594 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5595 (unsigned) hs->buffering.total_bytes_buffered ) );
5596 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005597
Hanno Beckera02b0b42018-08-21 17:20:27 +01005598 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005599 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005600 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",
5601 (unsigned) msg_len,
5602 (unsigned) reassembly_buf_sz,
5603 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005604 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005605 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5606 goto exit;
5607 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005608 }
5609
5610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5611 msg_len ) );
5612
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005613 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5614 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005615 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005616 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005617 goto exit;
5618 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005619 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005620
5621 /* Prepare final header: copy msg_type, length and message_seq,
5622 * then add standardised fragment_offset and fragment_length */
5623 memcpy( hs_buf->data, ssl->in_msg, 6 );
5624 memset( hs_buf->data + 6, 0, 3 );
5625 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5626
5627 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005628
5629 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005630 }
5631 else
5632 {
5633 /* Make sure msg_type and length are consistent */
5634 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5635 {
5636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5637 /* Ignore */
5638 goto exit;
5639 }
5640 }
5641
Hanno Becker4422bbb2018-08-20 09:40:19 +01005642 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005643 {
5644 size_t frag_len, frag_off;
5645 unsigned char * const msg = hs_buf->data + 12;
5646
5647 /*
5648 * Check and copy current fragment
5649 */
5650
5651 /* Validation of header fields already done in
5652 * mbedtls_ssl_prepare_handshake_record(). */
5653 frag_off = ssl_get_hs_frag_off( ssl );
5654 frag_len = ssl_get_hs_frag_len( ssl );
5655
5656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5657 frag_off, frag_len ) );
5658 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5659
5660 if( hs_buf->is_fragmented )
5661 {
5662 unsigned char * const bitmask = msg + msg_len;
5663 ssl_bitmask_set( bitmask, frag_off, frag_len );
5664 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5665 msg_len ) == 0 );
5666 }
5667 else
5668 {
5669 hs_buf->is_complete = 1;
5670 }
5671
5672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5673 hs_buf->is_complete ? "" : "not yet " ) );
5674 }
5675
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005676 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005677 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005678
5679 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005680 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005681 break;
5682 }
5683
5684exit:
5685
5686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5687 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005688}
5689#endif /* MBEDTLS_SSL_PROTO_DTLS */
5690
Hanno Becker1097b342018-08-15 14:09:41 +01005691static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005692{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005693 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005694 * Consume last content-layer message and potentially
5695 * update in_msglen which keeps track of the contents'
5696 * consumption state.
5697 *
5698 * (1) Handshake messages:
5699 * Remove last handshake message, move content
5700 * and adapt in_msglen.
5701 *
5702 * (2) Alert messages:
5703 * Consume whole record content, in_msglen = 0.
5704 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005705 * (3) Change cipher spec:
5706 * Consume whole record content, in_msglen = 0.
5707 *
5708 * (4) Application data:
5709 * Don't do anything - the record layer provides
5710 * the application data as a stream transport
5711 * and consumes through mbedtls_ssl_read only.
5712 *
5713 */
5714
5715 /* Case (1): Handshake messages */
5716 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005717 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005718 /* Hard assertion to be sure that no application data
5719 * is in flight, as corrupting ssl->in_msglen during
5720 * ssl->in_offt != NULL is fatal. */
5721 if( ssl->in_offt != NULL )
5722 {
5723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5725 }
5726
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005727 /*
5728 * Get next Handshake message in the current record
5729 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005730
Hanno Becker4a810fb2017-05-24 16:27:30 +01005731 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005732 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005733 * current handshake content: If DTLS handshake
5734 * fragmentation is used, that's the fragment
5735 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005736 * size here is faulty and should be changed at
5737 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005738 * (2) While it doesn't seem to cause problems, one
5739 * has to be very careful not to assume that in_hslen
5740 * is always <= in_msglen in a sensible communication.
5741 * Again, it's wrong for DTLS handshake fragmentation.
5742 * The following check is therefore mandatory, and
5743 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005744 * Additionally, ssl->in_hslen might be arbitrarily out of
5745 * bounds after handling a DTLS message with an unexpected
5746 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005747 */
5748 if( ssl->in_hslen < ssl->in_msglen )
5749 {
5750 ssl->in_msglen -= ssl->in_hslen;
5751 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5752 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005753
Hanno Becker4a810fb2017-05-24 16:27:30 +01005754 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5755 ssl->in_msg, ssl->in_msglen );
5756 }
5757 else
5758 {
5759 ssl->in_msglen = 0;
5760 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005761
Hanno Becker4a810fb2017-05-24 16:27:30 +01005762 ssl->in_hslen = 0;
5763 }
5764 /* Case (4): Application data */
5765 else if( ssl->in_offt != NULL )
5766 {
5767 return( 0 );
5768 }
5769 /* Everything else (CCS & Alerts) */
5770 else
5771 {
5772 ssl->in_msglen = 0;
5773 }
5774
Hanno Becker1097b342018-08-15 14:09:41 +01005775 return( 0 );
5776}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005777
Hanno Beckere74d5562018-08-15 14:26:08 +01005778static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5779{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005780 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005781 return( 1 );
5782
5783 return( 0 );
5784}
5785
Hanno Becker5f066e72018-08-16 14:56:31 +01005786#if defined(MBEDTLS_SSL_PROTO_DTLS)
5787
5788static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5789{
5790 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5791 if( hs == NULL )
5792 return;
5793
Hanno Becker01315ea2018-08-21 17:22:17 +01005794 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005795 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005796 hs->buffering.total_bytes_buffered -=
5797 hs->buffering.future_record.len;
5798
5799 mbedtls_free( hs->buffering.future_record.data );
5800 hs->buffering.future_record.data = NULL;
5801 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005802}
5803
5804static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5805{
5806 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5807 unsigned char * rec;
5808 size_t rec_len;
5809 unsigned rec_epoch;
5810
5811 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5812 return( 0 );
5813
5814 if( hs == NULL )
5815 return( 0 );
5816
Hanno Becker5f066e72018-08-16 14:56:31 +01005817 rec = hs->buffering.future_record.data;
5818 rec_len = hs->buffering.future_record.len;
5819 rec_epoch = hs->buffering.future_record.epoch;
5820
5821 if( rec == NULL )
5822 return( 0 );
5823
Hanno Becker4cb782d2018-08-20 11:19:05 +01005824 /* Only consider loading future records if the
5825 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005826 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005827 return( 0 );
5828
Hanno Becker5f066e72018-08-16 14:56:31 +01005829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5830
5831 if( rec_epoch != ssl->in_epoch )
5832 {
5833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5834 goto exit;
5835 }
5836
5837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5838
5839 /* Double-check that the record is not too large */
5840 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5841 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5842 {
5843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5845 }
5846
5847 memcpy( ssl->in_hdr, rec, rec_len );
5848 ssl->in_left = rec_len;
5849 ssl->next_record_offset = 0;
5850
5851 ssl_free_buffered_record( ssl );
5852
5853exit:
5854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5855 return( 0 );
5856}
5857
5858static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5859{
5860 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5861 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005862 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005863
5864 /* Don't buffer future records outside handshakes. */
5865 if( hs == NULL )
5866 return( 0 );
5867
5868 /* Only buffer handshake records (we are only interested
5869 * in Finished messages). */
5870 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5871 return( 0 );
5872
5873 /* Don't buffer more than one future epoch record. */
5874 if( hs->buffering.future_record.data != NULL )
5875 return( 0 );
5876
Hanno Becker01315ea2018-08-21 17:22:17 +01005877 /* Don't buffer record if there's not enough buffering space remaining. */
5878 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5879 hs->buffering.total_bytes_buffered ) )
5880 {
5881 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",
5882 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5883 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005884 return( 0 );
5885 }
5886
Hanno Becker5f066e72018-08-16 14:56:31 +01005887 /* Buffer record */
5888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5889 ssl->in_epoch + 1 ) );
5890 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5891 rec_hdr_len + ssl->in_msglen );
5892
5893 /* ssl_parse_record_header() only considers records
5894 * of the next epoch as candidates for buffering. */
5895 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005896 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005897
5898 hs->buffering.future_record.data =
5899 mbedtls_calloc( 1, hs->buffering.future_record.len );
5900 if( hs->buffering.future_record.data == NULL )
5901 {
5902 /* If we run out of RAM trying to buffer a
5903 * record from the next epoch, just ignore. */
5904 return( 0 );
5905 }
5906
Hanno Becker01315ea2018-08-21 17:22:17 +01005907 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005908
Hanno Becker01315ea2018-08-21 17:22:17 +01005909 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005910 return( 0 );
5911}
5912
5913#endif /* MBEDTLS_SSL_PROTO_DTLS */
5914
Hanno Beckere74d5562018-08-15 14:26:08 +01005915static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005916{
5917 int ret;
5918
Hanno Becker5f066e72018-08-16 14:56:31 +01005919#if defined(MBEDTLS_SSL_PROTO_DTLS)
5920 /* We might have buffered a future record; if so,
5921 * and if the epoch matches now, load it.
5922 * On success, this call will set ssl->in_left to
5923 * the length of the buffered record, so that
5924 * the calls to ssl_fetch_input() below will
5925 * essentially be no-ops. */
5926 ret = ssl_load_buffered_record( ssl );
5927 if( ret != 0 )
5928 return( ret );
5929#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005930
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005931 /* Reset in pointers to default state for TLS/DTLS records,
5932 * assuming no CID and no offset between record content and
5933 * record plaintext. */
5934 ssl_update_in_pointers( ssl );
5935
5936 /* Ensure that we have enough space available for the default form
5937 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5938 * with no space for CIDs counted in). */
5939 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5940 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005943 return( ret );
5944 }
5945
5946 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005949 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5950 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005951 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005952 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5953 {
5954 ret = ssl_buffer_future_record( ssl );
5955 if( ret != 0 )
5956 return( ret );
5957
5958 /* Fall through to handling of unexpected records */
5959 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5960 }
5961
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005962 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5963 {
5964 /* Skip unexpected record (but not whole datagram) */
5965 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005966 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005967
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5969 "(header)" ) );
5970 }
5971 else
5972 {
5973 /* Skip invalid record and the rest of the datagram */
5974 ssl->next_record_offset = 0;
5975 ssl->in_left = 0;
5976
5977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5978 "(header)" ) );
5979 }
5980
5981 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005982 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005983 }
5984#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005985 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005986 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005987
5988 /*
5989 * Read and optionally decrypt the message contents
5990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005992 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005995 return( ret );
5996 }
5997
5998 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006000 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006001 {
Hanno Becker5903de42019-05-03 14:46:38 +01006002 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01006003 if( ssl->next_record_offset < ssl->in_left )
6004 {
6005 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6006 }
6007 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006008 else
6009#endif
6010 ssl->in_left = 0;
6011
6012 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006015 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006016 {
6017 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006018 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006019 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006020 /* Except when waiting for Finished as a bad mac here
6021 * probably means something went wrong in the handshake
6022 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6023 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6024 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6025 {
6026#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6027 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6028 {
6029 mbedtls_ssl_send_alert_message( ssl,
6030 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6031 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6032 }
6033#endif
6034 return( ret );
6035 }
6036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006038 if( ssl->conf->badmac_limit != 0 &&
6039 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6042 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006043 }
6044#endif
6045
Hanno Becker4a810fb2017-05-24 16:27:30 +01006046 /* As above, invalid records cause
6047 * dismissal of the whole datagram. */
6048
6049 ssl->next_record_offset = 0;
6050 ssl->in_left = 0;
6051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006053 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006054 }
6055
6056 return( ret );
6057 }
6058 else
6059#endif
6060 {
6061 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6063 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 mbedtls_ssl_send_alert_message( ssl,
6066 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6067 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006068 }
6069#endif
6070 return( ret );
6071 }
6072 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006073
Simon Butcher99000142016-10-13 17:21:01 +01006074 return( 0 );
6075}
6076
6077int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6078{
6079 int ret;
6080
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006081 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006082 * Handle particular types of records
6083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006085 {
Simon Butcher99000142016-10-13 17:21:01 +01006086 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6087 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006088 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006089 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006090 }
6091
Hanno Beckere678eaa2018-08-21 14:57:46 +01006092 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006093 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006094 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006095 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6097 ssl->in_msglen ) );
6098 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006099 }
6100
Hanno Beckere678eaa2018-08-21 14:57:46 +01006101 if( ssl->in_msg[0] != 1 )
6102 {
6103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6104 ssl->in_msg[0] ) );
6105 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6106 }
6107
6108#if defined(MBEDTLS_SSL_PROTO_DTLS)
6109 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6110 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6111 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6112 {
6113 if( ssl->handshake == NULL )
6114 {
6115 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6116 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6117 }
6118
6119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6120 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6121 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006122#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006123 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006127 if( ssl->in_msglen != 2 )
6128 {
6129 /* Note: Standard allows for more than one 2 byte alert
6130 to be packed in a single message, but Mbed TLS doesn't
6131 currently support this. */
6132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6133 ssl->in_msglen ) );
6134 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6135 }
6136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006138 ssl->in_msg[0], ssl->in_msg[1] ) );
6139
6140 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006141 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006146 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006148 }
6149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6151 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6154 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006156
6157#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6158 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6159 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6160 {
Hanno Becker90333da2017-10-10 11:27:13 +01006161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006162 /* Will be handled when trying to parse ServerHello */
6163 return( 0 );
6164 }
6165#endif
6166
6167#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6168 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6169 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6170 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6171 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6172 {
6173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6174 /* Will be handled in mbedtls_ssl_parse_certificate() */
6175 return( 0 );
6176 }
6177#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6178
6179 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006180 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006181 }
6182
Hanno Beckerc76c6192017-06-06 10:03:17 +01006183#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006185 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006186 /* Drop unexpected ApplicationData records,
6187 * except at the beginning of renegotiations */
6188 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6189 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6190#if defined(MBEDTLS_SSL_RENEGOTIATION)
6191 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6192 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006193#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006194 )
6195 {
6196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6197 return( MBEDTLS_ERR_SSL_NON_FATAL );
6198 }
6199
6200 if( ssl->handshake != NULL &&
6201 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6202 {
6203 ssl_handshake_wrapup_free_hs_transform( ssl );
6204 }
6205 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006206#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006207
Paul Bakker5121ce52009-01-03 21:22:43 +00006208 return( 0 );
6209}
6210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006212{
6213 int ret;
6214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6216 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6217 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006218 {
6219 return( ret );
6220 }
6221
6222 return( 0 );
6223}
6224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006226 unsigned char level,
6227 unsigned char message )
6228{
6229 int ret;
6230
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006231 if( ssl == NULL || ssl->conf == NULL )
6232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006238 ssl->out_msglen = 2;
6239 ssl->out_msg[0] = level;
6240 ssl->out_msg[1] = message;
6241
Hanno Becker67bc7c32018-08-06 11:33:50 +01006242 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006245 return( ret );
6246 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006248
6249 return( 0 );
6250}
6251
Hanno Beckerb9d44792019-02-08 07:19:04 +00006252#if defined(MBEDTLS_X509_CRT_PARSE_C)
6253static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6254{
6255#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6256 if( session->peer_cert != NULL )
6257 {
6258 mbedtls_x509_crt_free( session->peer_cert );
6259 mbedtls_free( session->peer_cert );
6260 session->peer_cert = NULL;
6261 }
6262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6263 if( session->peer_cert_digest != NULL )
6264 {
6265 /* Zeroization is not necessary. */
6266 mbedtls_free( session->peer_cert_digest );
6267 session->peer_cert_digest = NULL;
6268 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6269 session->peer_cert_digest_len = 0;
6270 }
6271#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6272}
6273#endif /* MBEDTLS_X509_CRT_PARSE_C */
6274
Paul Bakker5121ce52009-01-03 21:22:43 +00006275/*
6276 * Handshake functions
6277 */
Hanno Becker21489932019-02-05 13:20:55 +00006278#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006279/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006281{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006282 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6283 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006286
Hanno Becker7177a882019-02-05 13:36:46 +00006287 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006290 ssl->state++;
6291 return( 0 );
6292 }
6293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6295 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006296}
6297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006299{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006300 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6301 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006304
Hanno Becker7177a882019-02-05 13:36:46 +00006305 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006308 ssl->state++;
6309 return( 0 );
6310 }
6311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6313 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006314}
Gilles Peskinef9828522017-05-03 12:28:43 +02006315
Hanno Becker21489932019-02-05 13:20:55 +00006316#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006317/* Some certificate support -> implement write and parse */
6318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006320{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006322 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006324 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6325 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006328
Hanno Becker7177a882019-02-05 13:36:46 +00006329 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006332 ssl->state++;
6333 return( 0 );
6334 }
6335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006337 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 {
6339 if( ssl->client_auth == 0 )
6340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006342 ssl->state++;
6343 return( 0 );
6344 }
6345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 /*
6348 * If using SSLv3 and got no cert, send an Alert message
6349 * (otherwise an empty Certificate message will be sent).
6350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6352 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 {
6354 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6356 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6357 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 goto write_msg;
6361 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364#endif /* MBEDTLS_SSL_CLI_C */
6365#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006366 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6371 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 }
6373 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006377
6378 /*
6379 * 0 . 0 handshake type
6380 * 1 . 3 handshake length
6381 * 4 . 6 length of all certs
6382 * 7 . 9 length of cert. 1
6383 * 10 . n-1 peer certificate
6384 * n . n+2 length of cert. 2
6385 * n+3 . ... upper level cert, etc.
6386 */
6387 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006389
Paul Bakker29087132010-03-21 21:03:34 +00006390 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 {
6392 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006393 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006396 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006398 }
6399
6400 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6401 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6402 ssl->out_msg[i + 2] = (unsigned char)( n );
6403
6404 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6405 i += n; crt = crt->next;
6406 }
6407
6408 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6409 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6410 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6411
6412 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6414 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006415
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006416#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006417write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006418#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006419
6420 ssl->state++;
6421
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006422 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006423 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006425 return( ret );
6426 }
6427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006429
Paul Bakkered27a042013-04-18 22:46:23 +02006430 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006431}
6432
Hanno Becker84879e32019-01-31 07:44:03 +00006433#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006434
6435#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006436static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6437 unsigned char *crt_buf,
6438 size_t crt_buf_len )
6439{
6440 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6441
6442 if( peer_crt == NULL )
6443 return( -1 );
6444
6445 if( peer_crt->raw.len != crt_buf_len )
6446 return( -1 );
6447
Hanno Becker46f34d02019-02-08 14:00:04 +00006448 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006449}
Hanno Becker177475a2019-02-05 17:02:46 +00006450#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6451static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6452 unsigned char *crt_buf,
6453 size_t crt_buf_len )
6454{
6455 int ret;
6456 unsigned char const * const peer_cert_digest =
6457 ssl->session->peer_cert_digest;
6458 mbedtls_md_type_t const peer_cert_digest_type =
6459 ssl->session->peer_cert_digest_type;
6460 mbedtls_md_info_t const * const digest_info =
6461 mbedtls_md_info_from_type( peer_cert_digest_type );
6462 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6463 size_t digest_len;
6464
6465 if( peer_cert_digest == NULL || digest_info == NULL )
6466 return( -1 );
6467
6468 digest_len = mbedtls_md_get_size( digest_info );
6469 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6470 return( -1 );
6471
6472 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6473 if( ret != 0 )
6474 return( -1 );
6475
6476 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6477}
6478#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006479#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006480
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006481/*
6482 * Once the certificate message is read, parse it into a cert chain and
6483 * perform basic checks, but leave actual verification to the caller
6484 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006485static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6486 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006487{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006488 int ret;
6489#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6490 int crt_cnt=0;
6491#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006492 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006493 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
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_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006501 }
6502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6504 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6508 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006510 }
6511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006513
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006516 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006517 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006518
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006519 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006520 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006523 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6524 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006526 }
6527
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006528 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6529 i += 3;
6530
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006531 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006532 while( i < ssl->in_hslen )
6533 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006534 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006535 if ( i + 3 > ssl->in_hslen ) {
6536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006537 mbedtls_ssl_send_alert_message( ssl,
6538 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6539 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006540 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6541 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006542 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6543 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 if( ssl->in_msg[i] != 0 )
6545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006547 mbedtls_ssl_send_alert_message( ssl,
6548 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6549 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 }
6552
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006553 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006554 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6555 | (unsigned int) ssl->in_msg[i + 2];
6556 i += 3;
6557
6558 if( n < 128 || i + n > ssl->in_hslen )
6559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006561 mbedtls_ssl_send_alert_message( ssl,
6562 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6563 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006565 }
6566
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006567 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006568#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6569 if( crt_cnt++ == 0 &&
6570 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6571 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006572 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006573 /* During client-side renegotiation, check that the server's
6574 * end-CRTs hasn't changed compared to the initial handshake,
6575 * mitigating the triple handshake attack. On success, reuse
6576 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006577 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6578 if( ssl_check_peer_crt_unchanged( ssl,
6579 &ssl->in_msg[i],
6580 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006581 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6583 mbedtls_ssl_send_alert_message( ssl,
6584 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6585 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6586 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006587 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006588
6589 /* Now we can safely free the original chain. */
6590 ssl_clear_peer_cert( ssl->session );
6591 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006592#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6593
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006594 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006595#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006596 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006597#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006598 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006599 * it in-place from the input buffer instead of making a copy. */
6600 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6601#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006602 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006604 case 0: /*ok*/
6605 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6606 /* Ignore certificate with an unknown algorithm: maybe a
6607 prior certificate was already trusted. */
6608 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006609
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006610 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6611 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6612 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006613
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006614 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6615 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6616 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006617
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006618 default:
6619 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6620 crt_parse_der_failed:
6621 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6622 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6623 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006624 }
6625
6626 i += n;
6627 }
6628
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006629 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006630 return( 0 );
6631}
6632
Hanno Becker4a55f632019-02-05 12:49:06 +00006633#if defined(MBEDTLS_SSL_SRV_C)
6634static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6635{
6636 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6637 return( -1 );
6638
6639#if defined(MBEDTLS_SSL_PROTO_SSL3)
6640 /*
6641 * Check if the client sent an empty certificate
6642 */
6643 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6644 {
6645 if( ssl->in_msglen == 2 &&
6646 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6647 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6648 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6649 {
6650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6651 return( 0 );
6652 }
6653
6654 return( -1 );
6655 }
6656#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6657
6658#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6659 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6660 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6661 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6662 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6663 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6664 {
6665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6666 return( 0 );
6667 }
6668
6669 return( -1 );
6670#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6671 MBEDTLS_SSL_PROTO_TLS1_2 */
6672}
6673#endif /* MBEDTLS_SSL_SRV_C */
6674
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006675/* Check if a certificate message is expected.
6676 * Return either
6677 * - SSL_CERTIFICATE_EXPECTED, or
6678 * - SSL_CERTIFICATE_SKIP
6679 * indicating whether a Certificate message is expected or not.
6680 */
6681#define SSL_CERTIFICATE_EXPECTED 0
6682#define SSL_CERTIFICATE_SKIP 1
6683static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6684 int authmode )
6685{
6686 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006687 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006688
6689 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6690 return( SSL_CERTIFICATE_SKIP );
6691
6692#if defined(MBEDTLS_SSL_SRV_C)
6693 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6694 {
6695 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6696 return( SSL_CERTIFICATE_SKIP );
6697
6698 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6699 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006700 ssl->session_negotiate->verify_result =
6701 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6702 return( SSL_CERTIFICATE_SKIP );
6703 }
6704 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006705#else
6706 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006707#endif /* MBEDTLS_SSL_SRV_C */
6708
6709 return( SSL_CERTIFICATE_EXPECTED );
6710}
6711
Hanno Becker68636192019-02-05 14:36:34 +00006712static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6713 int authmode,
6714 mbedtls_x509_crt *chain,
6715 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006716{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006717 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006718 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006719 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006720 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006721
Hanno Becker8927c832019-04-03 12:52:50 +01006722 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6723 void *p_vrfy;
6724
Hanno Becker68636192019-02-05 14:36:34 +00006725 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6726 return( 0 );
6727
Hanno Becker8927c832019-04-03 12:52:50 +01006728 if( ssl->f_vrfy != NULL )
6729 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006731 f_vrfy = ssl->f_vrfy;
6732 p_vrfy = ssl->p_vrfy;
6733 }
6734 else
6735 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006737 f_vrfy = ssl->conf->f_vrfy;
6738 p_vrfy = ssl->conf->p_vrfy;
6739 }
6740
Hanno Becker68636192019-02-05 14:36:34 +00006741 /*
6742 * Main check: verify certificate
6743 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006744#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6745 if( ssl->conf->f_ca_cb != NULL )
6746 {
6747 ((void) rs_ctx);
6748 have_ca_chain = 1;
6749
6750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006751 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006752 chain,
6753 ssl->conf->f_ca_cb,
6754 ssl->conf->p_ca_cb,
6755 ssl->conf->cert_profile,
6756 ssl->hostname,
6757 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006758 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006759 }
6760 else
6761#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6762 {
6763 mbedtls_x509_crt *ca_chain;
6764 mbedtls_x509_crl *ca_crl;
6765
6766#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6767 if( ssl->handshake->sni_ca_chain != NULL )
6768 {
6769 ca_chain = ssl->handshake->sni_ca_chain;
6770 ca_crl = ssl->handshake->sni_ca_crl;
6771 }
6772 else
6773#endif
6774 {
6775 ca_chain = ssl->conf->ca_chain;
6776 ca_crl = ssl->conf->ca_crl;
6777 }
6778
6779 if( ca_chain != NULL )
6780 have_ca_chain = 1;
6781
6782 ret = mbedtls_x509_crt_verify_restartable(
6783 chain,
6784 ca_chain, ca_crl,
6785 ssl->conf->cert_profile,
6786 ssl->hostname,
6787 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006788 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006789 }
Hanno Becker68636192019-02-05 14:36:34 +00006790
6791 if( ret != 0 )
6792 {
6793 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6794 }
6795
6796#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6797 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6798 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6799#endif
6800
6801 /*
6802 * Secondary checks: always done, but change 'ret' only if it was 0
6803 */
6804
6805#if defined(MBEDTLS_ECP_C)
6806 {
6807 const mbedtls_pk_context *pk = &chain->pk;
6808
6809 /* If certificate uses an EC key, make sure the curve is OK */
6810 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6811 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6812 {
6813 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6814
6815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6816 if( ret == 0 )
6817 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6818 }
6819 }
6820#endif /* MBEDTLS_ECP_C */
6821
6822 if( mbedtls_ssl_check_cert_usage( chain,
6823 ciphersuite_info,
6824 ! ssl->conf->endpoint,
6825 &ssl->session_negotiate->verify_result ) != 0 )
6826 {
6827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6828 if( ret == 0 )
6829 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6830 }
6831
6832 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6833 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6834 * with details encoded in the verification flags. All other kinds
6835 * of error codes, including those from the user provided f_vrfy
6836 * functions, are treated as fatal and lead to a failure of
6837 * ssl_parse_certificate even if verification was optional. */
6838 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6839 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6840 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6841 {
6842 ret = 0;
6843 }
6844
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006845 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006846 {
6847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6848 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6849 }
6850
6851 if( ret != 0 )
6852 {
6853 uint8_t alert;
6854
6855 /* The certificate may have been rejected for several reasons.
6856 Pick one and send the corresponding alert. Which alert to send
6857 may be a subject of debate in some cases. */
6858 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6859 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6860 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6861 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6862 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6863 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6864 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6865 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6866 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6867 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6868 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6869 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6870 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6871 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6872 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6873 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6874 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6875 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6876 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6877 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6878 else
6879 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6880 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6881 alert );
6882 }
6883
6884#if defined(MBEDTLS_DEBUG_C)
6885 if( ssl->session_negotiate->verify_result != 0 )
6886 {
6887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6888 ssl->session_negotiate->verify_result ) );
6889 }
6890 else
6891 {
6892 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6893 }
6894#endif /* MBEDTLS_DEBUG_C */
6895
6896 return( ret );
6897}
6898
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006899#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6900static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6901 unsigned char *start, size_t len )
6902{
6903 int ret;
6904 /* Remember digest of the peer's end-CRT. */
6905 ssl->session_negotiate->peer_cert_digest =
6906 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6907 if( ssl->session_negotiate->peer_cert_digest == NULL )
6908 {
6909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6910 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6911 mbedtls_ssl_send_alert_message( ssl,
6912 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6913 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6914
6915 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6916 }
6917
6918 ret = mbedtls_md( mbedtls_md_info_from_type(
6919 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6920 start, len,
6921 ssl->session_negotiate->peer_cert_digest );
6922
6923 ssl->session_negotiate->peer_cert_digest_type =
6924 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6925 ssl->session_negotiate->peer_cert_digest_len =
6926 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6927
6928 return( ret );
6929}
6930
6931static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6932 unsigned char *start, size_t len )
6933{
6934 unsigned char *end = start + len;
6935 int ret;
6936
6937 /* Make a copy of the peer's raw public key. */
6938 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6939 ret = mbedtls_pk_parse_subpubkey( &start, end,
6940 &ssl->handshake->peer_pubkey );
6941 if( ret != 0 )
6942 {
6943 /* We should have parsed the public key before. */
6944 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6945 }
6946
6947 return( 0 );
6948}
6949#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6950
Hanno Becker68636192019-02-05 14:36:34 +00006951int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6952{
6953 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006954 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006955#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6956 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6957 ? ssl->handshake->sni_authmode
6958 : ssl->conf->authmode;
6959#else
6960 const int authmode = ssl->conf->authmode;
6961#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006962 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006963 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006964
6965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6966
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006967 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6968 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006969 {
6970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006971 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006972 }
6973
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006974#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6975 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006976 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006977 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006978 chain = ssl->handshake->ecrs_peer_cert;
6979 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006980 goto crt_verify;
6981 }
6982#endif
6983
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006984 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006985 {
6986 /* mbedtls_ssl_read_record may have sent an alert already. We
6987 let it decide whether to alert. */
6988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006989 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006990 }
6991
Hanno Becker4a55f632019-02-05 12:49:06 +00006992#if defined(MBEDTLS_SSL_SRV_C)
6993 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6994 {
6995 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006996
6997 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006998 ret = 0;
6999 else
7000 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007001
Hanno Becker6bdfab22019-02-05 13:11:17 +00007002 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007003 }
7004#endif /* MBEDTLS_SSL_SRV_C */
7005
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007006 /* Clear existing peer CRT structure in case we tried to
7007 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007008 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007009
7010 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7011 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007012 {
7013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7014 sizeof( mbedtls_x509_crt ) ) );
7015 mbedtls_ssl_send_alert_message( ssl,
7016 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7017 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007018
Hanno Becker3dad3112019-02-05 17:19:52 +00007019 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7020 goto exit;
7021 }
7022 mbedtls_x509_crt_init( chain );
7023
7024 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007025 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007026 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007027
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007028#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7029 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007030 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007031
7032crt_verify:
7033 if( ssl->handshake->ecrs_enabled)
7034 rs_ctx = &ssl->handshake->ecrs_ctx;
7035#endif
7036
Hanno Becker68636192019-02-05 14:36:34 +00007037 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007038 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007039 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007040 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007041
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007042#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007043 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007044 unsigned char *crt_start, *pk_start;
7045 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007046
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007047 /* We parse the CRT chain without copying, so
7048 * these pointers point into the input buffer,
7049 * and are hence still valid after freeing the
7050 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007051
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007052 crt_start = chain->raw.p;
7053 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007054
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007055 pk_start = chain->pk_raw.p;
7056 pk_len = chain->pk_raw.len;
7057
7058 /* Free the CRT structures before computing
7059 * digest and copying the peer's public key. */
7060 mbedtls_x509_crt_free( chain );
7061 mbedtls_free( chain );
7062 chain = NULL;
7063
7064 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007065 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007066 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007067
7068 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7069 if( ret != 0 )
7070 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007071 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007072#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7073 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007074 ssl->session_negotiate->peer_cert = chain;
7075 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007076#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007079
Hanno Becker6bdfab22019-02-05 13:11:17 +00007080exit:
7081
Hanno Becker3dad3112019-02-05 17:19:52 +00007082 if( ret == 0 )
7083 ssl->state++;
7084
7085#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7086 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7087 {
7088 ssl->handshake->ecrs_peer_cert = chain;
7089 chain = NULL;
7090 }
7091#endif
7092
7093 if( chain != NULL )
7094 {
7095 mbedtls_x509_crt_free( chain );
7096 mbedtls_free( chain );
7097 }
7098
Paul Bakker5121ce52009-01-03 21:22:43 +00007099 return( ret );
7100}
Hanno Becker21489932019-02-05 13:20:55 +00007101#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007104{
7105 int ret;
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007110 ssl->out_msglen = 1;
7111 ssl->out_msg[0] = 1;
7112
Paul Bakker5121ce52009-01-03 21:22:43 +00007113 ssl->state++;
7114
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007115 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007116 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007118 return( ret );
7119 }
7120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007122
7123 return( 0 );
7124}
7125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007127{
7128 int ret;
7129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007131
Hanno Becker327c93b2018-08-15 13:56:18 +01007132 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007135 return( ret );
7136 }
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007141 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7142 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007144 }
7145
Hanno Beckere678eaa2018-08-21 14:57:46 +01007146 /* CCS records are only accepted if they have length 1 and content '1',
7147 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007148
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007149 /*
7150 * Switch to our negotiated transform and session parameters for inbound
7151 * data.
7152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007154 ssl->transform_in = ssl->transform_negotiate;
7155 ssl->session_in = ssl->session_negotiate;
7156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007161 ssl_dtls_replay_reset( ssl );
7162#endif
7163
7164 /* Increment epoch */
7165 if( ++ssl->in_epoch == 0 )
7166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007168 /* This is highly unlikely to happen for legitimate reasons, so
7169 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007171 }
7172 }
7173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007175 memset( ssl->in_ctr, 0, 8 );
7176
Hanno Becker79594fd2019-05-08 09:38:41 +01007177 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7180 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7186 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007188 }
7189 }
7190#endif
7191
Paul Bakker5121ce52009-01-03 21:22:43 +00007192 ssl->state++;
7193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007195
7196 return( 0 );
7197}
7198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7200 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007201{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007202 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7205 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7206 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007207 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007208 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007209#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7211#if defined(MBEDTLS_SHA512_C)
7212 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007213 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7214 else
7215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216#if defined(MBEDTLS_SHA256_C)
7217 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007218 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007219 else
7220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007224 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007225 }
Paul Bakker380da532012-04-18 16:10:25 +00007226}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7231 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007232 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7233 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7236#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007237#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007238 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007239 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7240#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007241 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007242#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007245#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007246 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007247 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007248#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007249 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007250#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007253}
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007256 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7259 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007260 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7261 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7264#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007265#if defined(MBEDTLS_USE_PSA_CRYPTO)
7266 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7267#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007268 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007269#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007272#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007273 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007274#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007275 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007276#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007277#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007279}
7280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7282 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7283static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007284 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007285{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007286 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7287 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007288}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007289#endif
Paul Bakker380da532012-04-18 16:10:25 +00007290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7292#if defined(MBEDTLS_SHA256_C)
7293static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007294 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007295{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007296#if defined(MBEDTLS_USE_PSA_CRYPTO)
7297 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7298#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007299 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007300#endif
Paul Bakker380da532012-04-18 16:10:25 +00007301}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007302#endif
Paul Bakker380da532012-04-18 16:10:25 +00007303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#if defined(MBEDTLS_SHA512_C)
7305static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007306 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007307{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007308#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007309 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007310#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007311 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007312#endif
Paul Bakker380da532012-04-18 16:10:25 +00007313}
Paul Bakker769075d2012-11-24 11:26:46 +01007314#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007318static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007320{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007321 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007322 mbedtls_md5_context md5;
7323 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007324
Paul Bakker5121ce52009-01-03 21:22:43 +00007325 unsigned char padbuf[48];
7326 unsigned char md5sum[16];
7327 unsigned char sha1sum[20];
7328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007330 if( !session )
7331 session = ssl->session;
7332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007334
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007335 mbedtls_md5_init( &md5 );
7336 mbedtls_sha1_init( &sha1 );
7337
7338 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7339 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007340
7341 /*
7342 * SSLv3:
7343 * hash =
7344 * MD5( master + pad2 +
7345 * MD5( handshake + sender + master + pad1 ) )
7346 * + SHA1( master + pad2 +
7347 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007348 */
7349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007351 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7352 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007353#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007356 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7357 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007358#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007361 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
Paul Bakker1ef83d62012-04-11 12:09:53 +00007363 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007364
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007365 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7366 mbedtls_md5_update_ret( &md5, session->master, 48 );
7367 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7368 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007369
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007370 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7371 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7372 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7373 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374
Paul Bakker1ef83d62012-04-11 12:09:53 +00007375 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007376
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007377 mbedtls_md5_starts_ret( &md5 );
7378 mbedtls_md5_update_ret( &md5, session->master, 48 );
7379 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7380 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7381 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007382
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007383 mbedtls_sha1_starts_ret( &sha1 );
7384 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7385 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7386 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7387 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007390
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007391 mbedtls_md5_free( &md5 );
7392 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007393
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007394 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7395 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7396 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007403static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007405{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007406 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007407 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007408 mbedtls_md5_context md5;
7409 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007410 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007413 if( !session )
7414 session = ssl->session;
7415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007417
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007418 mbedtls_md5_init( &md5 );
7419 mbedtls_sha1_init( &sha1 );
7420
7421 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7422 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007423
Paul Bakker1ef83d62012-04-11 12:09:53 +00007424 /*
7425 * TLSv1:
7426 * hash = PRF( master, finished_label,
7427 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7428 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007431 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7432 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007433#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007436 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7437 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007438#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007441 ? "client finished"
7442 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007443
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007444 mbedtls_md5_finish_ret( &md5, padbuf );
7445 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007446
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007447 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007448 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007451
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007452 mbedtls_md5_free( &md5 );
7453 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007454
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007455 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7462#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007463static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007465{
7466 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007467 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007468 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007469#if defined(MBEDTLS_USE_PSA_CRYPTO)
7470 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007471 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007472 psa_status_t status;
7473#else
7474 mbedtls_sha256_context sha256;
7475#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007478 if( !session )
7479 session = ssl->session;
7480
Andrzej Kurekeb342242019-01-29 09:14:33 -05007481 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7482 ? "client finished"
7483 : "server finished";
7484
7485#if defined(MBEDTLS_USE_PSA_CRYPTO)
7486 sha256_psa = psa_hash_operation_init();
7487
7488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7489
7490 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7491 if( status != PSA_SUCCESS )
7492 {
7493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7494 return;
7495 }
7496
7497 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7498 if( status != PSA_SUCCESS )
7499 {
7500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7501 return;
7502 }
7503 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7504#else
7505
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007506 mbedtls_sha256_init( &sha256 );
7507
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007509
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007510 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007511
7512 /*
7513 * TLSv1.2:
7514 * hash = PRF( master, finished_label,
7515 * Hash( handshake ) )[0.11]
7516 */
7517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#if !defined(MBEDTLS_SHA256_ALT)
7519 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007520 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007521#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007522
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007523 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007524 mbedtls_sha256_free( &sha256 );
7525#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007526
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007527 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007528 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007531
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007532 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007539static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007541{
7542 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007543 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007544 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007545#if defined(MBEDTLS_USE_PSA_CRYPTO)
7546 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007547 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007548 psa_status_t status;
7549#else
7550 mbedtls_sha512_context sha512;
7551#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007554 if( !session )
7555 session = ssl->session;
7556
Andrzej Kurekeb342242019-01-29 09:14:33 -05007557 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7558 ? "client finished"
7559 : "server finished";
7560
7561#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007562 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007563
7564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7565
Andrzej Kurek972fba52019-01-30 03:29:12 -05007566 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007567 if( status != PSA_SUCCESS )
7568 {
7569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7570 return;
7571 }
7572
Andrzej Kurek972fba52019-01-30 03:29:12 -05007573 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007574 if( status != PSA_SUCCESS )
7575 {
7576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7577 return;
7578 }
7579 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7580#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007581 mbedtls_sha512_init( &sha512 );
7582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007584
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007585 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007586
7587 /*
7588 * TLSv1.2:
7589 * hash = PRF( master, finished_label,
7590 * Hash( handshake ) )[0.11]
7591 */
7592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007594 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7595 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007596#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007597
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007598 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007599 mbedtls_sha512_free( &sha512 );
7600#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007601
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007602 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007603 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007606
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007607 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007610}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611#endif /* MBEDTLS_SHA512_C */
7612#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007617
7618 /*
7619 * Free our handshake params
7620 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007621 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007623 ssl->handshake = NULL;
7624
7625 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007626 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007627 */
7628 if( ssl->transform )
7629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_transform_free( ssl->transform );
7631 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007632 }
7633 ssl->transform = ssl->transform_negotiate;
7634 ssl->transform_negotiate = NULL;
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637}
7638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640{
7641 int resume = ssl->handshake->resume;
7642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#if defined(MBEDTLS_SSL_RENEGOTIATION)
7646 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007649 ssl->renego_records_seen = 0;
7650 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007651#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007652
7653 /*
7654 * Free the previous session and switch in the current one
7655 */
Paul Bakker0a597072012-09-25 21:55:46 +00007656 if( ssl->session )
7657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007659 /* RFC 7366 3.1: keep the EtM state */
7660 ssl->session_negotiate->encrypt_then_mac =
7661 ssl->session->encrypt_then_mac;
7662#endif
7663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 mbedtls_ssl_session_free( ssl->session );
7665 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007666 }
7667 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007668 ssl->session_negotiate = NULL;
7669
Paul Bakker0a597072012-09-25 21:55:46 +00007670 /*
7671 * Add cache entry
7672 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007673 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007674 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007675 resume == 0 )
7676 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007677 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007679 }
Paul Bakker0a597072012-09-25 21:55:46 +00007680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007682 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007683 ssl->handshake->flight != NULL )
7684 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007685 /* Cancel handshake timer */
7686 ssl_set_timer( ssl, 0 );
7687
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007688 /* Keep last flight around in case we need to resend it:
7689 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007691 }
7692 else
7693#endif
7694 ssl_handshake_wrapup_free_hs_transform( ssl );
7695
Paul Bakker48916f92012-09-16 19:57:18 +00007696 ssl->state++;
7697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007699}
7700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007702{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007703 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007706
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007707 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007708
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007709 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007710
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007711 /*
7712 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7713 * may define some other value. Currently (early 2016), no defined
7714 * ciphersuite does this (and this is unlikely to change as activity has
7715 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007720 ssl->verify_data_len = hash_len;
7721 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007722#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007723
Paul Bakker5121ce52009-01-03 21:22:43 +00007724 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7726 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007727
7728 /*
7729 * In case of session resuming, invert the client and server
7730 * ChangeCipherSpec messages order.
7731 */
Paul Bakker0a597072012-09-25 21:55:46 +00007732 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007735 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007737#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007739 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007741#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007742 }
7743 else
7744 ssl->state++;
7745
Paul Bakker48916f92012-09-16 19:57:18 +00007746 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007747 * Switch to our negotiated transform and session parameters for outbound
7748 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007753 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007754 {
7755 unsigned char i;
7756
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007757 /* Remember current epoch settings for resending */
7758 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007759 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007760
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007761 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007762 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007763
7764 /* Increment epoch */
7765 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007766 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007767 break;
7768
7769 /* The loop goes to its end iff the counter is wrapping */
7770 if( i == 0 )
7771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7773 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007774 }
7775 }
7776 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007778 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007779
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007780 ssl->transform_out = ssl->transform_negotiate;
7781 ssl->session_out = ssl->session_negotiate;
7782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7784 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7789 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007790 }
7791 }
7792#endif
7793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007795 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007796 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007797#endif
7798
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007799 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007800 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007801 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007802 return( ret );
7803 }
7804
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007805#if defined(MBEDTLS_SSL_PROTO_DTLS)
7806 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7807 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7808 {
7809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7810 return( ret );
7811 }
7812#endif
7813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007815
7816 return( 0 );
7817}
7818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007820#define SSL_MAX_HASH_LEN 36
7821#else
7822#define SSL_MAX_HASH_LEN 12
7823#endif
7824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007826{
Paul Bakker23986e52011-04-24 08:57:21 +00007827 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007828 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007829 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007832
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007833 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007834
Hanno Becker327c93b2018-08-15 13:56:18 +01007835 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007838 return( ret );
7839 }
7840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007844 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7845 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 }
7848
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007849 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850#if defined(MBEDTLS_SSL_PROTO_SSL3)
7851 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007852 hash_len = 36;
7853 else
7854#endif
7855 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7858 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
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( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007867 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007870 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7871 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007873 }
7874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007876 ssl->verify_data_len = hash_len;
7877 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007878#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007879
Paul Bakker0a597072012-09-25 21:55:46 +00007880 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007883 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007885#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007887 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007889#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007890 }
7891 else
7892 ssl->state++;
7893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007895 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007897#endif
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007900
7901 return( 0 );
7902}
7903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007904static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007905{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7909 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7910 mbedtls_md5_init( &handshake->fin_md5 );
7911 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007912 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7913 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7916#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007917#if defined(MBEDTLS_USE_PSA_CRYPTO)
7918 handshake->fin_sha256_psa = psa_hash_operation_init();
7919 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7920#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007922 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
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#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007926#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007927 handshake->fin_sha384_psa = psa_hash_operation_init();
7928 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007929#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007931 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007932#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007935
7936 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007937
7938#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7939 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7940 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7941#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943#if defined(MBEDTLS_DHM_C)
7944 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_ECDH_C)
7947 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007948#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007949#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007950 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007951#if defined(MBEDTLS_SSL_CLI_C)
7952 handshake->ecjpake_cache = NULL;
7953 handshake->ecjpake_cache_len = 0;
7954#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007955#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007956
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007957#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007958 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007959#endif
7960
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007961#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7962 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7963#endif
Hanno Becker75173122019-02-06 16:18:31 +00007964
7965#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7966 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7967 mbedtls_pk_init( &handshake->peer_pubkey );
7968#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007969}
7970
Hanno Beckera18d1322018-01-03 14:27:32 +00007971void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7976 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007977
Hanno Beckerd56ed242018-01-03 15:32:51 +00007978#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 mbedtls_md_init( &transform->md_ctx_enc );
7980 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007981#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982}
7983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007987}
7988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007990{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007991 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007992 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007994 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007997 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007998
7999 /*
8000 * Either the pointers are now NULL or cleared properly and can be freed.
8001 * Now allocate missing structures.
8002 */
8003 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008004 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008005 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008006 }
Paul Bakker48916f92012-09-16 19:57:18 +00008007
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008008 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008009 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008010 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008011 }
Paul Bakker48916f92012-09-16 19:57:18 +00008012
Paul Bakker82788fb2014-10-20 13:59:19 +02008013 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008014 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008015 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008016 }
Paul Bakker48916f92012-09-16 19:57:18 +00008017
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008018 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008019 if( ssl->handshake == NULL ||
8020 ssl->transform_negotiate == NULL ||
8021 ssl->session_negotiate == NULL )
8022 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 mbedtls_free( ssl->handshake );
8026 mbedtls_free( ssl->transform_negotiate );
8027 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008028
8029 ssl->handshake = NULL;
8030 ssl->transform_negotiate = NULL;
8031 ssl->session_negotiate = NULL;
8032
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008033 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008034 }
8035
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008036 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008038 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008039 ssl_handshake_params_init( ssl->handshake );
8040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008042 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8043 {
8044 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008045
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008046 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8047 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8048 else
8049 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008050
8051 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008052 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008053#endif
8054
Paul Bakker48916f92012-09-16 19:57:18 +00008055 return( 0 );
8056}
8057
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008058#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008059/* Dummy cookie callbacks for defaults */
8060static int ssl_cookie_write_dummy( void *ctx,
8061 unsigned char **p, unsigned char *end,
8062 const unsigned char *cli_id, size_t cli_id_len )
8063{
8064 ((void) ctx);
8065 ((void) p);
8066 ((void) end);
8067 ((void) cli_id);
8068 ((void) cli_id_len);
8069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008071}
8072
8073static int ssl_cookie_check_dummy( void *ctx,
8074 const unsigned char *cookie, size_t cookie_len,
8075 const unsigned char *cli_id, size_t cli_id_len )
8076{
8077 ((void) ctx);
8078 ((void) cookie);
8079 ((void) cookie_len);
8080 ((void) cli_id);
8081 ((void) cli_id_len);
8082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008084}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008085#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008086
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008087/* Once ssl->out_hdr as the address of the beginning of the
8088 * next outgoing record is set, deduce the other pointers.
8089 *
8090 * Note: For TLS, we save the implicit record sequence number
8091 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8092 * and the caller has to make sure there's space for this.
8093 */
8094
8095static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8096 mbedtls_ssl_transform *transform )
8097{
8098#if defined(MBEDTLS_SSL_PROTO_DTLS)
8099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8100 {
8101 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008102#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008103 ssl->out_cid = ssl->out_ctr + 8;
8104 ssl->out_len = ssl->out_cid;
8105 if( transform != NULL )
8106 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008107#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008108 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008109#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008110 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008111 }
8112 else
8113#endif
8114 {
8115 ssl->out_ctr = ssl->out_hdr - 8;
8116 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008118 ssl->out_cid = ssl->out_len;
8119#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008120 ssl->out_iv = ssl->out_hdr + 5;
8121 }
8122
8123 /* Adjust out_msg to make space for explicit IV, if used. */
8124 if( transform != NULL &&
8125 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8126 {
8127 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8128 }
8129 else
8130 ssl->out_msg = ssl->out_iv;
8131}
8132
8133/* Once ssl->in_hdr as the address of the beginning of the
8134 * next incoming record is set, deduce the other pointers.
8135 *
8136 * Note: For TLS, we save the implicit record sequence number
8137 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8138 * and the caller has to make sure there's space for this.
8139 */
8140
Hanno Becker79594fd2019-05-08 09:38:41 +01008141static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008142{
Hanno Becker79594fd2019-05-08 09:38:41 +01008143 /* This function sets the pointers to match the case
8144 * of unprotected TLS/DTLS records, with both ssl->in_iv
8145 * and ssl->in_msg pointing to the beginning of the record
8146 * content.
8147 *
8148 * When decrypting a protected record, ssl->in_msg
8149 * will be shifted to point to the beginning of the
8150 * record plaintext.
8151 */
8152
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008153#if defined(MBEDTLS_SSL_PROTO_DTLS)
8154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8155 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008156 /* This sets the header pointers to match records
8157 * without CID. When we receive a record containing
8158 * a CID, the fields are shifted accordingly in
8159 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008160 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008161#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008162 ssl->in_cid = ssl->in_ctr + 8;
8163 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008164#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008165 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008166#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008167 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168 }
8169 else
8170#endif
8171 {
8172 ssl->in_ctr = ssl->in_hdr - 8;
8173 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008174#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008175 ssl->in_cid = ssl->in_len;
8176#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008177 ssl->in_iv = ssl->in_hdr + 5;
8178 }
8179
Hanno Becker79594fd2019-05-08 09:38:41 +01008180 /* This will be adjusted at record decryption time. */
8181 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008182}
8183
Paul Bakker5121ce52009-01-03 21:22:43 +00008184/*
8185 * Initialize an SSL context
8186 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008187void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8188{
8189 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8190}
8191
8192/*
8193 * Setup an SSL context
8194 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008195
8196static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8197{
8198 /* Set the incoming and outgoing record pointers. */
8199#if defined(MBEDTLS_SSL_PROTO_DTLS)
8200 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8201 {
8202 ssl->out_hdr = ssl->out_buf;
8203 ssl->in_hdr = ssl->in_buf;
8204 }
8205 else
8206#endif /* MBEDTLS_SSL_PROTO_DTLS */
8207 {
8208 ssl->out_hdr = ssl->out_buf + 8;
8209 ssl->in_hdr = ssl->in_buf + 8;
8210 }
8211
8212 /* Derive other internal pointers. */
8213 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008214 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008215}
8216
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008217int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008218 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008219{
Paul Bakker48916f92012-09-16 19:57:18 +00008220 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008221
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008222 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008223
8224 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008225 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008226 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008227
8228 /* Set to NULL in case of an error condition */
8229 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008230
Angus Grattond8213d02016-05-25 20:56:48 +10008231 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8232 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008233 {
Angus Grattond8213d02016-05-25 20:56:48 +10008234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008235 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008236 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008237 }
8238
8239 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8240 if( ssl->out_buf == NULL )
8241 {
8242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008243 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008244 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008245 }
8246
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008247 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008248
Paul Bakker48916f92012-09-16 19:57:18 +00008249 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008250 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008251
8252 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008253
8254error:
8255 mbedtls_free( ssl->in_buf );
8256 mbedtls_free( ssl->out_buf );
8257
8258 ssl->conf = NULL;
8259
8260 ssl->in_buf = NULL;
8261 ssl->out_buf = NULL;
8262
8263 ssl->in_hdr = NULL;
8264 ssl->in_ctr = NULL;
8265 ssl->in_len = NULL;
8266 ssl->in_iv = NULL;
8267 ssl->in_msg = NULL;
8268
8269 ssl->out_hdr = NULL;
8270 ssl->out_ctr = NULL;
8271 ssl->out_len = NULL;
8272 ssl->out_iv = NULL;
8273 ssl->out_msg = NULL;
8274
k-stachowiak9f7798e2018-07-31 16:52:32 +02008275 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008276}
8277
8278/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008279 * Reset an initialized and used SSL context for re-use while retaining
8280 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008281 *
8282 * If partial is non-zero, keep data in the input buffer and client ID.
8283 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008284 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008285static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008286{
Paul Bakker48916f92012-09-16 19:57:18 +00008287 int ret;
8288
Hanno Becker7e772132018-08-10 12:38:21 +01008289#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8290 !defined(MBEDTLS_SSL_SRV_C)
8291 ((void) partial);
8292#endif
8293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008295
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008296 /* Cancel any possibly running timer */
8297 ssl_set_timer( ssl, 0 );
8298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#if defined(MBEDTLS_SSL_RENEGOTIATION)
8300 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008301 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008302
8303 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8305 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008306#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008308
Paul Bakker7eb013f2011-10-06 12:37:39 +00008309 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008310 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008311
8312 ssl->in_msgtype = 0;
8313 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008315 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008316 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008319 ssl_dtls_replay_reset( ssl );
8320#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008321
8322 ssl->in_hslen = 0;
8323 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008324
8325 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008326
8327 ssl->out_msgtype = 0;
8328 ssl->out_msglen = 0;
8329 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8331 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008332 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008333#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008334
Hanno Becker19859472018-08-06 09:40:20 +01008335 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8336
Paul Bakker48916f92012-09-16 19:57:18 +00008337 ssl->transform_in = NULL;
8338 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008339
Hanno Becker78640902018-08-13 16:35:15 +01008340 ssl->session_in = NULL;
8341 ssl->session_out = NULL;
8342
Angus Grattond8213d02016-05-25 20:56:48 +10008343 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008344
8345#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008346 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008347#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8348 {
8349 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008350 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008351 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8354 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8357 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8360 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008361 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008362 }
8363#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008364
Paul Bakker48916f92012-09-16 19:57:18 +00008365 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 mbedtls_ssl_transform_free( ssl->transform );
8368 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008369 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008370 }
Paul Bakker48916f92012-09-16 19:57:18 +00008371
Paul Bakkerc0463502013-02-14 11:19:38 +01008372 if( ssl->session )
8373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374 mbedtls_ssl_session_free( ssl->session );
8375 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008376 ssl->session = NULL;
8377 }
8378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008380 ssl->alpn_chosen = NULL;
8381#endif
8382
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008383#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008384#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008385 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008386#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008387 {
8388 mbedtls_free( ssl->cli_id );
8389 ssl->cli_id = NULL;
8390 ssl->cli_id_len = 0;
8391 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008392#endif
8393
Paul Bakker48916f92012-09-16 19:57:18 +00008394 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8395 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008396
8397 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008398}
8399
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008400/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008401 * Reset an initialized and used SSL context for re-use while retaining
8402 * all application-set variables, function pointers and data.
8403 */
8404int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8405{
8406 return( ssl_session_reset_int( ssl, 0 ) );
8407}
8408
8409/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008410 * SSL set accessors
8411 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008412void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008413{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008414 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008415}
8416
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008417void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008418{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008419 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008420}
8421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008423void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008424{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008425 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008426}
8427#endif
8428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008430void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008431{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008432 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008433}
8434#endif
8435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008437
Hanno Becker1841b0a2018-08-24 11:13:57 +01008438void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8439 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008440{
8441 ssl->disable_datagram_packing = !allow_packing;
8442}
8443
8444void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8445 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008447 conf->hs_timeout_min = min;
8448 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008449}
8450#endif
8451
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008452void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008453{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008454 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008455}
8456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008458void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008459 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008460 void *p_vrfy )
8461{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008462 conf->f_vrfy = f_vrfy;
8463 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008466
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008467void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008468 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008469 void *p_rng )
8470{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008471 conf->f_rng = f_rng;
8472 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008473}
8474
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008475void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008476 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008477 void *p_dbg )
8478{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008479 conf->f_dbg = f_dbg;
8480 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008481}
8482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008484 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008485 mbedtls_ssl_send_t *f_send,
8486 mbedtls_ssl_recv_t *f_recv,
8487 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008488{
8489 ssl->p_bio = p_bio;
8490 ssl->f_send = f_send;
8491 ssl->f_recv = f_recv;
8492 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008493}
8494
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008495#if defined(MBEDTLS_SSL_PROTO_DTLS)
8496void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8497{
8498 ssl->mtu = mtu;
8499}
8500#endif
8501
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008502void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008503{
8504 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008505}
8506
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008507void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8508 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008509 mbedtls_ssl_set_timer_t *f_set_timer,
8510 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008511{
8512 ssl->p_timer = p_timer;
8513 ssl->f_set_timer = f_set_timer;
8514 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008515
8516 /* Make sure we start with no timer running */
8517 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008518}
8519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008521void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008522 void *p_cache,
8523 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8524 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008525{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008526 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008527 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008528 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008532#if defined(MBEDTLS_SSL_CLI_C)
8533int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008534{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008535 int ret;
8536
8537 if( ssl == NULL ||
8538 session == NULL ||
8539 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008540 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008543 }
8544
Hanno Becker52055ae2019-02-06 14:30:46 +00008545 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8546 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008547 return( ret );
8548
Paul Bakker0a597072012-09-25 21:55:46 +00008549 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008550
8551 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008554
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008555void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008556 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008558 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8559 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8560 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8561 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008562}
8563
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008564void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008565 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008566 int major, int minor )
8567{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008568 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008569 return;
8570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008572 return;
8573
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008574 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008575}
8576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008577#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008578void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008579 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008580{
8581 conf->cert_profile = profile;
8582}
8583
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008584/* Append a new keycert entry to a (possibly empty) list */
8585static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8586 mbedtls_x509_crt *cert,
8587 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008588{
niisato8ee24222018-06-25 19:05:48 +09008589 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008590
niisato8ee24222018-06-25 19:05:48 +09008591 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8592 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008593 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008594
niisato8ee24222018-06-25 19:05:48 +09008595 new_cert->cert = cert;
8596 new_cert->key = key;
8597 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008598
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008599 /* Update head is the list was null, else add to the end */
8600 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008601 {
niisato8ee24222018-06-25 19:05:48 +09008602 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008603 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008604 else
8605 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008606 mbedtls_ssl_key_cert *cur = *head;
8607 while( cur->next != NULL )
8608 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008609 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008610 }
8611
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008612 return( 0 );
8613}
8614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008615int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008616 mbedtls_x509_crt *own_cert,
8617 mbedtls_pk_context *pk_key )
8618{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008619 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008620}
8621
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008622void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008623 mbedtls_x509_crt *ca_chain,
8624 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008625{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008626 conf->ca_chain = ca_chain;
8627 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008628
8629#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8630 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8631 * cannot be used together. */
8632 conf->f_ca_cb = NULL;
8633 conf->p_ca_cb = NULL;
8634#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008635}
Hanno Becker5adaad92019-03-27 16:54:37 +00008636
8637#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8638void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008639 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008640 void *p_ca_cb )
8641{
8642 conf->f_ca_cb = f_ca_cb;
8643 conf->p_ca_cb = p_ca_cb;
8644
8645 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8646 * cannot be used together. */
8647 conf->ca_chain = NULL;
8648 conf->ca_crl = NULL;
8649}
8650#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008651#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008652
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008653#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8654int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8655 mbedtls_x509_crt *own_cert,
8656 mbedtls_pk_context *pk_key )
8657{
8658 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8659 own_cert, pk_key ) );
8660}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008661
8662void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8663 mbedtls_x509_crt *ca_chain,
8664 mbedtls_x509_crl *ca_crl )
8665{
8666 ssl->handshake->sni_ca_chain = ca_chain;
8667 ssl->handshake->sni_ca_crl = ca_crl;
8668}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008669
8670void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8671 int authmode )
8672{
8673 ssl->handshake->sni_authmode = authmode;
8674}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008675#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8676
Hanno Becker8927c832019-04-03 12:52:50 +01008677#if defined(MBEDTLS_X509_CRT_PARSE_C)
8678void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8679 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8680 void *p_vrfy )
8681{
8682 ssl->f_vrfy = f_vrfy;
8683 ssl->p_vrfy = p_vrfy;
8684}
8685#endif
8686
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008687#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008688/*
8689 * Set EC J-PAKE password for current handshake
8690 */
8691int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8692 const unsigned char *pw,
8693 size_t pw_len )
8694{
8695 mbedtls_ecjpake_role role;
8696
Janos Follath8eb64132016-06-03 15:40:57 +01008697 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008698 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8699
8700 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8701 role = MBEDTLS_ECJPAKE_SERVER;
8702 else
8703 role = MBEDTLS_ECJPAKE_CLIENT;
8704
8705 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8706 role,
8707 MBEDTLS_MD_SHA256,
8708 MBEDTLS_ECP_DP_SECP256R1,
8709 pw, pw_len ) );
8710}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008711#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008713#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008714
8715static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8716{
8717 /* Remove reference to existing PSK, if any. */
8718#if defined(MBEDTLS_USE_PSA_CRYPTO)
8719 if( conf->psk_opaque != 0 )
8720 {
8721 /* The maintenance of the PSK key slot is the
8722 * user's responsibility. */
8723 conf->psk_opaque = 0;
8724 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008725 /* This and the following branch should never
8726 * be taken simultaenously as we maintain the
8727 * invariant that raw and opaque PSKs are never
8728 * configured simultaneously. As a safeguard,
8729 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008730#endif /* MBEDTLS_USE_PSA_CRYPTO */
8731 if( conf->psk != NULL )
8732 {
8733 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8734
8735 mbedtls_free( conf->psk );
8736 conf->psk = NULL;
8737 conf->psk_len = 0;
8738 }
8739
8740 /* Remove reference to PSK identity, if any. */
8741 if( conf->psk_identity != NULL )
8742 {
8743 mbedtls_free( conf->psk_identity );
8744 conf->psk_identity = NULL;
8745 conf->psk_identity_len = 0;
8746 }
8747}
8748
Hanno Becker7390c712018-11-15 13:33:04 +00008749/* This function assumes that PSK identity in the SSL config is unset.
8750 * It checks that the provided identity is well-formed and attempts
8751 * to make a copy of it in the SSL config.
8752 * On failure, the PSK identity in the config remains unset. */
8753static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8754 unsigned char const *psk_identity,
8755 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008756{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008757 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008758 if( psk_identity == NULL ||
8759 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008760 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008761 {
8762 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8763 }
8764
Hanno Becker7390c712018-11-15 13:33:04 +00008765 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8766 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008767 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008768
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008769 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008770 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008771
8772 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008773}
8774
Hanno Becker7390c712018-11-15 13:33:04 +00008775int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8776 const unsigned char *psk, size_t psk_len,
8777 const unsigned char *psk_identity, size_t psk_identity_len )
8778{
8779 int ret;
8780 /* Remove opaque/raw PSK + PSK Identity */
8781 ssl_conf_remove_psk( conf );
8782
8783 /* Check and set raw PSK */
8784 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8785 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8786 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8787 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8788 conf->psk_len = psk_len;
8789 memcpy( conf->psk, psk, conf->psk_len );
8790
8791 /* Check and set PSK Identity */
8792 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8793 if( ret != 0 )
8794 ssl_conf_remove_psk( conf );
8795
8796 return( ret );
8797}
8798
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008799static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8800{
8801#if defined(MBEDTLS_USE_PSA_CRYPTO)
8802 if( ssl->handshake->psk_opaque != 0 )
8803 {
8804 ssl->handshake->psk_opaque = 0;
8805 }
8806 else
8807#endif /* MBEDTLS_USE_PSA_CRYPTO */
8808 if( ssl->handshake->psk != NULL )
8809 {
8810 mbedtls_platform_zeroize( ssl->handshake->psk,
8811 ssl->handshake->psk_len );
8812 mbedtls_free( ssl->handshake->psk );
8813 ssl->handshake->psk_len = 0;
8814 }
8815}
8816
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008817int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8818 const unsigned char *psk, size_t psk_len )
8819{
8820 if( psk == NULL || ssl->handshake == NULL )
8821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8822
8823 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8825
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008826 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008827
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008828 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008829 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008830
8831 ssl->handshake->psk_len = psk_len;
8832 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8833
8834 return( 0 );
8835}
8836
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008837#if defined(MBEDTLS_USE_PSA_CRYPTO)
8838int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008839 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008840 const unsigned char *psk_identity,
8841 size_t psk_identity_len )
8842{
Hanno Becker7390c712018-11-15 13:33:04 +00008843 int ret;
8844 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008845 ssl_conf_remove_psk( conf );
8846
Hanno Becker7390c712018-11-15 13:33:04 +00008847 /* Check and set opaque PSK */
8848 if( psk_slot == 0 )
8849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008850 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008851
8852 /* Check and set PSK Identity */
8853 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8854 psk_identity_len );
8855 if( ret != 0 )
8856 ssl_conf_remove_psk( conf );
8857
8858 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008859}
8860
8861int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008862 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008863{
8864 if( psk_slot == 0 || ssl->handshake == NULL )
8865 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8866
8867 ssl_remove_psk( ssl );
8868 ssl->handshake->psk_opaque = psk_slot;
8869 return( 0 );
8870}
8871#endif /* MBEDTLS_USE_PSA_CRYPTO */
8872
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008873void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008875 size_t),
8876 void *p_psk )
8877{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008878 conf->f_psk = f_psk;
8879 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008882
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008883#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008884
8885#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008886int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008887{
8888 int ret;
8889
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008890 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8891 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8892 {
8893 mbedtls_mpi_free( &conf->dhm_P );
8894 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008895 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008896 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008897
8898 return( 0 );
8899}
Hanno Becker470a8c42017-10-04 15:28:46 +01008900#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008901
Hanno Beckera90658f2017-10-04 15:29:08 +01008902int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8903 const unsigned char *dhm_P, size_t P_len,
8904 const unsigned char *dhm_G, size_t G_len )
8905{
8906 int ret;
8907
8908 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8909 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8910 {
8911 mbedtls_mpi_free( &conf->dhm_P );
8912 mbedtls_mpi_free( &conf->dhm_G );
8913 return( ret );
8914 }
8915
8916 return( 0 );
8917}
Paul Bakker5121ce52009-01-03 21:22:43 +00008918
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008919int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008920{
8921 int ret;
8922
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008923 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8924 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8925 {
8926 mbedtls_mpi_free( &conf->dhm_P );
8927 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008928 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008929 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008930
8931 return( 0 );
8932}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008933#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008934
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008935#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8936/*
8937 * Set the minimum length for Diffie-Hellman parameters
8938 */
8939void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8940 unsigned int bitlen )
8941{
8942 conf->dhm_min_bitlen = bitlen;
8943}
8944#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8945
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008946#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008947/*
8948 * Set allowed/preferred hashes for handshake signatures
8949 */
8950void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8951 const int *hashes )
8952{
8953 conf->sig_hashes = hashes;
8954}
Hanno Becker947194e2017-04-07 13:25:49 +01008955#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008956
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008957#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008958/*
8959 * Set the allowed elliptic curves
8960 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008961void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008962 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008963{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008964 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008965}
Hanno Becker947194e2017-04-07 13:25:49 +01008966#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008967
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008968#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008969int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008970{
Hanno Becker947194e2017-04-07 13:25:49 +01008971 /* Initialize to suppress unnecessary compiler warning */
8972 size_t hostname_len = 0;
8973
8974 /* Check if new hostname is valid before
8975 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008976 if( hostname != NULL )
8977 {
8978 hostname_len = strlen( hostname );
8979
8980 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8981 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8982 }
8983
8984 /* Now it's clear that we will overwrite the old hostname,
8985 * so we can free it safely */
8986
8987 if( ssl->hostname != NULL )
8988 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008989 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008990 mbedtls_free( ssl->hostname );
8991 }
8992
8993 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008994
Paul Bakker5121ce52009-01-03 21:22:43 +00008995 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008996 {
8997 ssl->hostname = NULL;
8998 }
8999 else
9000 {
9001 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009002 if( ssl->hostname == NULL )
9003 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009004
Hanno Becker947194e2017-04-07 13:25:49 +01009005 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009006
Hanno Becker947194e2017-04-07 13:25:49 +01009007 ssl->hostname[hostname_len] = '\0';
9008 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009009
9010 return( 0 );
9011}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009012#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009013
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009014#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009015void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009017 const unsigned char *, size_t),
9018 void *p_sni )
9019{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009020 conf->f_sni = f_sni;
9021 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009025#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009026int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009027{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009028 size_t cur_len, tot_len;
9029 const char **p;
9030
9031 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009032 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9033 * MUST NOT be truncated."
9034 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009035 */
9036 tot_len = 0;
9037 for( p = protos; *p != NULL; p++ )
9038 {
9039 cur_len = strlen( *p );
9040 tot_len += cur_len;
9041
9042 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009044 }
9045
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009046 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009047
9048 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009049}
9050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009051const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009052{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009053 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009054}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009056
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009057void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009059 conf->max_major_ver = major;
9060 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009061}
9062
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009063void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009064{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009065 conf->min_major_ver = major;
9066 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009067}
9068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009070void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009071{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009072 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009073}
9074#endif
9075
Janos Follath088ce432017-04-10 12:42:31 +01009076#if defined(MBEDTLS_SSL_SRV_C)
9077void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9078 char cert_req_ca_list )
9079{
9080 conf->cert_req_ca_list = cert_req_ca_list;
9081}
9082#endif
9083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009085void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009087 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009088}
9089#endif
9090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009092void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009093{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009094 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009095}
9096#endif
9097
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009098#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009099void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009100{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009101 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009102}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009103#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009106int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009109 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009112 }
9113
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009114 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009115
9116 return( 0 );
9117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009121void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009123 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009128void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009129{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009130 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009131}
9132#endif
9133
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009134void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009136 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009137}
9138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009140void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009141{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009142 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009143}
9144
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009145void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009146{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009147 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009148}
9149
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009150void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009151 const unsigned char period[8] )
9152{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009153 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009158#if defined(MBEDTLS_SSL_CLI_C)
9159void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009160{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009161 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009162}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009163#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009164
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009165#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009166void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9167 mbedtls_ssl_ticket_write_t *f_ticket_write,
9168 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9169 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009170{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009171 conf->f_ticket_write = f_ticket_write;
9172 conf->f_ticket_parse = f_ticket_parse;
9173 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009174}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009177
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009178#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9179void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9180 mbedtls_ssl_export_keys_t *f_export_keys,
9181 void *p_export_keys )
9182{
9183 conf->f_export_keys = f_export_keys;
9184 conf->p_export_keys = p_export_keys;
9185}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009186
9187void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9188 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9189 void *p_export_keys )
9190{
9191 conf->f_export_keys_ext = f_export_keys_ext;
9192 conf->p_export_keys = p_export_keys;
9193}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009194#endif
9195
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009196#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009197void mbedtls_ssl_conf_async_private_cb(
9198 mbedtls_ssl_config *conf,
9199 mbedtls_ssl_async_sign_t *f_async_sign,
9200 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9201 mbedtls_ssl_async_resume_t *f_async_resume,
9202 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009203 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009204{
9205 conf->f_async_sign_start = f_async_sign;
9206 conf->f_async_decrypt_start = f_async_decrypt;
9207 conf->f_async_resume = f_async_resume;
9208 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009209 conf->p_async_config_data = async_config_data;
9210}
9211
Gilles Peskine8f97af72018-04-26 11:46:10 +02009212void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9213{
9214 return( conf->p_async_config_data );
9215}
9216
Gilles Peskine1febfef2018-04-30 11:54:39 +02009217void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009218{
9219 if( ssl->handshake == NULL )
9220 return( NULL );
9221 else
9222 return( ssl->handshake->user_async_ctx );
9223}
9224
Gilles Peskine1febfef2018-04-30 11:54:39 +02009225void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009226 void *ctx )
9227{
9228 if( ssl->handshake != NULL )
9229 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009230}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009231#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009232
Paul Bakker5121ce52009-01-03 21:22:43 +00009233/*
9234 * SSL get accessors
9235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009236size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009237{
9238 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9239}
9240
Hanno Becker8b170a02017-10-10 11:51:19 +01009241int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9242{
9243 /*
9244 * Case A: We're currently holding back
9245 * a message for further processing.
9246 */
9247
9248 if( ssl->keep_current_message == 1 )
9249 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009251 return( 1 );
9252 }
9253
9254 /*
9255 * Case B: Further records are pending in the current datagram.
9256 */
9257
9258#if defined(MBEDTLS_SSL_PROTO_DTLS)
9259 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9260 ssl->in_left > ssl->next_record_offset )
9261 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009263 return( 1 );
9264 }
9265#endif /* MBEDTLS_SSL_PROTO_DTLS */
9266
9267 /*
9268 * Case C: A handshake message is being processed.
9269 */
9270
Hanno Becker8b170a02017-10-10 11:51:19 +01009271 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9272 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009274 return( 1 );
9275 }
9276
9277 /*
9278 * Case D: An application data message is being processed
9279 */
9280 if( ssl->in_offt != NULL )
9281 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009283 return( 1 );
9284 }
9285
9286 /*
9287 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009288 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009289 * we implement support for multiple alerts in single records.
9290 */
9291
9292 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9293 return( 0 );
9294}
9295
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009296uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009297{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009298 if( ssl->session != NULL )
9299 return( ssl->session->verify_result );
9300
9301 if( ssl->session_negotiate != NULL )
9302 return( ssl->session_negotiate->verify_result );
9303
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009304 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009305}
9306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009308{
Paul Bakker926c8e42013-03-06 10:23:34 +01009309 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009310 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009313}
9314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009316{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009319 {
9320 switch( ssl->minor_ver )
9321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009323 return( "DTLSv1.0" );
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009326 return( "DTLSv1.2" );
9327
9328 default:
9329 return( "unknown (DTLS)" );
9330 }
9331 }
9332#endif
9333
Paul Bakker43ca69c2011-01-15 17:35:19 +00009334 switch( ssl->minor_ver )
9335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009337 return( "SSLv3.0" );
9338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009340 return( "TLSv1.0" );
9341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009343 return( "TLSv1.1" );
9344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009346 return( "TLSv1.2" );
9347
Paul Bakker43ca69c2011-01-15 17:35:19 +00009348 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009349 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009350 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009351}
9352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009354{
Hanno Becker3136ede2018-08-17 15:28:19 +01009355 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009357 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009358
Hanno Becker5903de42019-05-03 14:46:38 +01009359 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9360
Hanno Becker78640902018-08-13 16:35:15 +01009361 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009362 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364#if defined(MBEDTLS_ZLIB_SUPPORT)
9365 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9366 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009367#endif
9368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009369 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 case MBEDTLS_MODE_GCM:
9372 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009373 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009375 transform_expansion = transform->minlen;
9376 break;
9377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009378 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009379
9380 block_size = mbedtls_cipher_get_block_size(
9381 &transform->cipher_ctx_enc );
9382
Hanno Becker3136ede2018-08-17 15:28:19 +01009383 /* Expansion due to the addition of the MAC. */
9384 transform_expansion += transform->maclen;
9385
9386 /* Expansion due to the addition of CBC padding;
9387 * Theoretically up to 256 bytes, but we never use
9388 * more than the block size of the underlying cipher. */
9389 transform_expansion += block_size;
9390
9391 /* For TLS 1.1 or higher, an explicit IV is added
9392 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009393#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9394 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009395 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009396#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009397
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009398 break;
9399
9400 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009403 }
9404
Hanno Beckera0e20d02019-05-15 14:03:01 +01009405#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009406 if( transform->out_cid_len != 0 )
9407 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009408#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009409
Hanno Becker5903de42019-05-03 14:46:38 +01009410 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009411}
9412
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009413#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9414size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9415{
9416 size_t max_len;
9417
9418 /*
9419 * Assume mfl_code is correct since it was checked when set
9420 */
Angus Grattond8213d02016-05-25 20:56:48 +10009421 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009422
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009423 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009424 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009425 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009426 {
Angus Grattond8213d02016-05-25 20:56:48 +10009427 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009428 }
9429
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009430 /* During a handshake, use the value being negotiated */
9431 if( ssl->session_negotiate != NULL &&
9432 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9433 {
9434 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9435 }
9436
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009437 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009438}
9439#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9440
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009441#if defined(MBEDTLS_SSL_PROTO_DTLS)
9442static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9443{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009444 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9445 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9446 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9447 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9448 return ( 0 );
9449
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009450 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9451 return( ssl->mtu );
9452
9453 if( ssl->mtu == 0 )
9454 return( ssl->handshake->mtu );
9455
9456 return( ssl->mtu < ssl->handshake->mtu ?
9457 ssl->mtu : ssl->handshake->mtu );
9458}
9459#endif /* MBEDTLS_SSL_PROTO_DTLS */
9460
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009461int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9462{
9463 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9464
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009465#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9466 !defined(MBEDTLS_SSL_PROTO_DTLS)
9467 (void) ssl;
9468#endif
9469
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009470#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9471 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9472
9473 if( max_len > mfl )
9474 max_len = mfl;
9475#endif
9476
9477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009478 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009479 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009480 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009481 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9482 const size_t overhead = (size_t) ret;
9483
9484 if( ret < 0 )
9485 return( ret );
9486
9487 if( mtu <= overhead )
9488 {
9489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9490 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9491 }
9492
9493 if( max_len > mtu - overhead )
9494 max_len = mtu - overhead;
9495 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009496#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009497
Hanno Becker0defedb2018-08-10 12:35:02 +01009498#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9499 !defined(MBEDTLS_SSL_PROTO_DTLS)
9500 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009501#endif
9502
9503 return( (int) max_len );
9504}
9505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#if defined(MBEDTLS_X509_CRT_PARSE_C)
9507const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009508{
9509 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009510 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009511
Hanno Beckere6824572019-02-07 13:18:46 +00009512#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009513 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009514#else
9515 return( NULL );
9516#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009521int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9522 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009523{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009524 if( ssl == NULL ||
9525 dst == NULL ||
9526 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009527 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009530 }
9531
Hanno Becker52055ae2019-02-06 14:30:46 +00009532 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009535
Paul Bakker5121ce52009-01-03 21:22:43 +00009536/*
Paul Bakker1961b702013-01-25 14:49:24 +01009537 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009540{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009542
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009543 if( ssl == NULL || ssl->conf == NULL )
9544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009547 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009549#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009551 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009553#endif
9554
Paul Bakker1961b702013-01-25 14:49:24 +01009555 return( ret );
9556}
9557
9558/*
9559 * Perform the SSL handshake
9560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009562{
9563 int ret = 0;
9564
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009565 if( ssl == NULL || ssl->conf == NULL )
9566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009573
9574 if( ret != 0 )
9575 break;
9576 }
9577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009579
9580 return( ret );
9581}
9582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583#if defined(MBEDTLS_SSL_RENEGOTIATION)
9584#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009585/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009586 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009589{
9590 int ret;
9591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009593
9594 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9596 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009597
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009598 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009599 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009601 return( ret );
9602 }
9603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009605
9606 return( 0 );
9607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009609
9610/*
9611 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612 * - any side: calling mbedtls_ssl_renegotiate(),
9613 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9614 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009615 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009616 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009620{
9621 int ret;
9622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009624
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009625 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9626 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009627
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009628 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9629 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009631 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009633 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009634 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009635 ssl->handshake->out_msg_seq = 1;
9636 else
9637 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009638 }
9639#endif
9640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9642 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009647 return( ret );
9648 }
9649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009651
9652 return( 0 );
9653}
9654
9655/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009656 * Renegotiate current connection on client,
9657 * or request renegotiation on server
9658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009660{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009662
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009663 if( ssl == NULL || ssl->conf == NULL )
9664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009667 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009668 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9671 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009674
9675 /* Did we already try/start sending HelloRequest? */
9676 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009678
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009679 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009680 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009684 /*
9685 * On client, either start the renegotiation process or,
9686 * if already in progress, continue the handshake
9687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009692
9693 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009696 return( ret );
9697 }
9698 }
9699 else
9700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009704 return( ret );
9705 }
9706 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009708
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009709 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009710}
9711
9712/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009713 * Check record counters and renegotiate if they're above the limit.
9714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009716{
Andres AG2196c7f2016-12-15 17:01:16 +00009717 size_t ep_len = ssl_ep_len( ssl );
9718 int in_ctr_cmp;
9719 int out_ctr_cmp;
9720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9722 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009723 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009724 {
9725 return( 0 );
9726 }
9727
Andres AG2196c7f2016-12-15 17:01:16 +00009728 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9729 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009730 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009731 ssl->conf->renego_period + ep_len, 8 - ep_len );
9732
9733 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009734 {
9735 return( 0 );
9736 }
9737
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009742
9743/*
9744 * Receive application data decrypted from the SSL layer
9745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009747{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009748 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009749 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009750
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009751 if( ssl == NULL || ssl->conf == NULL )
9752 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009757 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009760 return( ret );
9761
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009762 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009764 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009765 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009766 return( ret );
9767 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009768 }
9769#endif
9770
Hanno Becker4a810fb2017-05-24 16:27:30 +01009771 /*
9772 * Check if renegotiation is necessary and/or handshake is
9773 * in process. If yes, perform/continue, and fall through
9774 * if an unexpected packet is received while the client
9775 * is waiting for the ServerHello.
9776 *
9777 * (There is no equivalent to the last condition on
9778 * the server-side as it is not treated as within
9779 * a handshake while waiting for the ClientHello
9780 * after a renegotiation request.)
9781 */
9782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009784 ret = ssl_check_ctr_renegotiate( ssl );
9785 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9786 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009789 return( ret );
9790 }
9791#endif
9792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009796 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9797 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009800 return( ret );
9801 }
9802 }
9803
Hanno Beckere41158b2017-10-23 13:30:32 +01009804 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009805 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009806 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009807 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009808 if( ssl->f_get_timer != NULL &&
9809 ssl->f_get_timer( ssl->p_timer ) == -1 )
9810 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009811 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009812 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009813
Hanno Becker327c93b2018-08-15 13:56:18 +01009814 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009815 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009816 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9817 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009818
Hanno Becker4a810fb2017-05-24 16:27:30 +01009819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9820 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009821 }
9822
9823 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009825 {
9826 /*
9827 * OpenSSL sends empty messages to randomize the IV
9828 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009829 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009832 return( 0 );
9833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009835 return( ret );
9836 }
9837 }
9838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009842
Hanno Becker4a810fb2017-05-24 16:27:30 +01009843 /*
9844 * - For client-side, expect SERVER_HELLO_REQUEST.
9845 * - For server-side, expect CLIENT_HELLO.
9846 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9847 */
9848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009850 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009855
9856 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009858 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009859 {
9860 continue;
9861 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009864 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009865#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009866
Hanno Becker4a810fb2017-05-24 16:27:30 +01009867#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009868 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009869 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009872
9873 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009875 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009876 {
9877 continue;
9878 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009881 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009882#endif /* MBEDTLS_SSL_SRV_C */
9883
Hanno Becker21df7f92017-10-17 11:03:26 +01009884#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009885 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009886 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9887 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9888 ssl->conf->allow_legacy_renegotiation ==
9889 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9890 {
9891 /*
9892 * Accept renegotiation request
9893 */
Paul Bakker48916f92012-09-16 19:57:18 +00009894
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009895 /* DTLS clients need to know renego is server-initiated */
9896#if defined(MBEDTLS_SSL_PROTO_DTLS)
9897 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9898 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9899 {
9900 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9901 }
9902#endif
9903 ret = ssl_start_renegotiation( ssl );
9904 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9905 ret != 0 )
9906 {
9907 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9908 return( ret );
9909 }
9910 }
9911 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009912#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009913 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009914 /*
9915 * Refuse renegotiation
9916 */
9917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920#if defined(MBEDTLS_SSL_PROTO_SSL3)
9921 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009922 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009923 /* SSLv3 does not have a "no_renegotiation" warning, so
9924 we send a fatal alert and abort the connection. */
9925 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9926 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9927 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009928 }
9929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9931#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9932 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9933 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9936 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9937 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009938 {
9939 return( ret );
9940 }
Paul Bakker48916f92012-09-16 19:57:18 +00009941 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009942 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9944 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9947 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009948 }
Paul Bakker48916f92012-09-16 19:57:18 +00009949 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009950
Hanno Becker90333da2017-10-10 11:27:13 +01009951 /* At this point, we don't know whether the renegotiation has been
9952 * completed or not. The cases to consider are the following:
9953 * 1) The renegotiation is complete. In this case, no new record
9954 * has been read yet.
9955 * 2) The renegotiation is incomplete because the client received
9956 * an application data record while awaiting the ServerHello.
9957 * 3) The renegotiation is incomplete because the client received
9958 * a non-handshake, non-application data message while awaiting
9959 * the ServerHello.
9960 * In each of these case, looping will be the proper action:
9961 * - For 1), the next iteration will read a new record and check
9962 * if it's application data.
9963 * - For 2), the loop condition isn't satisfied as application data
9964 * is present, hence continue is the same as break
9965 * - For 3), the loop condition is satisfied and read_record
9966 * will re-deliver the message that was held back by the client
9967 * when expecting the ServerHello.
9968 */
9969 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009970 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009971#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009972 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009973 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009974 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009975 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009976 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009979 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009981 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009982 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009983 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9987 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009990 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009991 }
9992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9996 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009997 }
9998
9999 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010000
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010001 /* We're going to return something now, cancel timer,
10002 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010004 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010005
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010007 /* If we requested renego but received AppData, resend HelloRequest.
10008 * Do it now, after setting in_offt, to avoid taking this branch
10009 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010011 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010013 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010014 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010017 return( ret );
10018 }
10019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010020#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010021#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010022 }
10023
10024 n = ( len < ssl->in_msglen )
10025 ? len : ssl->in_msglen;
10026
10027 memcpy( buf, ssl->in_offt, n );
10028 ssl->in_msglen -= n;
10029
10030 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010031 {
10032 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010033 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010034 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010035 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010036 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010037 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010038 /* more data available */
10039 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010040 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010043
Paul Bakker23986e52011-04-24 08:57:21 +000010044 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010045}
10046
10047/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010048 * Send application data to be encrypted by the SSL layer, taking care of max
10049 * fragment length and buffer size.
10050 *
10051 * According to RFC 5246 Section 6.2.1:
10052 *
10053 * Zero-length fragments of Application data MAY be sent as they are
10054 * potentially useful as a traffic analysis countermeasure.
10055 *
10056 * Therefore, it is possible that the input message length is 0 and the
10057 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010058 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010059static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010060 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010061{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010062 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10063 const size_t max_len = (size_t) ret;
10064
10065 if( ret < 0 )
10066 {
10067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10068 return( ret );
10069 }
10070
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010071 if( len > max_len )
10072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010074 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010077 "maximum fragment length: %d > %d",
10078 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010080 }
10081 else
10082#endif
10083 len = max_len;
10084 }
Paul Bakker887bd502011-06-08 13:10:54 +000010085
Paul Bakker5121ce52009-01-03 21:22:43 +000010086 if( ssl->out_left != 0 )
10087 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010088 /*
10089 * The user has previously tried to send the data and
10090 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10091 * written. In this case, we expect the high-level write function
10092 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010097 return( ret );
10098 }
10099 }
Paul Bakker887bd502011-06-08 13:10:54 +000010100 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010101 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010102 /*
10103 * The user is trying to send a message the first time, so we need to
10104 * copy the data into the internal buffers and setup the data structure
10105 * to keep track of partial writes
10106 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010107 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010108 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010109 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010110
Hanno Becker67bc7c32018-08-06 11:33:50 +010010111 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010114 return( ret );
10115 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010116 }
10117
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010118 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010119}
10120
10121/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010122 * Write application data, doing 1/n-1 splitting if necessary.
10123 *
10124 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010125 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010126 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010129static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010130 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010131{
10132 int ret;
10133
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010134 if( ssl->conf->cbc_record_splitting ==
10135 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010136 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10138 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10139 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010140 {
10141 return( ssl_write_real( ssl, buf, len ) );
10142 }
10143
10144 if( ssl->split_done == 0 )
10145 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010146 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010147 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010148 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010149 }
10150
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010151 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10152 return( ret );
10153 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010154
10155 return( ret + 1 );
10156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010157#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010158
10159/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010160 * Write application data (public-facing wrapper)
10161 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010162int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010163{
10164 int ret;
10165
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010167
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010168 if( ssl == NULL || ssl->conf == NULL )
10169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10170
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010171#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010172 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10173 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010174 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010175 return( ret );
10176 }
10177#endif
10178
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010179 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010180 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010181 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010182 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010184 return( ret );
10185 }
10186 }
10187
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010188#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010189 ret = ssl_write_split( ssl, buf, len );
10190#else
10191 ret = ssl_write_real( ssl, buf, len );
10192#endif
10193
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010195
10196 return( ret );
10197}
10198
10199/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010200 * Notify the peer that the connection is being closed
10201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010203{
10204 int ret;
10205
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010206 if( ssl == NULL || ssl->conf == NULL )
10207 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010210
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010211 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10217 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10218 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010221 return( ret );
10222 }
10223 }
10224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010226
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010227 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010228}
10229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010231{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010232 if( transform == NULL )
10233 return;
10234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010236 deflateEnd( &transform->ctx_deflate );
10237 inflateEnd( &transform->ctx_inflate );
10238#endif
10239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10241 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010242
Hanno Beckerd56ed242018-01-03 15:32:51 +000010243#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244 mbedtls_md_free( &transform->md_ctx_enc );
10245 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010246#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010247
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010248 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010249}
10250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251#if defined(MBEDTLS_X509_CRT_PARSE_C)
10252static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010254 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010255
10256 while( cur != NULL )
10257 {
10258 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010260 cur = next;
10261 }
10262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010264
Hanno Becker0271f962018-08-16 13:23:47 +010010265#if defined(MBEDTLS_SSL_PROTO_DTLS)
10266
10267static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10268{
10269 unsigned offset;
10270 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10271
10272 if( hs == NULL )
10273 return;
10274
Hanno Becker283f5ef2018-08-24 09:34:47 +010010275 ssl_free_buffered_record( ssl );
10276
Hanno Becker0271f962018-08-16 13:23:47 +010010277 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010278 ssl_buffering_free_slot( ssl, offset );
10279}
10280
10281static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10282 uint8_t slot )
10283{
10284 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10285 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010286
10287 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10288 return;
10289
Hanno Beckere605b192018-08-21 15:59:07 +010010290 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010291 {
Hanno Beckere605b192018-08-21 15:59:07 +010010292 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010293 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010294 mbedtls_free( hs_buf->data );
10295 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010296 }
10297}
10298
10299#endif /* MBEDTLS_SSL_PROTO_DTLS */
10300
Gilles Peskine9b562d52018-04-25 20:32:43 +020010301void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010302{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010303 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10304
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010305 if( handshake == NULL )
10306 return;
10307
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010308#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10309 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10310 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010311 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010312 handshake->async_in_progress = 0;
10313 }
10314#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10315
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010316#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10317 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10318 mbedtls_md5_free( &handshake->fin_md5 );
10319 mbedtls_sha1_free( &handshake->fin_sha1 );
10320#endif
10321#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10322#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010323#if defined(MBEDTLS_USE_PSA_CRYPTO)
10324 psa_hash_abort( &handshake->fin_sha256_psa );
10325#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010326 mbedtls_sha256_free( &handshake->fin_sha256 );
10327#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010328#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010329#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010330#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010331 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010332#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010333 mbedtls_sha512_free( &handshake->fin_sha512 );
10334#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010335#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010336#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010338#if defined(MBEDTLS_DHM_C)
10339 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010340#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341#if defined(MBEDTLS_ECDH_C)
10342 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010343#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010344#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010345 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010346#if defined(MBEDTLS_SSL_CLI_C)
10347 mbedtls_free( handshake->ecjpake_cache );
10348 handshake->ecjpake_cache = NULL;
10349 handshake->ecjpake_cache_len = 0;
10350#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010351#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010352
Janos Follath4ae5c292016-02-10 11:27:43 +000010353#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10354 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010355 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010357#endif
10358
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010359#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10360 if( handshake->psk != NULL )
10361 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010362 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010363 mbedtls_free( handshake->psk );
10364 }
10365#endif
10366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10368 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010369 /*
10370 * Free only the linked list wrapper, not the keys themselves
10371 * since the belong to the SNI callback
10372 */
10373 if( handshake->sni_key_cert != NULL )
10374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010376
10377 while( cur != NULL )
10378 {
10379 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010380 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010381 cur = next;
10382 }
10383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010384#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010385
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010386#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010387 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010388 if( handshake->ecrs_peer_cert != NULL )
10389 {
10390 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10391 mbedtls_free( handshake->ecrs_peer_cert );
10392 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010393#endif
10394
Hanno Becker75173122019-02-06 16:18:31 +000010395#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10396 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10397 mbedtls_pk_free( &handshake->peer_pubkey );
10398#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400#if defined(MBEDTLS_SSL_PROTO_DTLS)
10401 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010402 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010403 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010404#endif
10405
Hanno Becker4a63ed42019-01-08 11:39:35 +000010406#if defined(MBEDTLS_ECDH_C) && \
10407 defined(MBEDTLS_USE_PSA_CRYPTO)
10408 psa_destroy_key( handshake->ecdh_psa_privkey );
10409#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10410
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010411 mbedtls_platform_zeroize( handshake,
10412 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010413}
10414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010416{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010417 if( session == NULL )
10418 return;
10419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010421 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010422#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010423
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010424#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010426#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010427
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010428 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010429}
10430
Paul Bakker5121ce52009-01-03 21:22:43 +000010431/*
10432 * Free an SSL context
10433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010435{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010436 if( ssl == NULL )
10437 return;
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010440
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010441 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010442 {
Angus Grattond8213d02016-05-25 20:56:48 +100010443 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010445 }
10446
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010447 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010448 {
Angus Grattond8213d02016-05-25 20:56:48 +100010449 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010451 }
10452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010453#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010454 if( ssl->compress_buf != NULL )
10455 {
Angus Grattond8213d02016-05-25 20:56:48 +100010456 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010458 }
10459#endif
10460
Paul Bakker48916f92012-09-16 19:57:18 +000010461 if( ssl->transform )
10462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463 mbedtls_ssl_transform_free( ssl->transform );
10464 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010465 }
10466
10467 if( ssl->handshake )
10468 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010469 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010470 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10471 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 mbedtls_free( ssl->handshake );
10474 mbedtls_free( ssl->transform_negotiate );
10475 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010476 }
10477
Paul Bakkerc0463502013-02-14 11:19:38 +010010478 if( ssl->session )
10479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480 mbedtls_ssl_session_free( ssl->session );
10481 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010482 }
10483
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010484#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010485 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010486 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010487 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010489 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010490#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10493 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10496 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010497 }
10498#endif
10499
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010500#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010502#endif
10503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010505
Paul Bakker86f04f42013-02-14 11:20:09 +010010506 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010507 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010508}
10509
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010510/*
10511 * Initialze mbedtls_ssl_config
10512 */
10513void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10514{
10515 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10516}
10517
Simon Butcherc97b6972015-12-27 23:48:17 +000010518#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010519static int ssl_preset_default_hashes[] = {
10520#if defined(MBEDTLS_SHA512_C)
10521 MBEDTLS_MD_SHA512,
10522 MBEDTLS_MD_SHA384,
10523#endif
10524#if defined(MBEDTLS_SHA256_C)
10525 MBEDTLS_MD_SHA256,
10526 MBEDTLS_MD_SHA224,
10527#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010528#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010529 MBEDTLS_MD_SHA1,
10530#endif
10531 MBEDTLS_MD_NONE
10532};
Simon Butcherc97b6972015-12-27 23:48:17 +000010533#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010534
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010535static int ssl_preset_suiteb_ciphersuites[] = {
10536 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10537 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10538 0
10539};
10540
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010541#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010542static int ssl_preset_suiteb_hashes[] = {
10543 MBEDTLS_MD_SHA256,
10544 MBEDTLS_MD_SHA384,
10545 MBEDTLS_MD_NONE
10546};
10547#endif
10548
10549#if defined(MBEDTLS_ECP_C)
10550static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010551#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010552 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010553#endif
10554#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010555 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010556#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010557 MBEDTLS_ECP_DP_NONE
10558};
10559#endif
10560
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010561/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010562 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010563 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010564int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010565 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010566{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010567#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010568 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010569#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010570
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010571 /* Use the functions here so that they are covered in tests,
10572 * but otherwise access member directly for efficiency */
10573 mbedtls_ssl_conf_endpoint( conf, endpoint );
10574 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010575
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010576 /*
10577 * Things that are common to all presets
10578 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010579#if defined(MBEDTLS_SSL_CLI_C)
10580 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10581 {
10582 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10583#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10584 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10585#endif
10586 }
10587#endif
10588
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010589#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010590 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010591#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010592
10593#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10594 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10595#endif
10596
10597#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10598 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10599#endif
10600
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010601#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10602 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10603#endif
10604
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010605#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010606 conf->f_cookie_write = ssl_cookie_write_dummy;
10607 conf->f_cookie_check = ssl_cookie_check_dummy;
10608#endif
10609
10610#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10611 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10612#endif
10613
Janos Follath088ce432017-04-10 12:42:31 +010010614#if defined(MBEDTLS_SSL_SRV_C)
10615 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10616#endif
10617
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010618#if defined(MBEDTLS_SSL_PROTO_DTLS)
10619 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10620 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10621#endif
10622
10623#if defined(MBEDTLS_SSL_RENEGOTIATION)
10624 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010625 memset( conf->renego_period, 0x00, 2 );
10626 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010627#endif
10628
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010629#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10630 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10631 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010632 const unsigned char dhm_p[] =
10633 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10634 const unsigned char dhm_g[] =
10635 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10636
Hanno Beckera90658f2017-10-04 15:29:08 +010010637 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10638 dhm_p, sizeof( dhm_p ),
10639 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010640 {
10641 return( ret );
10642 }
10643 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010644#endif
10645
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010646 /*
10647 * Preset-specific defaults
10648 */
10649 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010650 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010651 /*
10652 * NSA Suite B
10653 */
10654 case MBEDTLS_SSL_PRESET_SUITEB:
10655 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10656 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10657 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10658 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10659
10660 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10661 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10662 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10663 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10664 ssl_preset_suiteb_ciphersuites;
10665
10666#if defined(MBEDTLS_X509_CRT_PARSE_C)
10667 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010668#endif
10669
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010670#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010671 conf->sig_hashes = ssl_preset_suiteb_hashes;
10672#endif
10673
10674#if defined(MBEDTLS_ECP_C)
10675 conf->curve_list = ssl_preset_suiteb_curves;
10676#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010677 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010678
10679 /*
10680 * Default
10681 */
10682 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010683 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10684 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10685 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10686 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10687 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10688 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10689 MBEDTLS_SSL_MIN_MINOR_VERSION :
10690 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010691 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10692 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10693
10694#if defined(MBEDTLS_SSL_PROTO_DTLS)
10695 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10696 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10697#endif
10698
10699 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10700 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10701 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10702 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10703 mbedtls_ssl_list_ciphersuites();
10704
10705#if defined(MBEDTLS_X509_CRT_PARSE_C)
10706 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10707#endif
10708
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010709#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010710 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010711#endif
10712
10713#if defined(MBEDTLS_ECP_C)
10714 conf->curve_list = mbedtls_ecp_grp_id_list();
10715#endif
10716
10717#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10718 conf->dhm_min_bitlen = 1024;
10719#endif
10720 }
10721
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722 return( 0 );
10723}
10724
10725/*
10726 * Free mbedtls_ssl_config
10727 */
10728void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10729{
10730#if defined(MBEDTLS_DHM_C)
10731 mbedtls_mpi_free( &conf->dhm_P );
10732 mbedtls_mpi_free( &conf->dhm_G );
10733#endif
10734
10735#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10736 if( conf->psk != NULL )
10737 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010738 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010739 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010740 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010741 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010742 }
10743
10744 if( conf->psk_identity != NULL )
10745 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010746 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010747 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010748 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010749 conf->psk_identity_len = 0;
10750 }
10751#endif
10752
10753#if defined(MBEDTLS_X509_CRT_PARSE_C)
10754 ssl_key_cert_free( conf->key_cert );
10755#endif
10756
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010757 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010758}
10759
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010760#if defined(MBEDTLS_PK_C) && \
10761 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010762/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767#if defined(MBEDTLS_RSA_C)
10768 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10769 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010771#if defined(MBEDTLS_ECDSA_C)
10772 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10773 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010774#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010776}
10777
Hanno Becker7e5437a2017-04-28 17:15:26 +010010778unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10779{
10780 switch( type ) {
10781 case MBEDTLS_PK_RSA:
10782 return( MBEDTLS_SSL_SIG_RSA );
10783 case MBEDTLS_PK_ECDSA:
10784 case MBEDTLS_PK_ECKEY:
10785 return( MBEDTLS_SSL_SIG_ECDSA );
10786 default:
10787 return( MBEDTLS_SSL_SIG_ANON );
10788 }
10789}
10790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010792{
10793 switch( sig )
10794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010795#if defined(MBEDTLS_RSA_C)
10796 case MBEDTLS_SSL_SIG_RSA:
10797 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799#if defined(MBEDTLS_ECDSA_C)
10800 case MBEDTLS_SSL_SIG_ECDSA:
10801 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010802#endif
10803 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010804 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010805 }
10806}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010807#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010808
Hanno Becker7e5437a2017-04-28 17:15:26 +010010809#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10810 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10811
10812/* Find an entry in a signature-hash set matching a given hash algorithm. */
10813mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10814 mbedtls_pk_type_t sig_alg )
10815{
10816 switch( sig_alg )
10817 {
10818 case MBEDTLS_PK_RSA:
10819 return( set->rsa );
10820 case MBEDTLS_PK_ECDSA:
10821 return( set->ecdsa );
10822 default:
10823 return( MBEDTLS_MD_NONE );
10824 }
10825}
10826
10827/* Add a signature-hash-pair to a signature-hash set */
10828void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10829 mbedtls_pk_type_t sig_alg,
10830 mbedtls_md_type_t md_alg )
10831{
10832 switch( sig_alg )
10833 {
10834 case MBEDTLS_PK_RSA:
10835 if( set->rsa == MBEDTLS_MD_NONE )
10836 set->rsa = md_alg;
10837 break;
10838
10839 case MBEDTLS_PK_ECDSA:
10840 if( set->ecdsa == MBEDTLS_MD_NONE )
10841 set->ecdsa = md_alg;
10842 break;
10843
10844 default:
10845 break;
10846 }
10847}
10848
10849/* Allow exactly one hash algorithm for each signature. */
10850void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10851 mbedtls_md_type_t md_alg )
10852{
10853 set->rsa = md_alg;
10854 set->ecdsa = md_alg;
10855}
10856
10857#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10858 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10859
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010860/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010861 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010864{
10865 switch( hash )
10866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867#if defined(MBEDTLS_MD5_C)
10868 case MBEDTLS_SSL_HASH_MD5:
10869 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871#if defined(MBEDTLS_SHA1_C)
10872 case MBEDTLS_SSL_HASH_SHA1:
10873 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875#if defined(MBEDTLS_SHA256_C)
10876 case MBEDTLS_SSL_HASH_SHA224:
10877 return( MBEDTLS_MD_SHA224 );
10878 case MBEDTLS_SSL_HASH_SHA256:
10879 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881#if defined(MBEDTLS_SHA512_C)
10882 case MBEDTLS_SSL_HASH_SHA384:
10883 return( MBEDTLS_MD_SHA384 );
10884 case MBEDTLS_SSL_HASH_SHA512:
10885 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010886#endif
10887 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010889 }
10890}
10891
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010892/*
10893 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10894 */
10895unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10896{
10897 switch( md )
10898 {
10899#if defined(MBEDTLS_MD5_C)
10900 case MBEDTLS_MD_MD5:
10901 return( MBEDTLS_SSL_HASH_MD5 );
10902#endif
10903#if defined(MBEDTLS_SHA1_C)
10904 case MBEDTLS_MD_SHA1:
10905 return( MBEDTLS_SSL_HASH_SHA1 );
10906#endif
10907#if defined(MBEDTLS_SHA256_C)
10908 case MBEDTLS_MD_SHA224:
10909 return( MBEDTLS_SSL_HASH_SHA224 );
10910 case MBEDTLS_MD_SHA256:
10911 return( MBEDTLS_SSL_HASH_SHA256 );
10912#endif
10913#if defined(MBEDTLS_SHA512_C)
10914 case MBEDTLS_MD_SHA384:
10915 return( MBEDTLS_SSL_HASH_SHA384 );
10916 case MBEDTLS_MD_SHA512:
10917 return( MBEDTLS_SSL_HASH_SHA512 );
10918#endif
10919 default:
10920 return( MBEDTLS_SSL_HASH_NONE );
10921 }
10922}
10923
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010924#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010925/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010926 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010927 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010928 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010929int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010930{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010932
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010933 if( ssl->conf->curve_list == NULL )
10934 return( -1 );
10935
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010936 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010937 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010938 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010939
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010940 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010941}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010942#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010943
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010944#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010945/*
10946 * Check if a hash proposed by the peer is in our list.
10947 * Return 0 if we're willing to use it, -1 otherwise.
10948 */
10949int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10950 mbedtls_md_type_t md )
10951{
10952 const int *cur;
10953
10954 if( ssl->conf->sig_hashes == NULL )
10955 return( -1 );
10956
10957 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10958 if( *cur == (int) md )
10959 return( 0 );
10960
10961 return( -1 );
10962}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010963#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010965#if defined(MBEDTLS_X509_CRT_PARSE_C)
10966int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10967 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010968 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010969 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010970{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010971 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010972#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010973 int usage = 0;
10974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010976 const char *ext_oid;
10977 size_t ext_len;
10978#endif
10979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010980#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10981 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010982 ((void) cert);
10983 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010984 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010985#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010987#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10988 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010989 {
10990 /* Server part of the key exchange */
10991 switch( ciphersuite->key_exchange )
10992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993 case MBEDTLS_KEY_EXCHANGE_RSA:
10994 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010995 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010996 break;
10997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10999 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11000 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11001 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011002 break;
11003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11005 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011006 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011007 break;
11008
11009 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010 case MBEDTLS_KEY_EXCHANGE_NONE:
11011 case MBEDTLS_KEY_EXCHANGE_PSK:
11012 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11013 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011014 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011015 usage = 0;
11016 }
11017 }
11018 else
11019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11021 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011022 }
11023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011025 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011026 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011027 ret = -1;
11028 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011029#else
11030 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011033#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11034 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11037 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011038 }
11039 else
11040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011041 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11042 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011043 }
11044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011045 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011046 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011047 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011048 ret = -1;
11049 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011050#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011051
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011052 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011053}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011055
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011056/*
11057 * Convert version numbers to/from wire format
11058 * and, for DTLS, to/from TLS equivalent.
11059 *
11060 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011061 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011062 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11063 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011065void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011066 unsigned char ver[2] )
11067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068#if defined(MBEDTLS_SSL_PROTO_DTLS)
11069 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011072 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11073
11074 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11075 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11076 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011077 else
11078#else
11079 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011080#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011081 {
11082 ver[0] = (unsigned char) major;
11083 ver[1] = (unsigned char) minor;
11084 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011085}
11086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011087void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011088 const unsigned char ver[2] )
11089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011090#if defined(MBEDTLS_SSL_PROTO_DTLS)
11091 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011092 {
11093 *major = 255 - ver[0] + 2;
11094 *minor = 255 - ver[1] + 1;
11095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011096 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011097 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11098 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011099 else
11100#else
11101 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011102#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011103 {
11104 *major = ver[0];
11105 *minor = ver[1];
11106 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011107}
11108
Simon Butcher99000142016-10-13 17:21:01 +010011109int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11110{
11111#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11112 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11113 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11114
11115 switch( md )
11116 {
11117#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11118#if defined(MBEDTLS_MD5_C)
11119 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011120 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011121#endif
11122#if defined(MBEDTLS_SHA1_C)
11123 case MBEDTLS_SSL_HASH_SHA1:
11124 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11125 break;
11126#endif
11127#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11128#if defined(MBEDTLS_SHA512_C)
11129 case MBEDTLS_SSL_HASH_SHA384:
11130 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11131 break;
11132#endif
11133#if defined(MBEDTLS_SHA256_C)
11134 case MBEDTLS_SSL_HASH_SHA256:
11135 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11136 break;
11137#endif
11138 default:
11139 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11140 }
11141
11142 return 0;
11143#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11144 (void) ssl;
11145 (void) md;
11146
11147 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11148#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11149}
11150
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011151#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11152 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11153int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11154 unsigned char *output,
11155 unsigned char *data, size_t data_len )
11156{
11157 int ret = 0;
11158 mbedtls_md5_context mbedtls_md5;
11159 mbedtls_sha1_context mbedtls_sha1;
11160
11161 mbedtls_md5_init( &mbedtls_md5 );
11162 mbedtls_sha1_init( &mbedtls_sha1 );
11163
11164 /*
11165 * digitally-signed struct {
11166 * opaque md5_hash[16];
11167 * opaque sha_hash[20];
11168 * };
11169 *
11170 * md5_hash
11171 * MD5(ClientHello.random + ServerHello.random
11172 * + ServerParams);
11173 * sha_hash
11174 * SHA(ClientHello.random + ServerHello.random
11175 * + ServerParams);
11176 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011177 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011178 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011180 goto exit;
11181 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011182 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011183 ssl->handshake->randbytes, 64 ) ) != 0 )
11184 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011186 goto exit;
11187 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011188 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011189 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011191 goto exit;
11192 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011193 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011194 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011196 goto exit;
11197 }
11198
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011199 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011200 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011202 goto exit;
11203 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011204 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011205 ssl->handshake->randbytes, 64 ) ) != 0 )
11206 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011208 goto exit;
11209 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011210 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011211 data_len ) ) != 0 )
11212 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011214 goto exit;
11215 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011216 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011217 output + 16 ) ) != 0 )
11218 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011219 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011220 goto exit;
11221 }
11222
11223exit:
11224 mbedtls_md5_free( &mbedtls_md5 );
11225 mbedtls_sha1_free( &mbedtls_sha1 );
11226
11227 if( ret != 0 )
11228 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11229 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11230
11231 return( ret );
11232
11233}
11234#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11235 MBEDTLS_SSL_PROTO_TLS1_1 */
11236
11237#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11238 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011239
11240#if defined(MBEDTLS_USE_PSA_CRYPTO)
11241int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11242 unsigned char *hash, size_t *hashlen,
11243 unsigned char *data, size_t data_len,
11244 mbedtls_md_type_t md_alg )
11245{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011246 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011247 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011248 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11249
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011251
11252 if( ( status = psa_hash_setup( &hash_operation,
11253 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011254 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011255 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011256 goto exit;
11257 }
11258
Andrzej Kurek814feff2019-01-14 04:35:19 -050011259 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11260 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011261 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011262 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011263 goto exit;
11264 }
11265
Andrzej Kurek814feff2019-01-14 04:35:19 -050011266 if( ( status = psa_hash_update( &hash_operation,
11267 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011268 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011269 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011270 goto exit;
11271 }
11272
Andrzej Kurek814feff2019-01-14 04:35:19 -050011273 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11274 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011275 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011276 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011277 goto exit;
11278 }
11279
11280exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011281 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011282 {
11283 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11284 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011285 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011286 {
11287 case PSA_ERROR_NOT_SUPPORTED:
11288 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011289 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011290 case PSA_ERROR_BUFFER_TOO_SMALL:
11291 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11292 case PSA_ERROR_INSUFFICIENT_MEMORY:
11293 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11294 default:
11295 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11296 }
11297 }
11298 return( 0 );
11299}
11300
11301#else
11302
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011303int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011304 unsigned char *hash, size_t *hashlen,
11305 unsigned char *data, size_t data_len,
11306 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011307{
11308 int ret = 0;
11309 mbedtls_md_context_t ctx;
11310 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011311 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011312
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011314
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011315 mbedtls_md_init( &ctx );
11316
11317 /*
11318 * digitally-signed struct {
11319 * opaque client_random[32];
11320 * opaque server_random[32];
11321 * ServerDHParams params;
11322 * };
11323 */
11324 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11325 {
11326 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11327 goto exit;
11328 }
11329 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11330 {
11331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11332 goto exit;
11333 }
11334 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11335 {
11336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11337 goto exit;
11338 }
11339 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11340 {
11341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11342 goto exit;
11343 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011344 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345 {
11346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11347 goto exit;
11348 }
11349
11350exit:
11351 mbedtls_md_free( &ctx );
11352
11353 if( ret != 0 )
11354 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11355 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11356
11357 return( ret );
11358}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011359#endif /* MBEDTLS_USE_PSA_CRYPTO */
11360
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011361#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11362 MBEDTLS_SSL_PROTO_TLS1_2 */
11363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011364#endif /* MBEDTLS_SSL_TLS_C */